package org.jboss.test;
import org.apache.log4j.Category;
import org.jboss.jmx.adaptor.rmi.RMIAdaptor;
import org.jboss.test.util.AppCallbackHandler;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.security.auth.login.LoginContext;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Hashtable;
public class JBossTestServices
{
public final static String DEPLOYER_NAME = "jboss.system:service=MainDeployer";
public final static String DEFAULT_USERNAME = "jduke";
public final static String DEFAULT_PASSWORD = "theduke";
public final static String DEFAULT_LOGIN_CONFIG = "other";
public final static int DEFAULT_THREADCOUNT = 10;
public final static int DEFAULT_ITERATIONCOUNT = 1000;
public final static int DEFAULT_BEANCOUNT = 100;
protected RMIAdaptor server;
protected Category log;
protected InitialContext initialContext;
protected Hashtable jndiEnv;
protected LoginContext lc;
public JBossTestServices(String className)
{
log = Category.getInstance(className);
log.debug("JBossTestServices(), className=" + className);
}
public void setUp() throws Exception
{
log.debug("JBossTestServices.setUp()");
init();
log.info("jbosstest.beancount: " + System.getProperty("jbosstest.beancount"));
log.info("jbosstest.iterationcount: " + System.getProperty("jbosstest.iterationcount"));
log.info("jbosstest.threadcount: " + System.getProperty("jbosstest.threadcount"));
log.info("jbosstest.nodeploy: " + System.getProperty("jbosstest.nodeploy"));
log.info("jbosstest.jndiurl: " + this.getJndiURL());
log.info("jbosstest.jndifactory: " + this.getJndiInitFactory());
}
public void tearDown() throws Exception
{
log.debug("JBossTestServices.tearDown()");
}
InitialContext getInitialContext() throws Exception
{
return initialContext;
}
public RMIAdaptor getServer() throws Exception
{
return server;
}
Category getLog()
{
return log;
}
ObjectName getDeployerName() throws MalformedObjectNameException
{
return new ObjectName(DEPLOYER_NAME);
}
protected String getDeployURL(final String filename)
throws MalformedURLException
{
try
{
return new URL(filename).toString();
}
catch (MalformedURLException e)
{
log.debug(filename + " is not a valid URL, " + e.getMessage());
}
String deployDir = System.getProperty("jbosstest.deploy.dir");
if (deployDir == null)
{
deployDir = "../lib";
}
String url = deployDir + "/" + filename;
log.debug("Testing file: " + url);
File file = new File(url);
if (file.exists())
{
log.debug(file.getAbsolutePath() + " is a valid file");
return file.toURL().toString();
}
else
{
log.debug("File does not exist, creating url: " + url);
return new URL(url).toString();
}
}
protected Object invoke(ObjectName name, String method, Object[] args, String[] sig) throws Exception
{
return invoke(getServer(), name, method, args, sig);
}
protected Object invoke(RMIAdaptor server, ObjectName name, String method, Object[] args, String[] sig)
throws Exception
{
try
{
this.getLog().debug("Invoking " + name.getCanonicalName() + " method=" + method);
this.getLog().debug(args.toString());
return server.invoke(name, method, args, sig);
}
catch (javax.management.MBeanException e)
{
log.error("MbeanException", e.getTargetException());
throw e.getTargetException();
}
catch (javax.management.ReflectionException e)
{
log.error("ReflectionException", e.getTargetException());
throw e.getTargetException();
}
catch (javax.management.RuntimeOperationsException e)
{
log.error("RuntimeOperationsException", e.getTargetException());
throw e.getTargetException();
}
catch (javax.management.RuntimeMBeanException e)
{
log.error("RuntimeMbeanException", e.getTargetException());
throw e.getTargetException();
}
catch (javax.management.RuntimeErrorException e)
{
log.error("RuntimeErrorException", e.getTargetError());
throw e.getTargetError();
}
}
public void deploy(String name) throws Exception
{
if (Boolean.getBoolean("jbosstest.nodeploy") == true)
{
log.debug("Skipping deployment of: " + name);
return;
}
String deployURL = getDeployURL(name);
log.debug("Deploying " + name + ", url=" + deployURL);
invoke(getDeployerName(),
"deploy",
new Object[]{deployURL},
new String[]{"java.lang.String"});
}
public void redeploy(String name) throws Exception
{
if (Boolean.getBoolean("jbosstest.nodeploy") == true)
{
log.debug("Skipping redeployment of: " + name);
return;
}
String deployURL = getDeployURL(name);
log.debug("Deploying " + name + ", url=" + deployURL);
invoke(getDeployerName(),
"redeploy",
new Object[]{deployURL},
new String[]{"java.lang.String"});
}
public void login() throws Exception
{
flushAuthCache("other");
String username = getUsername();
String pass = getPassword();
String config = getLoginConfig();
char[] password = null;
if (pass != null)
password = pass.toCharArray();
AppCallbackHandler handler = new AppCallbackHandler(username, password);
getLog().debug("Creating LoginContext(" + config + ")");
lc = new LoginContext(config, handler);
lc.login();
getLog().debug("Created LoginContext, subject=" + lc.getSubject());
}
public void logout()
{
try
{
getLog().debug("logout, LoginContext: " + lc);
if (lc != null)
lc.logout();
}
catch (Exception e)
{
getLog().error("logout error: ", e);
}
}
public void undeploy(String name) throws Exception
{
if (Boolean.getBoolean("jbosstest.nodeploy") == true)
return;
String deployURL = getDeployURL(name);
log.debug("Undeploying " + name + ", url=" + deployURL);
Object[] args = {deployURL};
String[] sig = {"java.lang.String"};
invoke(getDeployerName(), "undeploy", args, sig);
}
void flushAuthCache(String domain) throws Exception
{
ObjectName jaasMgr = new ObjectName("jboss.security:service=JaasSecurityManager");
Object[] params = {domain};
String[] signature = {"java.lang.String"};
invoke(jaasMgr, "flushAuthenticationCache", params, signature);
}
void restartDBPool() throws Exception
{
ObjectName dbPool = new ObjectName("jboss.jca:service=ManagedConnectionPool,name=DefaultDS");
Object[] params = {};
String[] signature = {};
invoke(dbPool, "stop", params, signature);
invoke(dbPool, "start", params, signature);
}
boolean isSecure()
{
return Boolean.getBoolean("jbosstest.secure");
}
String getUsername()
{
return System.getProperty("jbosstest.username", DEFAULT_USERNAME);
}
String getPassword()
{
return System.getProperty("jbosstest.password", DEFAULT_PASSWORD);
}
String getLoginConfig()
{
return System.getProperty("jbosstest.loginconfig", DEFAULT_LOGIN_CONFIG);
}
String getJndiURL()
{
String url = (String)jndiEnv.get(Context.PROVIDER_URL);
return url;
}
String getJndiInitFactory()
{
String factory = (String)jndiEnv.get(Context.INITIAL_CONTEXT_FACTORY);
return factory;
}
int getThreadCount()
{
return Integer.getInteger("jbosstest.threadcount", DEFAULT_THREADCOUNT).intValue();
}
int getIterationCount()
{
return Integer.getInteger("jbosstest.iterationcount", DEFAULT_ITERATIONCOUNT).intValue();
}
int getBeanCount()
{
return Integer.getInteger("jbosstest.beancount", DEFAULT_BEANCOUNT).intValue();
}
public void init() throws Exception
{
if (initialContext == null)
{
initialContext = new InitialContext();
jndiEnv = initialContext.getEnvironment();
}
if (server == null)
{
String adaptorName = System.getProperty("jbosstest.server.name", "jmx/invoker/RMIAdaptor");
server = (RMIAdaptor)initialContext.lookup(adaptorName);
}
}
public void reinit() throws Exception
{
initialContext = null;
server = null;
init();
}
public String getServerHost()
{
String hostName = System.getProperty("jbosstest.server.host", "localhost");
return hostName;
}
}