Coverage Report - org.apache.tapestry.ApplicationServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
ApplicationServlet
0%
0/66
0%
0/8
1.818
 
 1  
 // Copyright 2004, 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry;
 16  
 
 17  
 import org.apache.commons.logging.Log;
 18  
 import org.apache.commons.logging.LogFactory;
 19  
 import org.apache.hivemind.ClassResolver;
 20  
 import org.apache.hivemind.ErrorHandler;
 21  
 import org.apache.hivemind.Registry;
 22  
 import org.apache.hivemind.Resource;
 23  
 import org.apache.hivemind.impl.DefaultClassResolver;
 24  
 import org.apache.hivemind.impl.RegistryBuilder;
 25  
 import org.apache.hivemind.impl.StrictErrorHandler;
 26  
 import org.apache.hivemind.impl.XmlModuleDescriptorProvider;
 27  
 import org.apache.hivemind.util.ContextResource;
 28  
 import org.apache.tapestry.services.ApplicationInitializer;
 29  
 import org.apache.tapestry.services.ServletRequestServicer;
 30  
 import org.apache.tapestry.util.exception.ExceptionAnalyzer;
 31  
 
 32  
 import javax.servlet.ServletConfig;
 33  
 import javax.servlet.ServletContext;
 34  
 import javax.servlet.ServletException;
 35  
 import javax.servlet.http.HttpServlet;
 36  
 import javax.servlet.http.HttpServletRequest;
 37  
 import javax.servlet.http.HttpServletResponse;
 38  
 import java.io.IOException;
 39  
 import java.util.Locale;
 40  
 
 41  
 /**
 42  
  * Links a servlet container with a Tapestry application. The servlet init parameter
 43  
  * <code>org.apache.tapestry.application-specification</code> should be set to the complete
 44  
  * resource path (within the classpath) to the application specification, i.e.,
 45  
  * <code>/com/foo/bar/MyApp.application</code>. As of release 4.0, this servlet will also create
 46  
  * a HiveMind Registry and manage it.
 47  
  *
 48  
  * @author Howard Lewis Ship
 49  
  * @see org.apache.tapestry.services.ApplicationInitializer
 50  
  * @see org.apache.tapestry.services.ServletRequestServicer
 51  
  */
 52  
 
 53  0
 public class ApplicationServlet extends HttpServlet
 54  
 {
 55  
     private static final long serialVersionUID = -8046042689991538059L;
 56  
 
 57  
     /**
 58  
      * Prefix used to store the HiveMind Registry into the ServletContext. This string is suffixed
 59  
      * with the servlet name (in case multiple Tapestry applications are executing within a single
 60  
      * web application).
 61  
      *
 62  
      * @since 4.0
 63  
      */
 64  
 
 65  
     private static final String REGISTRY_KEY_PREFIX = "org.apache.tapestry.Registry:";
 66  
 
 67  0
     private static final Log LOG = LogFactory.getLog(ApplicationServlet.class);
 68  
 
 69  
     /**
 70  
      * @since 2.3
 71  
      */
 72  
 
 73  
     private ClassResolver _resolver;
 74  
 
 75  
     /**
 76  
      * The key used to store the registry into the ServletContext.
 77  
      *
 78  
      * @since 4.0
 79  
      */
 80  
 
 81  
     private String _registryKey;
 82  
 
 83  
     /**
 84  
      * @since 4.0
 85  
      */
 86  
 
 87  
     private Registry _registry;
 88  
 
 89  
     /**
 90  
      * @since 4.0
 91  
      */
 92  
     private ServletRequestServicer _requestServicer;
 93  
 
 94  
     /**
 95  
      * Invokes {@link #doService(HttpServletRequest, HttpServletResponse)}.
 96  
      *
 97  
      * @since 1.0.6
 98  
      */
 99  
 
 100  
     public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,
 101  
             ServletException
 102  
     {
 103  0
         doService(request, response);
 104  0
     }
 105  
 
 106  
     /**
 107  
      * Handles the GET and POST requests. Performs the following:
 108  
      *
 109  
      * <ul>
 110  
      *  <li>
 111  
      *  Invokes {@link org.apache.hivemind.Registry#setupThread()}
 112  
      * </li>
 113  
      * <li>
 114  
      *  Invokes {@link ServletRequestServicer#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)}.
 115  
      * </li>
 116  
      * </ul>
 117  
      */
 118  
 
 119  
     protected void doService(HttpServletRequest request, HttpServletResponse response)
 120  
             throws IOException, ServletException
 121  
     {
 122  
         try
 123  
         {
 124  0
             _registry.setupThread();
 125  
 
 126  0
             _requestServicer.service(request, response);
 127  
         }
 128  0
         catch (ServletException ex)
 129  
         {
 130  0
             log("ServletException", ex);
 131  
 
 132  0
             show(ex);
 133  
 
 134  
             // Rethrow it.
 135  
 
 136  0
             throw ex;
 137  
         }
 138  0
         catch (IOException ex)
 139  
         {
 140  0
             log("IOException", ex);
 141  
 
 142  0
             show(ex);
 143  
 
 144  
             // Rethrow it.
 145  
 
 146  0
             throw ex;
 147  
         }
 148  
         finally
 149  
         {
 150  0
             _registry.cleanupThread();
 151  0
         }
 152  0
     }
 153  
 
 154  
     protected void show(Exception ex)
 155  
     {
 156  0
         System.err.println("\n\n**********************************************************\n\n");
 157  
 
 158  0
         new ExceptionAnalyzer().reportException(ex, System.err);
 159  
 
 160  0
         System.err.println("\n**********************************************************\n");
 161  
 
 162  0
     }
 163  
 
 164  
     /**
 165  
      * Invokes {@link #doService(HttpServletRequest, HttpServletResponse)}.
 166  
      */
 167  
 
 168  
     public void doPost(HttpServletRequest request, HttpServletResponse response)
 169  
             throws IOException, ServletException
 170  
     {
 171  0
         doService(request, response);
 172  0
     }
 173  
 
 174  
     /**
 175  
      * Reads the application specification when the servlet is first initialized. All
 176  
      * {@link IEngine engine instances}will have access to the specification via the servlet.
 177  
      *
 178  
      * @see #constructRegistry(ServletConfig)
 179  
      * @see #createClassResolver()
 180  
      */
 181  
 
 182  
     public void init(ServletConfig config) throws ServletException
 183  
     {
 184  0
         String name = config.getServletName();
 185  
 
 186  0
         _registryKey = REGISTRY_KEY_PREFIX + name;
 187  
 
 188  0
         long startTime = System.currentTimeMillis();
 189  0
         long elapsedToRegistry = 0;
 190  
 
 191  0
         super.init(config);
 192  
 
 193  0
         _resolver = createClassResolver();
 194  
 
 195  
         try
 196  
         {
 197  0
             _registry = constructRegistry(config);
 198  
 
 199  0
             elapsedToRegistry = System.currentTimeMillis() - startTime;
 200  
 
 201  0
             initializeApplication();
 202  
 
 203  0
             config.getServletContext().setAttribute(_registryKey, _registry);
 204  
         }
 205  0
         catch (Exception ex)
 206  
         {
 207  0
             show(ex);
 208  
 
 209  0
             throw new ServletException(TapestryMessages.servletInitFailure(ex), ex);
 210  0
         }
 211  
 
 212  0
         long elapsedOverall = System.currentTimeMillis() - startTime;
 213  
 
 214  0
         LOG.info(TapestryMessages.servletInit(name, elapsedToRegistry, elapsedOverall));
 215  0
     }
 216  
 
 217  
     /**
 218  
      * Invoked from {@link #init(ServletConfig)}to create a resource resolver for the servlet
 219  
      * (which will utlimately be shared and used through the application).
 220  
      * <p>
 221  
      * This implementation constructs a {@link DefaultClassResolver}, subclasses may provide a
 222  
      * different implementation.
 223  
      *
 224  
      * @see DefaultClassResolver
 225  
      * @since 2.3
 226  
      */
 227  
 
 228  
     protected ClassResolver createClassResolver()
 229  
     {
 230  0
         return new DefaultClassResolver();
 231  
     }
 232  
 
 233  
     /**
 234  
      * Invoked from {@link #init(ServletConfig)}to construct the Registry to be used by the
 235  
      * application.
 236  
      * <p>
 237  
      * This looks in the standard places (on the classpath), but also in the WEB-INF/name and
 238  
      * WEB-INF folders (where name is the name of the servlet).
 239  
      *
 240  
      * @since 4.0
 241  
      */
 242  
     protected Registry constructRegistry(ServletConfig config)
 243  
     {
 244  0
         ErrorHandler errorHandler = constructErrorHandler(config);
 245  
 
 246  0
         RegistryBuilder builder = new RegistryBuilder(errorHandler);
 247  
 
 248  0
         builder.addModuleDescriptorProvider(new XmlModuleDescriptorProvider(_resolver));
 249  
 
 250  0
         String name = config.getServletName();
 251  0
         ServletContext context = config.getServletContext();
 252  
 
 253  0
         addModuleIfExists(builder, context, "/WEB-INF/" + name + "/hivemodule.xml");
 254  0
         addModuleIfExists(builder, context, "/WEB-INF/hivemodule.xml");
 255  
 
 256  0
         return builder.constructRegistry(Locale.getDefault());
 257  
     }
 258  
 
 259  
     /**
 260  
      * Invoked by {@link #constructRegistry(ServletConfig)} to create and return an
 261  
      * {@link ErrorHandler} instance to be used when constructing the Registry (and then to handle
 262  
      * any runtime exceptions). This implementation returns a new instance of
 263  
      * {@link org.apache.hivemind.impl.StrictErrorHandler}.
 264  
      *
 265  
      * @since 4.0
 266  
      */
 267  
     protected ErrorHandler constructErrorHandler(ServletConfig config)
 268  
     {
 269  0
         return new StrictErrorHandler();
 270  
     }
 271  
 
 272  
     /**
 273  
      * Looks for a file in the servlet context; if it exists, it is expected to be a HiveMind module
 274  
      * descriptor, and is added to the builder.
 275  
      *
 276  
      * @since 4.0
 277  
      */
 278  
 
 279  
     protected void addModuleIfExists(RegistryBuilder builder, ServletContext context, String path)
 280  
     {
 281  0
         Resource r = new ContextResource(context, path);
 282  
 
 283  0
         if (r.getResourceURL() == null)
 284  0
             return;
 285  
 
 286  0
         builder.addModuleDescriptorProvider(new XmlModuleDescriptorProvider(_resolver, r));
 287  0
     }
 288  
 
 289  
     /**
 290  
      * Invoked from {@link #init(ServletConfig)}, after the registry has been constructed, to
 291  
      * bootstrap the application via the <code>tapestry.MasterApplicationInitializer</code>
 292  
      * service.
 293  
      *
 294  
      * @since 4.0
 295  
      */
 296  
     protected void initializeApplication()
 297  
     {
 298  0
         ApplicationInitializer ai = (ApplicationInitializer) _registry.getService(
 299  
                 "tapestry.init.MasterInitializer",
 300  
                 ApplicationInitializer.class);
 301  
 
 302  0
         ai.initialize(this);
 303  
 
 304  0
         _registry.cleanupThread();
 305  
 
 306  0
         _requestServicer = (ServletRequestServicer) _registry.getService(
 307  
                 "tapestry.request.ServletRequestServicer",
 308  
                 ServletRequestServicer.class);
 309  0
     }
 310  
 
 311  
     /**
 312  
      * Shuts down the registry (if it exists).
 313  
      *
 314  
      * @since 4.0
 315  
      */
 316  
     public void destroy()
 317  
     {
 318  0
         getServletContext().removeAttribute(_registryKey);
 319  
 
 320  0
         if (_registry != null)
 321  
         {
 322  0
             _registry.shutdown();
 323  0
             _registry = null;
 324  
         }
 325  
 
 326  0
         super.destroy();
 327  0
     }
 328  
 }