1 25 26 package org.objectweb.easybeans.server.war; 27 import static org.objectweb.easybeans.util.url.URLUtils.fileToURL2; 28 29 import java.io.BufferedReader ; 30 import java.io.File ; 31 import java.io.IOException ; 32 import java.io.InputStreamReader ; 33 import java.io.Reader ; 34 import java.lang.reflect.InvocationTargetException ; 35 import java.lang.reflect.Method ; 36 import java.net.URL ; 37 import java.net.URLClassLoader ; 38 import java.net.URLConnection ; 39 import java.util.ArrayList ; 40 import java.util.List ; 41 42 import javax.servlet.ServletContext ; 43 import javax.servlet.ServletContextEvent ; 44 import javax.servlet.ServletContextListener ; 45 46 import org.objectweb.easybeans.log.JLog; 47 import org.objectweb.easybeans.log.JLogFactory; 48 import org.objectweb.easybeans.server.EasyBeans; 49 import org.objectweb.easybeans.server.Embedded; 50 import org.objectweb.easybeans.server.EmbeddedConfigurator; 51 import org.objectweb.easybeans.server.EmbeddedException; 52 import org.objectweb.easybeans.server.ServerConfig; 53 import org.objectweb.easybeans.util.url.URLUtilsException; 54 55 60 public class EasyBeansContextListener implements ServletContextListener { 61 62 65 private static final String COMMON_LIBRARIES_LIST = "easybeans-exported-libraries.lst"; 66 67 70 public static final String DEFAULT_XML_FILE = "org/objectweb/easybeans/server/war/easybeans-default.xml"; 71 72 75 private static JLog logger = JLogFactory.getLog(EasyBeansContextListener.class); 76 77 80 private Embedded embedded = null; 81 82 85 private enum AvailableWebContainer { 86 89 TOMCAT, JETTY, UNKNOWN 90 } 91 92 95 private AvailableWebContainer containerType; 96 97 104 public void contextInitialized(final ServletContextEvent servletContextEvent) { 105 106 if (embedded == null) { 108 109 URL xmlConfigurationURL = Thread.currentThread().getContextClassLoader().getResource(EasyBeans.USER_XML_FILE); 111 112 if (xmlConfigurationURL == null) { 113 logger.warn("No {0} resource found in classpath, use default settings", EasyBeans.USER_XML_FILE); 114 xmlConfigurationURL = Thread.currentThread().getContextClassLoader().getResource(DEFAULT_XML_FILE); 115 } 116 try { 117 embedded = EmbeddedConfigurator.create(xmlConfigurationURL); 118 } catch (EmbeddedException e) { 119 throw new IllegalStateException ("Cannot create the embedded server", e); 120 } 121 122 String ejb3ParentDir = detectWebContainerDirectory(); 124 logger.info("Detecting '" + containerType + "' web container"); 125 126 File workDir = new File (ejb3ParentDir + File.separator + "ejb3s"); 128 workDir.mkdir(); 129 130 ServerConfig serverConfig = getServerConfig(); 131 serverConfig.setShouldWait(false); 132 serverConfig.setUseMBeans(true); 133 serverConfig.setUseNaming(false); 134 logger.info("Set ejb3 path to ''{0}''", workDir); 135 serverConfig.setEjb3Path(workDir.getAbsolutePath()); 136 137 139 registerLibraries(servletContextEvent); 140 try { 141 embedded.start(); 142 } catch (EmbeddedException e) { 143 throw new IllegalStateException ("Cannot start embedded EasyBeans server", e); 144 } 145 } 146 } 147 148 155 public void contextDestroyed(final ServletContextEvent servletContextEvent) { 156 if (embedded != null) { 157 logger.info("Stopping EasyBeans embedded server..."); 158 try { 159 embedded.stop(); 160 } catch (EmbeddedException e) { 161 throw new IllegalStateException ("Cannot stop the embedded server", e); 162 } catch (Throwable e) { 163 logger.error("Unexpected error when stopping the embedded server", e); 164 throw new IllegalStateException ("Unexpected error when stopping the embedded server", e); 165 } finally { 166 embedded = null; 167 } 168 } 169 } 170 171 175 private String detectWebContainerDirectory() { 176 String catalinaBase = System.getProperty("catalina.base"); 178 if (catalinaBase != null) { 179 containerType = AvailableWebContainer.TOMCAT; 180 return catalinaBase; 181 } 182 183 String jettyHome = System.getProperty("jetty.home"); 185 if (jettyHome != null) { 186 containerType = AvailableWebContainer.JETTY; 187 return jettyHome; 188 } 189 190 containerType = AvailableWebContainer.UNKNOWN; 192 return System.getProperty("java.io.tmpdir"); 193 } 194 195 201 private void registerLibraries(final ServletContextEvent servletContextEvent) { 202 if (containerType.equals(AvailableWebContainer.TOMCAT)) { 203 registerTomcat(servletContextEvent); 204 } 205 206 } 207 208 213 private void registerTomcat(final ServletContextEvent servletContextEvent) { 214 215 ClassLoader currentCL = Thread.currentThread().getContextClassLoader(); 217 218 ClassLoader cl = currentCL.getParent().getParent(); 221 if (!"org.apache.catalina.loader.StandardClassLoader".equals(cl.getClass().getName())) { 223 throw new IllegalStateException ("Didn't find common classloader"); 224 } 225 226 URLClassLoader urlCl = (URLClassLoader ) cl; 228 Method addURL = null; 230 try { 231 addURL = URLClassLoader .class.getDeclaredMethod("addURL", new Class [] {URL .class}); 232 } catch (SecurityException e) { 233 throw new IllegalArgumentException ("Cannot get addURL method on the class '" + URLClassLoader .class + "'.", e); 234 } catch (NoSuchMethodException e) { 235 throw new IllegalArgumentException ("Cannot get addURL method on the class '" + URLClassLoader .class + "'.", e); 236 } 237 addURL.setAccessible(true); 238 239 ServletContext sContext = servletContextEvent.getServletContext(); 242 List <String > libraries = new ArrayList <String >(); 243 getLibraries(sContext, libraries); 244 245 for (String library : libraries) { 246 URL url = null; 247 try { 248 url = fileToURL2(new File (library)); 249 } catch (URLUtilsException e) { 250 throw new IllegalArgumentException ("Cannot get URL on file '" + library + "'.", e); 251 } 252 try { 253 logger.debug("Registering '" + url + "'."); 254 addURL.invoke(urlCl, url); 255 } catch (IllegalArgumentException e) { 256 throw new IllegalStateException ("Cannot register the URL '" + url + "'.", e); 257 } catch (IllegalAccessException e) { 258 throw new IllegalStateException ("Cannot register the URL '" + url + "'.", e); 259 } catch (InvocationTargetException e) { 260 throw new IllegalStateException ("Cannot register the URL '" + url + "'.", e); 261 } 262 } 263 addURL.setAccessible(false); 264 265 } 266 267 272 protected void getLibraries(final ServletContext sContext, final List <String > libraries) { 273 276 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 277 278 URL url = classLoader.getResource(COMMON_LIBRARIES_LIST); 279 if (url == null) { 280 throw new IllegalStateException ("No resource named '" + COMMON_LIBRARIES_LIST 281 + "' was found in the current classloader."); 282 } 283 284 URLConnection urlConnection = null; 286 try { 287 urlConnection = url.openConnection(); 288 } catch (IOException e) { 289 throw new IllegalStateException ("Cannot open connection on the URL '" + url + "'.", e); 290 } 291 urlConnection.setDefaultUseCaches(false); 292 293 Reader reader = null; 294 try { 295 try { 296 reader = new InputStreamReader (urlConnection.getInputStream()); 297 } catch (IOException e) { 298 throw new IllegalStateException ("Cannot get inputstream on the URL '" + url + "'.", e); 299 } 300 BufferedReader bufferedReader = new BufferedReader (reader); 301 String fileName = null; 302 try { 303 while ((fileName = bufferedReader.readLine()) != null) { 304 fileName = fileName.trim(); 305 if (fileName.length() >= 0) { 307 libraries.add(sContext.getRealPath("/WEB-INF/lib/" + fileName.trim())); 309 } 310 } 311 } catch (IOException e) { 312 throw new IllegalStateException ("Cannot read the inputstream on the URL '" + url + "'.", e); 313 } finally { 314 if (bufferedReader != null) { 315 try { 316 bufferedReader.close(); 317 } catch (IOException e) { 318 throw new IllegalStateException ("Cannot close reader object on the URL '" + url + "'.", e); 319 } 320 } 321 } 322 } finally { 323 if (reader != null) { 324 try { 325 reader.close(); 326 } catch (IOException e) { 327 throw new IllegalStateException ("Cannot close reader object on the URL '" + url + "'.", e); 328 } 329 } 330 } 331 } 332 333 336 protected ServerConfig getServerConfig() { 337 return embedded.getServerConfig(); 338 } 339 } 340 | Popular Tags |