1 22 package org.jboss.test; 23 24 import java.io.File ; 25 import java.net.MalformedURLException ; 26 import java.net.URL ; 27 import java.util.Arrays ; 28 import java.util.Hashtable ; 29 30 import javax.management.MBeanServerConnection ; 31 import javax.management.MalformedObjectNameException ; 32 import javax.management.ObjectName ; 33 import javax.naming.Context ; 34 import javax.naming.InitialContext ; 35 import javax.security.auth.login.LoginContext ; 36 37 import org.jboss.logging.Logger; 38 import org.jboss.test.util.AppCallbackHandler; 39 40 56 public class JBossTestServices extends AbstractTestDelegate 57 { 58 public final static String DEPLOYER_NAME = "jboss.system:service=MainDeployer"; 60 public final static String DEFAULT_USERNAME = "jduke"; 61 public final static String DEFAULT_PASSWORD = "theduke"; 62 public final static String DEFAULT_LOGIN_CONFIG = "other"; 63 public final static int DEFAULT_THREADCOUNT = 10; 64 public final static int DEFAULT_ITERATIONCOUNT = 1000; 65 public final static int DEFAULT_BEANCOUNT = 100; 66 67 protected MBeanServerConnection server; 69 protected InitialContext initialContext; 70 protected Hashtable jndiEnv; 71 protected LoginContext lc; 72 73 private static Class getClass(String className) 74 { 75 Class clazz; 76 try 77 { 78 clazz = Class.forName(className); 79 } 80 catch(Exception e) 81 { 82 throw new RuntimeException (e); 83 } 84 return clazz; 85 } 86 91 public JBossTestServices(String className) 92 { 93 super(getClass(className)); 94 } 95 public JBossTestServices(Class clazz) 96 { 97 super(clazz); 98 } 99 100 102 107 public void setUp() throws Exception 108 { 109 super.setUp(); 110 log = getLog(); 111 log.debug("JBossTestServices.setUp()"); 112 init(); 113 log.info("jbosstest.beancount: " + System.getProperty("jbosstest.beancount")); 114 log.info("jbosstest.iterationcount: " + System.getProperty("jbosstest.iterationcount")); 115 log.info("jbosstest.threadcount: " + System.getProperty("jbosstest.threadcount")); 116 log.info("jbosstest.nodeploy: " + System.getProperty("jbosstest.nodeploy")); 117 log.info("jbosstest.jndiurl: " + this.getJndiURL()); 118 log.info("jbosstest.jndifactory: " + this.getJndiInitFactory()); 119 } 120 121 126 public void tearDown() throws Exception 127 { 128 log.debug("JBossTestServices.tearDown()"); 130 } 131 132 133 139 public InitialContext getInitialContext() throws Exception 140 { 141 return initialContext; 142 } 143 144 150 public MBeanServerConnection getServer() throws Exception 151 { 152 if (server == null) 153 { 154 String adaptorName = System.getProperty("jbosstest.server.name", "jmx/invoker/RMIAdaptor"); 155 server = (MBeanServerConnection )initialContext.lookup(adaptorName); 156 } 157 return server; 158 } 159 160 166 ObjectName getDeployerName() throws MalformedObjectNameException 167 { 168 return new ObjectName (DEPLOYER_NAME); 169 } 170 171 181 protected URL getDeployURL(final String filename) 182 throws MalformedURLException 183 { 184 try 186 { 187 return new URL (filename); 188 } 189 catch (MalformedURLException e) 190 { 191 log.debug(filename + " is not a valid URL, " + e.getMessage()); 192 } 193 194 String deployDir = System.getProperty("jbosstest.deploy.dir"); 196 if (deployDir == null) 197 { 198 deployDir = "output/lib"; 199 } 200 String url = deployDir + "/" + filename; 201 log.debug("Testing file: " + url); 202 File file = new File (url); 204 if (file.exists()) 205 { 206 log.debug(file.getAbsolutePath() + " is a valid file"); 207 return file.toURL(); 208 } 209 else 210 { 211 log.debug("File does not exist, creating url: " + url); 212 return new URL (url); 213 } 214 } 215 216 227 protected Object invoke(ObjectName name, String method, Object [] args, String [] sig) throws Exception 228 { 229 return invoke(getServer(), name, method, args, sig); 230 } 231 232 protected Object invoke(MBeanServerConnection server, ObjectName name, String method, Object [] args, String [] sig) 233 throws Exception 234 { 235 try 236 { 237 this.getLog().debug("Invoking " + name.getCanonicalName() + " method=" + method); 238 if (args != null) 239 this.getLog().debug("args=" + Arrays.asList(args)); 240 return server.invoke(name, method, args, sig); 241 } 242 catch (javax.management.MBeanException e) 243 { 244 log.error("MbeanException", e.getTargetException()); 245 throw e.getTargetException(); 246 } 247 catch (javax.management.ReflectionException e) 248 { 249 log.error("ReflectionException", e.getTargetException()); 250 throw e.getTargetException(); 251 } 252 catch (javax.management.RuntimeOperationsException e) 253 { 254 log.error("RuntimeOperationsException", e.getTargetException()); 255 throw e.getTargetException(); 256 } 257 catch (javax.management.RuntimeMBeanException e) 258 { 259 log.error("RuntimeMbeanException", e.getTargetException()); 260 throw e.getTargetException(); 261 } 262 catch (javax.management.RuntimeErrorException e) 263 { 264 log.error("RuntimeErrorException", e.getTargetError()); 265 throw e.getTargetError(); 266 } 267 } 268 269 270 277 public void deploy(String name) throws Exception 278 { 279 if (Boolean.getBoolean("jbosstest.nodeploy") == true) 280 { 281 log.debug("Skipping deployment of: " + name); 282 return; 283 } 284 285 URL deployURL = getDeployURL(name); 286 log.debug("Deploying " + name + ", url=" + deployURL); 287 invoke(getDeployerName(), 288 "deploy", 289 new Object []{deployURL}, 290 new String []{"java.net.URL"}); 291 } 292 293 public void redeploy(String name) throws Exception 294 { 295 if (Boolean.getBoolean("jbosstest.nodeploy") == true) 296 { 297 log.debug("Skipping redeployment of: " + name); 298 return; 299 } 300 301 URL deployURL = getDeployURL(name); 302 log.debug("Deploying " + name + ", url=" + deployURL); 303 invoke(getDeployerName(), 304 "redeploy", 305 new Object []{deployURL}, 306 new String []{"java.net.URL"}); 307 } 308 309 312 public void login() throws Exception 313 { 314 flushAuthCache("other"); 315 String username = getUsername(); 316 String pass = getPassword(); 317 String config = getLoginConfig(); 318 char[] password = null; 319 if (pass != null) 320 password = pass.toCharArray(); 321 AppCallbackHandler handler = new AppCallbackHandler(username, password); 322 getLog().debug("Creating LoginContext(" + config + ")"); 323 lc = new LoginContext (config, handler); 324 lc.login(); 325 getLog().debug("Created LoginContext, subject=" + lc.getSubject()); 326 } 327 328 public void logout() 329 { 330 try 331 { 332 getLog().debug("logout, LoginContext: " + lc); 333 if (lc != null) 334 lc.logout(); 335 } 336 catch (Exception e) 337 { 338 getLog().error("logout error: ", e); 339 } 340 } 341 342 349 public void undeploy(String name) throws Exception 350 { 351 if (Boolean.getBoolean("jbosstest.nodeploy") == true) 352 return; 353 URL deployURL = getDeployURL(name); 354 log.debug("Undeploying " + name + ", url=" + deployURL); 355 Object [] args = {deployURL}; 356 String [] sig = {"java.net.URL"}; 357 invoke(getDeployerName(), "undeploy", args, sig); 358 } 359 360 363 void flushAuthCache(String domain) throws Exception 364 { 365 ObjectName jaasMgr = new ObjectName ("jboss.security:service=JaasSecurityManager"); 366 Object [] params = {domain}; 367 String [] signature = {"java.lang.String"}; 368 invoke(jaasMgr, "flushAuthenticationCache", params, signature); 369 } 370 371 void restartDBPool() throws Exception 372 { 373 ObjectName dbPool = new ObjectName ("jboss.jca:service=ManagedConnectionPool,name=DefaultDS"); 374 Object [] params = {}; 375 String [] signature = {}; 376 invoke(dbPool, "stop", params, signature); 377 invoke(dbPool, "start", params, signature); 378 } 379 380 boolean isSecure() 381 { 382 return Boolean.getBoolean("jbosstest.secure"); 383 } 384 385 String getUsername() 386 { 387 return System.getProperty("jbosstest.username", DEFAULT_USERNAME); 388 } 389 390 String getPassword() 391 { 392 return System.getProperty("jbosstest.password", DEFAULT_PASSWORD); 393 } 394 395 String getLoginConfig() 396 { 397 return System.getProperty("jbosstest.loginconfig", DEFAULT_LOGIN_CONFIG); 398 } 399 400 String getJndiURL() 401 { 402 String url = (String )jndiEnv.get(Context.PROVIDER_URL); 403 return url; 404 } 405 406 String getJndiInitFactory() 407 { 408 String factory = (String )jndiEnv.get(Context.INITIAL_CONTEXT_FACTORY); 409 return factory; 410 } 411 412 int getThreadCount() 413 { 414 int result = Integer.getInteger("jbosstest.threadcount", DEFAULT_THREADCOUNT).intValue(); 415 log.debug("jbosstest.threadcount=" + result); 416 return result; 417 } 418 419 int getIterationCount() 420 { 421 int result = Integer.getInteger("jbosstest.iterationcount", DEFAULT_ITERATIONCOUNT).intValue(); 422 log.debug("jbosstest.iterationcount=" + result); 423 return result; 424 } 425 426 int getBeanCount() 427 { 428 int result = Integer.getInteger("jbosstest.beancount", DEFAULT_BEANCOUNT).intValue(); 429 log.debug("jbosstest.beancount=" + result); 430 return result; 431 } 432 433 438 public void init() throws Exception 439 { 440 if (initialContext == null) 441 { 442 initialContext = new InitialContext (); 443 log.debug("initialContext.getEnvironment()=" + initialContext.getEnvironment()); 444 jndiEnv = initialContext.getEnvironment(); 445 } 446 } 447 448 453 public void reinit() throws Exception 454 { 455 initialContext = null; 456 server = null; 457 init(); 458 } 459 460 466 public String getServerHost() 467 { 468 String hostName = System.getProperty("jbosstest.server.host", "localhost"); 469 return hostName; 470 } 471 } 472 | Popular Tags |