Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // Copyright 2004, 2005 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //     http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
 // distributed under the License is distributed on an "AS IS" BASIS,
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
 package org.apache.tapestry;
 
 import  org.apache.hivemind.ClassResolver;
 import  org.apache.hivemind.ErrorHandler;
 import  org.apache.hivemind.Registry;
 import  org.apache.hivemind.Resource;
 import  org.apache.hivemind.impl.DefaultClassResolver;
 import  org.apache.hivemind.impl.RegistryBuilder;
 import  org.apache.hivemind.impl.StrictErrorHandler;
 import  org.apache.hivemind.impl.XmlModuleDescriptorProvider;
 import  org.apache.hivemind.util.ContextResource;
 
Links a servlet container with a Tapestry application. The servlet init parameter org.apache.tapestry.application-specification should be set to the complete resource path (within the classpath) to the application specification, i.e., /com/foo/bar/MyApp.application. As of release 4.0, this servlet will also create a HiveMind Registry and manage it.

Author(s):
Howard Lewis Ship
See also:
org.apache.tapestry.services.ApplicationInitializer
org.apache.tapestry.services.ServletRequestServicer
 
 
 public class ApplicationServlet extends HttpServlet
 {
     private static final long serialVersionUID = -8046042689991538059L;

    
Prefix used to store the HiveMind Registry into the ServletContext. This string is suffixed with the servlet name (in case multiple Tapestry applications are executing within a single web application).

Since:
4.0
 
 
     private static final String REGISTRY_KEY_PREFIX = "org.apache.tapestry.Registry:";
 
     private static final Log LOG = LogFactory.getLog(ApplicationServlet.class);

    

Since:
2.3
 
 
     private ClassResolver _resolver;

    
The key used to store the registry into the ServletContext.

Since:
4.0
 
 
     private String _registryKey;

    

Since:
4.0
 
 
     private Registry _registry;

    

Since:
4.0
 
     private ServletRequestServicer _requestServicer;

    
Invokes doService(HttpServletRequest, HttpServletResponse).

Since:
1.0.6
 
 
    public void doGet(HttpServletRequest requestHttpServletResponse responsethrows IOException,
            ServletException
    {
        doService(requestresponse);
    }

    
Handles the GET and POST requests. Performs the following:
  • Invokes org.apache.hivemind.Registry.setupThread()
  • Invokes ServletRequestServicer.service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse).
    protected void doService(HttpServletRequest requestHttpServletResponse response)
            throws IOExceptionServletException
    {
        try
        {
            .setupThread();
            .service(requestresponse);
        }
        catch (ServletException ex)
        {
            log("ServletException"ex);
            show(ex);
            // Rethrow it.
            throw ex;
        }
        catch (IOException ex)
        {
            log("IOException"ex);
            show(ex);
            // Rethrow it.
            throw ex;
        }
        finally
        {
            .cleanupThread();
        }
    }
    protected void show(Exception ex)
    {
        ..println("\n\n**********************************************************\n\n");
        new ExceptionAnalyzer().reportException(ex.);
        ..println("\n**********************************************************\n");
    }

    
Invokes doService(HttpServletRequest, HttpServletResponse).
    public void doPost(HttpServletRequest requestHttpServletResponse response)
            throws IOExceptionServletException
    {
        doService(requestresponse);
    }

    
Reads the application specification when the servlet is first initialized. All engine instanceswill have access to the specification via the servlet.

See also:
constructRegistry(ServletConfig)
createClassResolver()
    public void init(ServletConfig configthrows ServletException
    {
        String name = config.getServletName();
         =  + name;
        long startTime = System.currentTimeMillis();
        long elapsedToRegistry = 0;
        super.init(config);
         = createClassResolver();
        try
        {
             = constructRegistry(config);
            elapsedToRegistry = System.currentTimeMillis() - startTime;
            initializeApplication();
            config.getServletContext().setAttribute();
        }
        catch (Exception ex)
        {
            show(ex);
            throw new ServletException(TapestryMessages.servletInitFailure(ex), ex);
        }
        long elapsedOverall = System.currentTimeMillis() - startTime;
        .info(TapestryMessages.servletInit(nameelapsedToRegistryelapsedOverall));
    }

    
Invoked from init(ServletConfig)to create a resource resolver for the servlet (which will utlimately be shared and used through the application).

This implementation constructs a DefaultClassResolver, subclasses may provide a different implementation.

Since:
2.3
See also:
DefaultClassResolver
    protected ClassResolver createClassResolver()
    {
        return new DefaultClassResolver();
    }

    
Invoked from init(ServletConfig)to construct the Registry to be used by the application.

This looks in the standard places (on the classpath), but also in the WEB-INF/name and WEB-INF folders (where name is the name of the servlet).

Since:
4.0
    protected Registry constructRegistry(ServletConfig config)
    {
        ErrorHandler errorHandler = constructErrorHandler(config);
        RegistryBuilder builder = new RegistryBuilder(errorHandler);
        builder.addModuleDescriptorProvider(new XmlModuleDescriptorProvider());
        String name = config.getServletName();
        ServletContext context = config.getServletContext();
        addModuleIfExists(buildercontext"/WEB-INF/" + name + "/hivemodule.xml");
        addModuleIfExists(buildercontext"/WEB-INF/hivemodule.xml");
        return builder.constructRegistry(Locale.getDefault());
    }

    
Invoked by constructRegistry(ServletConfig) to create and return an ErrorHandler instance to be used when constructing the Registry (and then to handle any runtime exceptions). This implementation returns a new instance of org.apache.hivemind.impl.StrictErrorHandler.

Since:
4.0
    protected ErrorHandler constructErrorHandler(ServletConfig config)
    {
        return new StrictErrorHandler();
    }

    
Looks for a file in the servlet context; if it exists, it is expected to be a HiveMind module descriptor, and is added to the builder.

Since:
4.0
    protected void addModuleIfExists(RegistryBuilder builderServletContext contextString path)
    {
        Resource r = new ContextResource(contextpath);
        if (r.getResourceURL() == null)
            return;
        builder.addModuleDescriptorProvider(new XmlModuleDescriptorProvider(r));
    }

    
Invoked from init(ServletConfig), after the registry has been constructed, to bootstrap the application via the tapestry.MasterApplicationInitializer service.

Since:
4.0
    protected void initializeApplication()
    {
        ApplicationInitializer ai = (ApplicationInitializer.getService(
                "tapestry.init.MasterInitializer",
                ApplicationInitializer.class);
        ai.initialize(this);
        .cleanupThread();
         = (ServletRequestServicer.getService(
                "tapestry.request.ServletRequestServicer",
                ServletRequestServicer.class);
    }

    
Shuts down the registry (if it exists).

Since:
4.0
    public void destroy()
    {
        if ( != null)
        {
            .shutdown();
             = null;
        }
        super.destroy();
    }
New to GrepCode? Check out our FAQ X