FileDocCategorySizeDatePackage
EJB3StandaloneBootstrap.javaAPI DocJBoss 4.2.113521Fri Jul 13 20:53:50 BST 2007org.jboss.ejb3.embedded

EJB3StandaloneBootstrap

public class EJB3StandaloneBootstrap extends Object
This class is used to bootstrap the ejb3 container. It find things by ClassLoader.getResource and will load by default the embedded-jbosss-beans.xml file and the ejb3-interceptors-aop.xml file.

This is usually only used in standalone Java programs or Junit tests.

author
Bill Burke
version
$Revision: 60233 $

Fields Summary
private static final Logger
log
private static final ObjectName
DEFAULT_LOADER_NAME
public static org.jboss.kernel.Kernel
kernel
private static MBeanServer
mbeanServer
private static EJB3StandaloneDeployer
deployer
private static boolean
warned
Whether we have warned about the initial context
private static ArrayList
deployments
private static ArrayList
aopDeployments
public static HashSet
ignoredJars
Constructors Summary
Methods Summary
public static voidboot(java.lang.String configPath)
Bootstrap the EJB3 container by loading up JBoss Microcontainer files.

param
configPath base path for default resources. null or "" should be good enough.

      initInitialContext();
      
      String basePath = "";
      if (configPath != null && !configPath.equals(""))
      {
         basePath = configPath;
         if (!configPath.endsWith("/"))
         {
            basePath += "/";
         }
      }
      
      try
      {
         URL bootstrap = Thread.currentThread().getContextClassLoader().getResource(basePath + "embedded-jboss-beans.xml");
         createKernel();
         loadMBeanServer();
         BeanXMLDeployer deployer = new BeanXMLDeployer(kernel);
         if (bootstrap == null)
            throw new RuntimeException("Cannot find embedded-jboss-beans.xml");
         deployments.add(0, deployer.deploy(bootstrap));
         deployer.validate();

         URL ejb3_interceptors = Thread.currentThread().getContextClassLoader().getResource(basePath + "ejb3-interceptors-aop.xml");
         if (ejb3_interceptors == null)
            throw new RuntimeException("Cannot find ejb3-interceptors-aop.xml");
         AspectXmlLoader.deployXML(ejb3_interceptors);
         aopDeployments.add(ejb3_interceptors);
         
         getDeployment("");
      }
      catch (Throwable throwable)
      {
         throw new RuntimeException(throwable);
      }
   
public static EJB3StandaloneDeployercreateDeployer()
Create a deployer that you can start deploying to. You must call create/start and stop/destroy to bootstrap and shutdown

return

      EJB3StandaloneDeployer deployer;
      deployer = new EJB3StandaloneDeployer();
      deployer.setJndiProperties(getInitialContextProperties());
      deployer.setKernel(kernel);
      deployer.setMbeanServer(mbeanServer);
      
      return deployer;
   
public static voidcreateKernel()

      BasicBootstrap bootstrap1 = new BasicBootstrap();
      bootstrap1.run();
      kernel = bootstrap1.getKernel();
      KernelAbstractionFactory.setKernel(kernel);
   
public static voiddeployXmlResource(java.lang.String resource)
Deploy a JBoss Microcontainer XML file.

param
resource



               
       
   
      try
      {
         BeanXMLDeployer deployer = new BeanXMLDeployer(kernel);
         URL bootstrap = Thread.currentThread().getContextClassLoader().getResource(resource);
         if (bootstrap == null)
            throw new RuntimeException("Cannot find " + resource);
         KernelDeployment deployment = deployer.deploy(bootstrap);
         deployments.add(0, deployment);
         deployer.validate();
      }
      catch (Throwable throwable)
      {
         throw new RuntimeException(throwable);
      }
   
public static java.lang.ObjectgetDeployment(java.lang.String name)

      Iterator allDeployments = deployments.iterator();
      while (allDeployments.hasNext())
      {
         KernelDeployment deployment = (KernelDeployment)allDeployments.next();
         Iterator contexts = deployment.getInstalledContexts().iterator();
         while (contexts.hasNext())
         {
            org.jboss.kernel.plugins.dependency.AbstractKernelControllerContext context = (org.jboss.kernel.plugins.dependency.AbstractKernelControllerContext)contexts.next();
            if (context.getName().equals(name))
               return context.getTarget();
         }
      }
      
      return null;
   
