1 22 package org.jboss.ejb3.embedded; 23 24 import java.io.File ; 25 import java.net.URL ; 26 import java.util.ArrayList ; 27 import java.util.HashSet ; 28 import java.util.Hashtable ; 29 import java.util.Iterator ; 30 import java.util.Properties ; 31 import java.util.StringTokenizer ; 32 33 import javax.management.MBeanServer ; 34 import javax.management.MBeanServerFactory ; 35 import javax.management.ObjectName ; 36 import javax.naming.Context ; 37 38 import org.jboss.aop.AspectXmlLoader; 39 import org.jboss.ejb3.InitialContextFactory; 40 import org.jboss.ejb3.KernelAbstractionFactory; 41 import org.jboss.dependency.spi.ControllerContext; 42 import org.jboss.kernel.Kernel; 43 import org.jboss.kernel.plugins.bootstrap.basic.BasicBootstrap; 44 import org.jboss.kernel.plugins.deployment.xml.BeanXMLDeployer; 45 import org.jboss.kernel.spi.deployment.KernelDeployment; 46 import org.jboss.logging.Logger; 47 import org.jboss.mx.server.ServerConstants; 48 import org.jboss.mx.util.ObjectNameFactory; 49 50 61 public class EJB3StandaloneBootstrap 62 { 63 private static final Logger log = Logger.getLogger(EJB3StandaloneBootstrap.class); 64 65 private final static ObjectName DEFAULT_LOADER_NAME = 66 ObjectNameFactory.create(ServerConstants.DEFAULT_LOADER_NAME); 67 68 public static Kernel kernel; 69 private static MBeanServer mbeanServer; 70 private static EJB3StandaloneDeployer deployer; 71 72 73 private static boolean warned = false; 74 75 public static Kernel getKernel() 76 { 77 return kernel; 78 } 79 80 private static ArrayList <KernelDeployment> deployments = new ArrayList <KernelDeployment>(); 81 private static ArrayList <URL > aopDeployments = new ArrayList <URL >(); 82 83 84 89 public static void deployXmlResource(String resource) 90 { 91 try 92 { 93 BeanXMLDeployer deployer = new BeanXMLDeployer(kernel); 94 URL bootstrap = Thread.currentThread().getContextClassLoader().getResource(resource); 95 if (bootstrap == null) 96 throw new RuntimeException ("Cannot find " + resource); 97 KernelDeployment deployment = deployer.deploy(bootstrap); 98 deployments.add(0, deployment); 99 deployer.validate(); 100 } 101 catch (Throwable throwable) 102 { 103 throw new RuntimeException (throwable); 104 } 105 } 106 107 public static Object getDeployment(String name) 108 { 109 Iterator allDeployments = deployments.iterator(); 110 while (allDeployments.hasNext()) 111 { 112 KernelDeployment deployment = (KernelDeployment)allDeployments.next(); 113 Iterator contexts = deployment.getInstalledContexts().iterator(); 114 while (contexts.hasNext()) 115 { 116 org.jboss.kernel.plugins.dependency.AbstractKernelControllerContext context = (org.jboss.kernel.plugins.dependency.AbstractKernelControllerContext)contexts.next(); 117 if (context.getName().equals(name)) 118 return context.getTarget(); 119 } 120 } 121 122 return null; 123 } 124 125 public static HashSet ignoredJars = new HashSet (); 126 127 static 128 { 129 ignoredJars.add("antlr-2.7.5H3.jar"); 130 ignoredJars.add("asm-attrs.jar"); 131 ignoredJars.add("asm.jar"); 132 ignoredJars.add("cglib-2.1.1.jar"); 133 ignoredJars.add("commons-collections.jar"); 134 ignoredJars.add("commons-logging-api.jar"); 135 ignoredJars.add("commons-logging.jar"); 136 ignoredJars.add("concurrent.jar"); 137 ignoredJars.add("dom4j.jar"); 138 ignoredJars.add("ejb3-persistence.jar"); 139 ignoredJars.add("hibernate3.jar"); 140 ignoredJars.add("hibernate-annotations.jar"); 141 ignoredJars.add("hibernate-entitymanager.jar"); 142 ignoredJars.add("hsqldb.jar"); 143 ignoredJars.add("javassist.jar"); 144 ignoredJars.add("jboss-annotations-ejb3.jar"); 145 ignoredJars.add("jboss-aop-jdk50.jar"); 146 ignoredJars.add("jboss-aspect-library-jdk50.jar"); 147 ignoredJars.add("jboss-common.jar"); 148 ignoredJars.add("jboss-common-jdbc-wrapper.jar"); 149 ignoredJars.add("jboss-container.jar"); 150 ignoredJars.add("jboss-dependency.jar"); 151 ignoredJars.add("jboss-ejb3.jar"); 152 ignoredJars.add("jboss-ejb3x.jar"); 153 ignoredJars.add("jboss-j2ee.jar"); 154 ignoredJars.add("jboss-j2se.jar"); 155 ignoredJars.add("jboss.jar"); 156 ignoredJars.add("jboss-jca.jar"); 157 ignoredJars.add("jboss-local-jdbc.jar"); 158 ignoredJars.add("jboss-microcontainer.jar"); 159 ignoredJars.add("jbossmq.jar"); 160 ignoredJars.add("jboss-remoting.jar"); 161 ignoredJars.add("jbosssx.jar"); 162 ignoredJars.add("jboss-system.jar"); 163 ignoredJars.add("jboss-transaction.jar"); 164 ignoredJars.add("jboss-xa-jdbc.jar"); 165 ignoredJars.add("jnpserver.jar"); 166 ignoredJars.add("log4j.jar"); 167 ignoredJars.add("resolver.jar"); 168 ignoredJars.add("trove.jar"); 169 ignoredJars.add("xercesImpl.jar"); 170 ignoredJars.add("xml-apis.jar"); 171 ignoredJars.add("jboss-ejb3-all.jar"); 172 ignoredJars.add("hibernate-all.jar"); 173 ignoredJars.add("thirdparty-all.jar"); 174 } 175 176 177 public static Hashtable getInitialContextProperties() 178 { 179 Hashtable hash = null; 180 ControllerContext context = kernel.getController().getInstalledContext("InitialContextProperties"); 181 if (context != null) 182 hash = (Hashtable ) context.getTarget(); 183 else if (warned == false) 184 { 185 log.warn("Could not find an configured InitialContextProperties, this is ok if your already have a correct jndi.properties file"); 186 warned = true; 187 } 188 return hash; 189 } 190 191 192 197 public static void scanClasspath(String paths) 198 { 199 try 200 { 201 String classpath = System.getProperty("java.class.path"); 202 StringTokenizer tokenizer = new StringTokenizer (classpath, File.pathSeparator); 203 String [] split = paths.split(","); 204 for (int i = 0; i < split.length; i++) 205 { 206 split[i] = split[i].trim(); 207 } 208 209 deployer = createDeployer(); 210 211 while (tokenizer.hasMoreTokens()) 212 { 213 String path = tokenizer.nextToken().trim(); 214 boolean found = false; 215 for (String wantedPath : split) 216 { 217 if (path.endsWith(System.getProperty("file.separator") + wantedPath)) 218 { 219 found = true; 220 break; 221 } 222 } 223 if (!found) continue; 224 File fp = new File (path); 225 if (ignoredJars.contains(fp.getName())) continue; 226 URL archive = fp.toURL(); 227 deployer.getArchives().add(archive); 228 } 229 if (deployer.getArchives().size() == 0) 230 { 231 deployer = null; 232 return; 233 } 234 deployer.create(); 235 deployer.start(); 236 237 } 238 catch (Exception e) 239 { 240 throw new RuntimeException (e); 241 } 242 if (!KernelErrors.validate(kernel)) throw new RuntimeException ("Problems scanning classpath"); 243 244 } 245 246 251 public static void scanClasspath() 252 { 253 try 254 { 255 String classpath = System.getProperty("java.class.path"); 256 StringTokenizer tokenizer = new StringTokenizer (classpath, File.pathSeparator); 257 258 deployer = createDeployer(); 259 260 while (tokenizer.hasMoreTokens()) 261 { 262 String path = tokenizer.nextToken(); 263 File fp = new File (path); 264 if (ignoredJars.contains(fp.getName())) continue; 265 URL archive = fp.toURL(); 266 deployer.getArchives().add(archive); 267 } 268 if (deployer.getArchives().size() == 0) 269 { 270 deployer = null; 271 return; 272 } 273 deployer.create(); 274 deployer.start(); 275 276 } 277 catch (Exception e) 278 { 279 throw new RuntimeException (e); 280 } 281 if (!KernelErrors.validate(kernel)) throw new RuntimeException ("Problems scanning classpath"); 282 283 } 284 285 291 public static EJB3StandaloneDeployer createDeployer() 292 { 293 EJB3StandaloneDeployer deployer; 294 deployer = new EJB3StandaloneDeployer(); 295 deployer.setJndiProperties(getInitialContextProperties()); 296 deployer.setKernel(kernel); 297 deployer.setMbeanServer(mbeanServer); 298 299 return deployer; 300 } 301 302 public static void shutdown() 303 { 304 try 305 { 306 if (deployer != null) 307 { 308 deployer.stop(); 309 deployer.destroy(); 310 deployer = null; 311 } 312 BeanXMLDeployer deployer = new BeanXMLDeployer(kernel); 313 for (KernelDeployment deployment : deployments) 314 { 315 deployer.undeploy(deployment); 316 } 317 318 for (URL url : aopDeployments) 319 { 320 try 321 { 322 AspectXmlLoader.undeployXML(url); 323 } 324 catch (Exception e) 325 { 326 log.warn(e); 327 } 328 } 329 kernel = null; 330 } 331 catch (Throwable throwable) 332 { 333 log.warn(throwable); 334 } 335 336 } 337 338 private static void initInitialContext() 339 { 340 Properties jndiProps = new Properties (); 341 jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.LocalOnlyContextFactory"); 342 jndiProps.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"); 343 InitialContextFactory.setProperties(jndiProps); 344 } 345 346 351 public static void boot(String configPath) 352 { 353 initInitialContext(); 354 355 String basePath = ""; 356 if (configPath != null && !configPath.equals("")) 357 { 358 basePath = configPath; 359 if (!configPath.endsWith("/")) 360 { 361 basePath += "/"; 362 } 363 } 364 365 try 366 { 367 URL bootstrap = Thread.currentThread().getContextClassLoader().getResource(basePath + "embedded-jboss-beans.xml"); 368 createKernel(); 369 loadMBeanServer(); 370 BeanXMLDeployer deployer = new BeanXMLDeployer(kernel); 371 if (bootstrap == null) 372 throw new RuntimeException ("Cannot find embedded-jboss-beans.xml"); 373 deployments.add(0, deployer.deploy(bootstrap)); 374 deployer.validate(); 375 376 URL ejb3_interceptors = Thread.currentThread().getContextClassLoader().getResource(basePath + "ejb3-interceptors-aop.xml"); 377 if (ejb3_interceptors == null) 378 throw new RuntimeException ("Cannot find ejb3-interceptors-aop.xml"); 379 AspectXmlLoader.deployXML(ejb3_interceptors); 380 aopDeployments.add(ejb3_interceptors); 381 382 getDeployment(""); 383 } 384 catch (Throwable throwable) 385 { 386 throw new RuntimeException (throwable); 387 } 388 } 389 390 public static void loadMBeanServer() throws Exception 391 { 392 ControllerContext context = kernel.getController().getInstalledContext("MBeanServer"); 393 394 if (context != null) 395 { 396 mbeanServer = (MBeanServer ) context.getTarget(); 397 } 398 else 399 { 400 ArrayList servers = MBeanServerFactory.findMBeanServer(null); 401 if (servers.size() == 0) 402 { 403 mbeanServer = MBeanServerFactory.createMBeanServer(); 404 org.jboss.mx.util.MBeanServerLocator.setJBoss(mbeanServer); 405 } 406 } 407 } 408 409 410 public static void createKernel() 411 throws Exception 412 { 413 BasicBootstrap bootstrap1 = new BasicBootstrap(); 414 bootstrap1.run(); 415 kernel = bootstrap1.getKernel(); 416 KernelAbstractionFactory.setKernel(kernel); 417 } 418 } 419 | Popular Tags |