1 22 package org.jboss.ejb3.embedded; 23 24 import java.net.URL ; 25 import java.util.ArrayList ; 26 import java.util.HashSet ; 27 import java.util.Hashtable ; 28 import java.util.List ; 29 import java.util.Set ; 30 import java.util.StringTokenizer ; 31 import javax.servlet.ServletContext ; 32 import javax.servlet.ServletContextEvent ; 33 import javax.servlet.ServletContextListener ; 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 48 public class ServletBootstrapListener implements ServletContextListener 49 { 50 private static final Logger log = Logger.getLogger(ServletBootstrapListener.class); 51 52 protected Kernel kernel; 53 54 protected List <KernelDeployment> deployments = new ArrayList <KernelDeployment>(); 55 protected List <URL > aopDeployments = new ArrayList <URL >(); 56 57 58 protected void createKernel() 59 throws Exception 60 { 61 BasicBootstrap bootstrap1 = new BasicBootstrap(); 62 bootstrap1.run(); 63 kernel = bootstrap1.getKernel(); 64 } 65 66 protected void deployXmlResource(String resource) 67 { 68 try 69 { 70 BeanXMLDeployer deployer = new BeanXMLDeployer(kernel); 71 URL bootstrap = Thread.currentThread().getContextClassLoader().getResource(resource); 72 if (bootstrap == null) 73 throw new RuntimeException ("Cannot find " + resource); 74 KernelDeployment deployment = deployer.deploy(bootstrap); 75 deployer.validate(); 76 deployments.add(0, deployment); 77 } 78 catch (Throwable throwable) 79 { 80 throwable.printStackTrace(); 81 throw new RuntimeException (throwable); 82 } 83 84 } 85 86 public static HashSet ignoredJars = new HashSet (); 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 servletContextEvent) 138 { 139 try 140 { 141 createKernel(); 142 143 ServletContext servletContext = servletContextEvent.getServletContext(); 144 String aop = (String ) servletContext.getInitParameter("jboss-aop-deployments"); 145 146 if (aop == null) aop = "ejb3-interceptors-aop.xml"; 147 148 StringTokenizer tokenizer = new StringTokenizer (aop, ","); 149 while (tokenizer.hasMoreTokens()) 150 { 151 String token = tokenizer.nextToken().trim(); 152 log.debug("deploying aop xml: " + token); 153 URL url = Thread.currentThread().getContextClassLoader().getResource(token); 154 try 155 { 156 AspectXmlLoader.deployXML(url); 157 aopDeployments.add(0, url); 158 } 159 catch (Exception e) 160 { 161 throw new RuntimeException (e); 162 } 163 } 164 165 String deployments = (String ) servletContext.getInitParameter("jboss-kernel-deployments"); 166 167 if (deployments == null) deployments = "embedded-jboss-beans.xml"; 168 169 tokenizer = new StringTokenizer (deployments, ","); 170 while (tokenizer.hasMoreTokens()) 171 { 172 String token = tokenizer.nextToken().trim(); 173 log.debug("deploying kernel xml: " + token); 174 deployXmlResource(token); 175 } 176 177 String scan = (String ) servletContext.getInitParameter("automatic-scan"); 178 if (scan != null && !scan.equals("false")) return; 179 180 Set libJars = servletContext.getResourcePaths("/WEB-INF/lib"); 181 deployer = new EJB3StandaloneDeployer(); 182 deployer.setJndiProperties(getInitialContextProperties()); 183 deployer.setKernel(kernel); 184 for (Object jar : libJars) 185 { 186 if (ignoredJars.contains(jar)) continue; 187 URL archive = servletContext.getResource((String )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 e) 199 { 200 log.error(e); 201 throw new RuntimeException (e); 202 } 203 204 if (!KernelErrors.validate(kernel)) throw new RuntimeException ("Problems scanning ServletContext.getResourcePaths"); 205 } 206 207 protected Hashtable getInitialContextProperties() 208 { 209 Hashtable hash = null; 210 ControllerContext context = kernel.getController().getInstalledContext("InitialContextProperties"); 211 if (context != null) 212 hash = (Hashtable ) 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 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 url : aopDeployments) 235 { 236 AspectXmlLoader.undeployXML(url); 237 } 238 } 239 catch (Throwable throwable) 240 { 241 throwable.printStackTrace(); 242 throw new RuntimeException (throwable); 243 } 244 } 245 } 246 | Popular Tags |