1 45 package org.openejb.loader; 46 47 import java.io.File ; 48 import java.io.IOException ; 49 import java.util.Enumeration ; 50 import java.util.Properties ; 51 import java.lang.reflect.Method ; 52 import java.lang.reflect.InvocationTargetException ; 53 54 import javax.servlet.ServletConfig ; 55 import javax.servlet.ServletContext ; 56 import javax.servlet.ServletException ; 57 import javax.servlet.http.HttpServlet ; 58 import javax.servlet.http.HttpServletRequest ; 59 import javax.servlet.http.HttpServletResponse ; 60 61 64 public class LoaderServlet extends HttpServlet { 65 66 private Loader loader; 67 68 public void init(ServletConfig config) throws ServletException { 69 if (loader != null) { 70 return; 71 } 72 73 Properties p = initParamsToProperties(config); 76 77 String embeddingStyle = p.getProperty("openejb.loader"); 78 79 if (embeddingStyle.endsWith("tomcat-webapp")) { 81 setPropertyIfNUll(p, "openejb.base", getWebappPath(config)); 82 } 86 87 try { 88 SystemInstance.init(p); 89 Embedder embedder = new Embedder("org.openejb.tomcat.TomcatLoader"); 90 Class loaderClass = embedder.load(); 91 Object instance = loaderClass.newInstance(); 92 try { 93 loader = (Loader) instance; 94 } catch (ClassCastException e) { 95 loader = new LoaderWrapper(instance); 96 } 97 98 loader.init(config); 99 } catch (Exception e) { 100 e.printStackTrace(); 101 } 102 } 103 104 105 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 106 loader.service(request, response); 107 } 108 109 private String getWebappPath(ServletConfig config) { 110 ServletContext ctx = config.getServletContext(); 111 File webInf = new File (ctx.getRealPath("WEB-INF")); 112 File webapp = webInf.getParentFile(); 113 String webappPath = webapp.getAbsolutePath(); 114 return webappPath; 115 } 116 117 private Properties initParamsToProperties(ServletConfig config) { 118 Properties p = new Properties (); 119 120 p.setProperty("openejb.loader","tomcat"); 122 123 Enumeration enumeration = config.getInitParameterNames(); 125 System.out.println("OpenEJB init-params:"); 126 while (enumeration.hasMoreElements()) { 127 String name = (String ) enumeration.nextElement(); 128 String value = config.getInitParameter(name); 129 p.put(name, value); 130 System.out.println("\tparam-name: " + name + ", param-value: " + value); 131 } 132 133 return p; 134 } 135 136 private Object setPropertyIfNUll(Properties properties, String key, String value){ 137 String currentValue = properties.getProperty(key); 138 if (currentValue == null){ 139 properties.setProperty(key, value); 140 } 141 return currentValue; 142 } 143 144 153 public static class LoaderWrapper implements Loader { 154 private final Object loader; 155 private final Method init; 156 private final Method service; 157 158 public LoaderWrapper(Object loader) { 159 this.loader = loader; 160 try { 161 Class loaderClass = loader.getClass(); 162 this.init = loaderClass.getMethod("init", new Class []{ServletConfig .class}); 163 this.service = loaderClass.getMethod("service", new Class []{HttpServletRequest .class, HttpServletResponse .class}); 164 } catch (NoSuchMethodException e) { 165 throw (IllegalStateException ) new IllegalStateException ("Signatures for Loader are no longer correct.").initCause(e); 166 } 167 } 168 169 public void init(ServletConfig servletConfig) throws ServletException { 170 try { 171 init.invoke(loader, new Object []{servletConfig}); 172 } catch (InvocationTargetException e) { 173 Throwable cause = e.getCause(); 174 if (cause instanceof RuntimeException ) { 175 throw (RuntimeException ) cause; 176 } else if (cause instanceof Error ) { 177 throw (Error ) cause; 178 } else { 179 throw (ServletException ) cause; 180 } 181 } catch (Exception e) { 182 throw new RuntimeException ("Loader.init: "+e.getMessage()+e.getClass().getName()+": "+e.getMessage(), e); 183 } 184 } 185 186 public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 187 try { 188 service.invoke(loader, new Object []{request, response}); 189 } catch (InvocationTargetException e) { 190 Throwable cause = e.getCause(); 191 if (cause instanceof RuntimeException ) { 192 throw (RuntimeException ) cause; 193 } else if (cause instanceof Error ) { 194 throw (Error ) cause; 195 } else if (cause instanceof IOException ) { 196 throw (IOException ) cause; 197 } else { 198 throw (ServletException ) cause; 199 } 200 } catch (Exception e) { 201 throw new RuntimeException ("Loader.service: "+e.getMessage()+e.getClass().getName()+": "+e.getMessage(), e); 202 } 203 } 204 } 205 } 206 | Popular Tags |