1 15 package org.apache.tapestry; 16 17 import java.io.IOException ; 18 import java.io.InputStream ; 19 import java.util.Locale ; 20 21 import javax.servlet.ServletConfig ; 22 import javax.servlet.ServletContext ; 23 import javax.servlet.ServletException ; 24 import javax.servlet.http.Cookie ; 25 import javax.servlet.http.HttpServlet ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 import javax.servlet.http.HttpSession ; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.apache.hivemind.ClassResolver; 33 import org.apache.hivemind.ErrorHandler; 34 import org.apache.hivemind.Registry; 35 import org.apache.hivemind.Resource; 36 import org.apache.hivemind.impl.DefaultClassResolver; 37 import org.apache.hivemind.impl.RegistryBuilder; 38 import org.apache.hivemind.impl.StrictErrorHandler; 39 import org.apache.hivemind.impl.XmlModuleDescriptorProvider; 40 import org.apache.hivemind.util.ContextResource; 41 import org.apache.tapestry.services.ApplicationInitializer; 42 import org.apache.tapestry.services.ServletRequestServicer; 43 import org.apache.tapestry.spec.ApplicationSpecification; 44 import org.apache.tapestry.util.exception.ExceptionAnalyzer; 45 46 import com.sun.jndi.ldap.pool.Pool; 47 48 77 78 public class ApplicationServlet extends HttpServlet 79 { 80 87 88 private static final String REGISTRY_KEY_PREFIX = "org.apache.tapestry.Registry:"; 89 90 private static final Log LOG = LogFactory.getLog(ApplicationServlet.class); 91 92 97 98 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException , 99 ServletException 100 { 101 doService(request, response); 102 } 103 104 107 108 private ClassResolver _resolver; 109 110 115 116 private String _registryKey; 117 118 121 122 private Registry _registry; 123 124 127 private ServletRequestServicer _requestServicer; 128 129 137 138 protected void doService(HttpServletRequest request, HttpServletResponse response) 139 throws IOException , ServletException 140 { 141 try 142 { 143 _registry.setupThread(); 144 145 _requestServicer.service(request, response); 146 } 147 catch (ServletException ex) 148 { 149 log("ServletException", ex); 150 151 show(ex); 152 153 155 throw ex; 156 } 157 catch (IOException ex) 158 { 159 log("IOException", ex); 160 161 show(ex); 162 163 165 throw ex; 166 } 167 finally 168 { 169 _registry.cleanupThread(); 170 } 171 } 172 173 protected void show(Exception ex) 174 { 175 System.err.println("\n\n**********************************************************\n\n"); 176 177 new ExceptionAnalyzer().reportException(ex, System.err); 178 179 System.err.println("\n**********************************************************\n"); 180 181 } 182 183 186 187 public void doPost(HttpServletRequest request, HttpServletResponse response) 188 throws IOException , ServletException 189 { 190 doService(request, response); 191 } 192 193 200 201 public void init(ServletConfig config) throws ServletException 202 { 203 String name = config.getServletName(); 204 205 _registryKey = REGISTRY_KEY_PREFIX + name; 206 207 long startTime = System.currentTimeMillis(); 208 long elapsedToRegistry = 0; 209 210 super.init(config); 211 212 _resolver = createClassResolver(); 213 214 try 215 { 216 _registry = constructRegistry(config); 217 218 elapsedToRegistry = System.currentTimeMillis() - startTime; 219 220 initializeApplication(); 221 222 config.getServletContext().setAttribute(_registryKey, _registry); 223 } 224 catch (Exception ex) 225 { 226 show(ex); 227 228 throw new ServletException (TapestryMessages.servletInitFailure(ex), ex); 229 } 230 231 long elapsedOverall = System.currentTimeMillis() - startTime; 232 233 LOG.info(TapestryMessages.servletInit(name, elapsedToRegistry, elapsedOverall)); 234 } 235 236 246 247 protected ClassResolver createClassResolver() throws ServletException 248 { 249 return new DefaultClassResolver(); 250 } 251 252 261 protected Registry constructRegistry(ServletConfig config) 262 { 263 ErrorHandler errorHandler = constructErrorHandler(config); 264 265 RegistryBuilder builder = new RegistryBuilder(errorHandler); 266 267 builder.addModuleDescriptorProvider(new XmlModuleDescriptorProvider(_resolver)); 268 269 String name = config.getServletName(); 270 ServletContext context = config.getServletContext(); 271 272 addModuleIfExists(builder, context, "/WEB-INF/" + name + "/hivemodule.xml"); 273 addModuleIfExists(builder, context, "/WEB-INF/hivemodule.xml"); 274 275 return builder.constructRegistry(Locale.getDefault()); 276 } 277 278 286 protected ErrorHandler constructErrorHandler(ServletConfig config) 287 { 288 return new StrictErrorHandler(); 289 } 290 291 297 298 protected void addModuleIfExists(RegistryBuilder builder, ServletContext context, String path) 299 { 300 Resource r = new ContextResource(context, path); 301 302 if (r.getResourceURL() == null) 303 return; 304 305 builder.addModuleDescriptorProvider(new XmlModuleDescriptorProvider(_resolver, r)); 306 } 307 308 315 protected void initializeApplication() 316 { 317 ApplicationInitializer ai = (ApplicationInitializer) _registry.getService( 318 "tapestry.init.MasterInitializer", 319 ApplicationInitializer.class); 320 321 ai.initialize(this); 322 323 _registry.cleanupThread(); 324 325 _requestServicer = (ServletRequestServicer) _registry.getService( 326 "tapestry.request.ServletRequestServicer", 327 ServletRequestServicer.class); 328 } 329 330 335 public void destroy() 336 { 337 getServletContext().removeAttribute(_registryKey); 338 339 if (_registry != null) 340 { 341 _registry.shutdown(); 342 _registry = null; 343 } 344 } 345 } | Popular Tags |