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

ServletBootstrapListener

public class ServletBootstrapListener extends Object implements ServletContextListener
Comment
author
Bill Burke
version
$Revision: 60233 $

Fields Summary
private static final Logger
log
protected org.jboss.kernel.Kernel
kernel
protected List
deployments
protected List
aopDeployments
public static HashSet
ignoredJars
private EJB3StandaloneDeployer
deployer
Constructors Summary
Methods Summary
public voidcontextDestroyed(javax.servlet.ServletContextEvent servletContextEvent)

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

         for (URL url : aopDeployments)
         {
            AspectXmlLoader.undeployXML(url);
         }
      }
      catch (Throwable throwable)
      {
         throwable.printStackTrace();
         throw new RuntimeException(throwable);
      }
   
public voidcontextInitialized(javax.servlet.ServletContextEvent servletContextEvent)

      try
      {
         createKernel();

         ServletContext servletContext = servletContextEvent.getServletContext();
         String aop = (String) servletContext.getInitParameter("jboss-aop-deployments");

         if (aop == null) aop = "ejb3-interceptors-aop.xml";

         StringTokenizer tokenizer = new StringTokenizer(aop, ",");
         while (tokenizer.hasMoreTokens())
         {
            String token = tokenizer.nextToken().trim();
            log.debug("deploying aop xml: " + token);
            URL url = Thread.currentThread().getContextClassLoader().getResource(token);
            try
            {
               AspectXmlLoader.deployXML(url);
               aopDeployments.add(0, url);
            }
            catch (Exception e)
            {
               throw new RuntimeException(e);
            }
         }

         String deployments = (String) servletContext.getInitParameter("jboss-kernel-deployments");

         if (deployments == null) deployments = "embedded-jboss-beans.xml";

         tokenizer = new StringTokenizer(deployments, ",");
         while (tokenizer.hasMoreTokens())
         {
            String token = tokenizer.nextToken().trim();
            log.debug("deploying kernel xml: " + token);
            deployXmlResource(token);
         }

         String scan = (String) servletContext.getInitParameter("automatic-scan");
         if (scan != null && !scan.equals("false")) return;

         Set libJars = servletContext.getResourcePaths("/WEB-INF/lib");
         deployer = new EJB3StandaloneDeployer();
         deployer.setJndiProperties(getInitialContextProperties());
         deployer.setKernel(kernel);
         for (Object jar : libJars)
         {
            if (ignoredJars.contains(jar)) continue;
            URL archive = servletContext.getResource((String)jar);
            deployer.getArchives().add(archive);
         }
         if (deployer.getArchives().size() == 0)
         {
            deployer = null;
            return;
         }
         deployer.create();
         deployer.start();
      }
      catch (Exception e)
      {
         log.error(e);
         throw new RuntimeException(e);
      }

      if (!KernelErrors.validate(kernel)) throw new RuntimeException("Problems scanning ServletContext.getResourcePaths");
   
protected voidcreateKernel()



     
            
   
      BasicBootstrap bootstrap1 = new BasicBootstrap();
      bootstrap1.run();
      kernel = bootstrap1.getKernel();
   
protected voiddeployXmlResource(java.lang.String 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);
         deployer.validate();
         deployments.add(0, deployment);
      }
      catch (Throwable throwable)
      {
         throwable.printStackTrace();
         throw new RuntimeException(throwable);
      }

   
protected java.util.HashtablegetInitialContextProperties()

      Hashtable hash = null;
      ControllerContext context = kernel.getController().getInstalledContext("InitialContextProperties");
      if (context != null)
         hash = (Hashtable) context.getTarget();
      else
         log.warn("could not find an configured InitialContextProperties, this is ok if your already have a correct jndi.properties file");
      return hash;