KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > embedded > ServletBootstrapListener


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.embedded;
23
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31 import javax.servlet.ServletContext JavaDoc;
32 import javax.servlet.ServletContextEvent JavaDoc;
33 import javax.servlet.ServletContextListener JavaDoc;
34 import org.jboss.aop.AspectXmlLoader;
35 import org.jboss.dependency.spi.ControllerContext;
36 import org.jboss.kernel.Kernel;
37 import org.jboss.kernel.plugins.bootstrap.basic.BasicBootstrap;
38 import org.jboss.kernel.plugins.deployment.xml.BeanXMLDeployer;
39 import org.jboss.kernel.spi.deployment.KernelDeployment;
40 import org.jboss.logging.Logger;
41
42 /**
43  * Comment
44  *
45  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
46  * @version $Revision: 44597 $
47  */

48 public class ServletBootstrapListener implements ServletContextListener JavaDoc
49 {
50    private static final Logger log = Logger.getLogger(ServletBootstrapListener.class);
51
52    protected Kernel kernel;
53
54    protected List JavaDoc<KernelDeployment> deployments = new ArrayList JavaDoc<KernelDeployment>();
55    protected List JavaDoc<URL JavaDoc> aopDeployments = new ArrayList JavaDoc<URL JavaDoc>();
56
57
58    protected void createKernel()
59            throws Exception JavaDoc
60    {
61       BasicBootstrap bootstrap1 = new BasicBootstrap();
62       bootstrap1.run();
63       kernel = bootstrap1.getKernel();
64    }
65
66    protected void deployXmlResource(String JavaDoc resource)
67    {
68       try
69       {
70          BeanXMLDeployer deployer = new BeanXMLDeployer(kernel);
71          URL JavaDoc bootstrap = Thread.currentThread().getContextClassLoader().getResource(resource);
72          if (bootstrap == null)
73             throw new RuntimeException JavaDoc("Cannot find " + resource);
74          KernelDeployment deployment = deployer.deploy(bootstrap);
75          deployer.validate();
76          deployments.add(0, deployment);
77       }
78       catch (Throwable JavaDoc throwable)
79       {
80          throwable.printStackTrace();
81          throw new RuntimeException JavaDoc(throwable);
82       }
83
84    }
85
86    public static HashSet JavaDoc ignoredJars = new HashSet JavaDoc();
87
88    static
89    {
90       ignoredJars.add("/WEB-INF/lib/antlr-2.7.5H3.jar");
91       ignoredJars.add("/WEB-INF/lib/asm-attrs.jar");
92       ignoredJars.add("/WEB-INF/lib/asm.jar");
93       ignoredJars.add("/WEB-INF/lib/cglib-2.1.1.jar");
94       ignoredJars.add("/WEB-INF/lib/commons-collections.jar");
95       ignoredJars.add("/WEB-INF/lib/commons-logging-api.jar");
96       ignoredJars.add("/WEB-INF/lib/commons-logging.jar");
97       ignoredJars.add("/WEB-INF/lib/concurrent.jar");
98       ignoredJars.add("/WEB-INF/lib/dom4j.jar");
99       ignoredJars.add("/WEB-INF/lib/ejb3-persistence.jar");
100       ignoredJars.add("/WEB-INF/lib/hibernate3.jar");
101       ignoredJars.add("/WEB-INF/lib/hibernate-annotations.jar");
102       ignoredJars.add("/WEB-INF/lib/hibernate-entitymanager.jar");
103       ignoredJars.add("/WEB-INF/lib/hsqldb.jar");
104       ignoredJars.add("/WEB-INF/lib/javassist.jar");
105       ignoredJars.add("/WEB-INF/lib/jboss-annotations-ejb3.jar");
106       ignoredJars.add("/WEB-INF/lib/jboss-aop-jdk50.jar");
107       ignoredJars.add("/WEB-INF/lib/jboss-aspect-library-jdk50.jar");
108       ignoredJars.add("/WEB-INF/lib/jboss-common.jar");
109       ignoredJars.add("/WEB-INF/lib/jboss-common-jdbc-wrapper.jar");
110       ignoredJars.add("/WEB-INF/lib/jboss-container.jar");
111       ignoredJars.add("/WEB-INF/lib/jboss-dependency.jar");
112       ignoredJars.add("/WEB-INF/lib/jboss-ejb3.jar");
113       ignoredJars.add("/WEB-INF/lib/jboss-ejb3x.jar");
114       ignoredJars.add("/WEB-INF/lib/jboss-j2ee.jar");
115       ignoredJars.add("/WEB-INF/lib/jboss-j2se.jar");
116       ignoredJars.add("/WEB-INF/lib/jboss.jar");
117       ignoredJars.add("/WEB-INF/lib/jboss-jca.jar");
118       ignoredJars.add("/WEB-INF/lib/jboss-local-jdbc.jar");
119       ignoredJars.add("/WEB-INF/lib/jboss-microcontainer.jar");
120       ignoredJars.add("/WEB-INF/lib/jbossmq.jar");
121       ignoredJars.add("/WEB-INF/lib/jboss-remoting.jar");
122       ignoredJars.add("/WEB-INF/lib/jbosssx.jar");
123       ignoredJars.add("/WEB-INF/lib/jboss-system.jar");
124       ignoredJars.add("/WEB-INF/lib/jboss-transaction.jar");
125       ignoredJars.add("/WEB-INF/lib/jboss-xa-jdbc.jar");
126       ignoredJars.add("/WEB-INF/lib/jnpserver.jar");
127       ignoredJars.add("/WEB-INF/lib/log4j.jar");
128       ignoredJars.add("/WEB-INF/lib/resolver.jar");
129       ignoredJars.add("/WEB-INF/lib/trove.jar");
130       ignoredJars.add("/WEB-INF/lib/xercesImpl.jar");
131       ignoredJars.add("/WEB-INF/lib/xml-apis.jar");
132    }
133
134    private EJB3StandaloneDeployer deployer;
135
136
137    public void contextInitialized(ServletContextEvent JavaDoc servletContextEvent)
138    {
139       try
140       {
141          createKernel();
142
143          ServletContext JavaDoc servletContext = servletContextEvent.getServletContext();
144          String JavaDoc aop = (String JavaDoc) servletContext.getInitParameter("jboss-aop-deployments");
145
146          if (aop == null) aop = "ejb3-interceptors-aop.xml";
147
148          StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(aop, ",");
149          while (tokenizer.hasMoreTokens())
150          {
151             String JavaDoc token = tokenizer.nextToken().trim();
152             log.debug("deploying aop xml: " + token);
153             URL JavaDoc url = Thread.currentThread().getContextClassLoader().getResource(token);
154             try
155             {
156                AspectXmlLoader.deployXML(url);
157                aopDeployments.add(0, url);
158             }
159             catch (Exception JavaDoc e)
160             {
161                throw new RuntimeException JavaDoc(e);
162             }
163          }
164
165          String JavaDoc deployments = (String JavaDoc) servletContext.getInitParameter("jboss-kernel-deployments");
166
167          if (deployments == null) deployments = "embedded-jboss-beans.xml";
168
169          tokenizer = new StringTokenizer JavaDoc(deployments, ",");
170          while (tokenizer.hasMoreTokens())
171          {
172             String JavaDoc token = tokenizer.nextToken().trim();
173             log.debug("deploying kernel xml: " + token);
174             deployXmlResource(token);
175          }
176
177          String JavaDoc scan = (String JavaDoc) servletContext.getInitParameter("automatic-scan");
178          if (scan != null && !scan.equals("false")) return;
179
180          Set JavaDoc libJars = servletContext.getResourcePaths("/WEB-INF/lib");
181          deployer = new EJB3StandaloneDeployer();
182          deployer.setJndiProperties(getInitialContextProperties());
183          deployer.setKernel(kernel);
184          for (Object JavaDoc jar : libJars)
185          {
186             if (ignoredJars.contains(jar)) continue;
187             URL JavaDoc archive = servletContext.getResource((String JavaDoc)jar);
188             deployer.getArchives().add(archive);
189          }
190          if (deployer.getArchives().size() == 0)
191          {
192             deployer = null;
193             return;
194          }
195          deployer.create();
196          deployer.start();
197       }
198       catch (Exception JavaDoc e)
199       {
200          log.error(e);
201          throw new RuntimeException JavaDoc(e);
202       }
203
204       if (!KernelErrors.validate(kernel)) throw new RuntimeException JavaDoc("Problems scanning ServletContext.getResourcePaths");
205    }
206
207    protected Hashtable JavaDoc getInitialContextProperties()
208    {
209       Hashtable JavaDoc hash = null;
210       ControllerContext context = kernel.getController().getInstalledContext("InitialContextProperties");
211       if (context != null)
212          hash = (Hashtable JavaDoc) context.getTarget();
213       else
214          log.warn("could not find an configured InitialContextProperties, this is ok if your already have a correct jndi.properties file");
215       return hash;
216    }
217
218
219    public void contextDestroyed(ServletContextEvent JavaDoc servletContextEvent)
220    {
221       try
222       {
223          if (deployer != null)
224          {
225             deployer.stop();
226             deployer.destroy();
227          }
228          BeanXMLDeployer deployer = new BeanXMLDeployer(kernel);
229          for (KernelDeployment deployment : deployments)
230          {
231             deployer.undeploy(deployment);
232          }
233
234          for (URL JavaDoc url : aopDeployments)
235          {
236             AspectXmlLoader.undeployXML(url);
237          }
238       }
239       catch (Throwable JavaDoc throwable)
240       {
241          throwable.printStackTrace();
242          throw new RuntimeException JavaDoc(throwable);
243       }
244    }
245 }
246
Popular Tags