public static java.util.HashtablegetInitialContextProperties()


   
   
      ignoredJars.add("antlr-2.7.5H3.jar");
      ignoredJars.add("asm-attrs.jar");
      ignoredJars.add("asm.jar");
      ignoredJars.add("cglib-2.1.1.jar");
      ignoredJars.add("commons-collections.jar");
      ignoredJars.add("commons-logging-api.jar");
      ignoredJars.add("commons-logging.jar");
      ignoredJars.add("concurrent.jar");
      ignoredJars.add("dom4j.jar");
      ignoredJars.add("ejb3-persistence.jar");
      ignoredJars.add("hibernate3.jar");
      ignoredJars.add("hibernate-annotations.jar");
      ignoredJars.add("hibernate-entitymanager.jar");
      ignoredJars.add("hsqldb.jar");
      ignoredJars.add("javassist.jar");
      ignoredJars.add("jboss-annotations-ejb3.jar");
      ignoredJars.add("jboss-aop-jdk50.jar");
      ignoredJars.add("jboss-aspect-library-jdk50.jar");
      ignoredJars.add("jboss-common.jar");
      ignoredJars.add("jboss-common-jdbc-wrapper.jar");
      ignoredJars.add("jboss-container.jar");
      ignoredJars.add("jboss-dependency.jar");
      ignoredJars.add("jboss-ejb3.jar");
      ignoredJars.add("jboss-ejb3x.jar");
      ignoredJars.add("jboss-j2ee.jar");
      ignoredJars.add("jboss-j2se.jar");
      ignoredJars.add("jboss.jar");
      ignoredJars.add("jboss-jca.jar");
      ignoredJars.add("jboss-local-jdbc.jar");
      ignoredJars.add("jboss-microcontainer.jar");
      ignoredJars.add("jbossmq.jar");
      ignoredJars.add("jboss-remoting.jar");
      ignoredJars.add("jbosssx.jar");
      ignoredJars.add("jboss-system.jar");
      ignoredJars.add("jboss-transaction.jar");
      ignoredJars.add("jboss-xa-jdbc.jar");
      ignoredJars.add("jnpserver.jar");
      ignoredJars.add("log4j.jar");
      ignoredJars.add("resolver.jar");
      ignoredJars.add("trove.jar");
      ignoredJars.add("xercesImpl.jar");
      ignoredJars.add("xml-apis.jar");
      ignoredJars.add("jboss-ejb3-all.jar");
      ignoredJars.add("hibernate-all.jar");
      ignoredJars.add("thirdparty-all.jar");
   
      Hashtable hash = null;
      ControllerContext context = kernel.getController().getInstalledContext("InitialContextProperties");
      if (context != null)
         hash = (Hashtable) context.getTarget();
      else if (warned == false)
      {
         log.warn("Could not find an configured InitialContextProperties, this is ok if your already have a correct jndi.properties file");
         warned = true;
      }
      return hash;
   
public static org.jboss.kernel.KernelgetKernel()


      
   
      return kernel;
   
private static voidinitInitialContext()

      Properties jndiProps = new Properties();
      jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.LocalOnlyContextFactory");
      jndiProps.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
      InitialContextFactory.setProperties(jndiProps);
   
public static voidloadMBeanServer()

      ControllerContext context = kernel.getController().getInstalledContext("MBeanServer");
       
      if (context != null)
      {
         mbeanServer = (MBeanServer) context.getTarget();
      }
      else
      {
         ArrayList servers = MBeanServerFactory.findMBeanServer(null);
         if (servers.size() == 0)
         {
            mbeanServer = MBeanServerFactory.createMBeanServer();
            org.jboss.mx.util.MBeanServerLocator.setJBoss(mbeanServer);
         }
      }
   
public static voidscanClasspath(java.lang.String paths)
Scan java.class.path System property for jars that contain EJB3 files.

This is unportable and unreliable. Use with caution.

      try
      {
         String classpath = System.getProperty("java.class.path");
         StringTokenizer tokenizer = new StringTokenizer(classpath, File.pathSeparator);
         String[] split = paths.split(",");
         for (int i = 0; i < split.length; i++)
         {
            split[i] = split[i].trim();
         }

         deployer = createDeployer();

         while (tokenizer.hasMoreTokens())
         {
            String path = tokenizer.nextToken().trim();
            boolean found = false;
            for (String wantedPath : split)
            {
               if (path.endsWith(System.getProperty("file.separator") + wantedPath))
               {
                  found = true;
                  break;
               }
            }
            if (!found) continue;
            File fp = new File(path);
            if (ignoredJars.contains(fp.getName())) continue;
            URL archive = fp.toURL();
            deployer.getArchives().add(archive);
         }
         if (deployer.getArchives().size() == 0)
         {
            deployer = null;
            return;
         }
         deployer.create();
         deployer.start();

      }
      catch (Exception e)
      {
         throw new RuntimeException(e);
      }
      if (!KernelErrors.validate(kernel)) throw new RuntimeException("Problems scanning classpath");

   
public static voidscanClasspath()
Scan java.class.path System property for jars that contain EJB3 files.

This is unportable and unreliable. Use with caution.

      try
      {
         String classpath = System.getProperty("java.class.path");
         StringTokenizer tokenizer = new StringTokenizer(classpath, File.pathSeparator);

         deployer = createDeployer();

         while (tokenizer.hasMoreTokens())
         {
            String path = tokenizer.nextToken();
            File fp = new File(path);
            if (ignoredJars.contains(fp.getName())) continue;
            URL archive = fp.toURL();
            deployer.getArchives().add(archive);
         }
         if (deployer.getArchives().size() == 0)
         {
            deployer = null;
            return;
         }
         deployer.create();
         deployer.start();

      }
      catch (Exception e)
      {
         throw new RuntimeException(e);
      }
      if (!KernelErrors.validate(kernel)) throw new RuntimeException("Problems scanning classpath");

   
public static voidshutdown()

      try
      {
         if (deployer != null)
         {
            deployer.stop();
            deployer.destroy();
            deployer = null;
         }
         BeanXMLDeployer deployer = new BeanXMLDeployer(kernel);
         for (KernelDeployment deployment : deployments)
         {
            deployer.undeploy(deployment);
         }

         for (URL url : aopDeployments)
         {
            try
            {
               AspectXmlLoader.undeployXML(url);
            }
            catch (Exception e)
            {
               log.warn(e);
            }
         }
         kernel = null;
      }
      catch (Throwable throwable)
      {
         log.warn(throwable);
      }