org.apache.tapestry
Class ApplicationServlet

java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
          extended by org.apache.tapestry.ApplicationServlet
All Implemented Interfaces:
Serializable, Servlet, ServletConfig

public class ApplicationServlet
extends HttpServlet

Links a servlet container with a Tapestry application. The servlet has some responsibilities related to bootstrapping the application (in terms of logging, reading the specification, etc.). It is also responsible for creating or locating the IEngine and delegating incoming requests to it.

The servlet init parameter org.apache.tapestry.specification-path should be set to the complete resource path (within the classpath) to the application specification, i.e., /com/foo/bar/MyApp.application.

In some servlet containers (notably WebLogic) it is necessary to invoke HttpSession.setAttribute(String,Object) in order to force a persistent value to be replicated to the other servers in the cluster. Tapestry applications usually only have a single persistent value, the engine. For persistence to work in such an environment, the JVM system property org.apache.tapestry.store-engine must be set to true. This will force the application servlet to restore the engine into the HttpSession at the end of each request cycle.

As of release 1.0.1, it is no longer necessary for a HttpSession to be created on the first request cycle. Instead, the HttpSession is created as needed by the IEngine ... that is, when a visit object is created, or when persistent page state is required. Otherwise, for sessionless requests, an IEngine from a Pool is used. Additional work must be done so that the IEngine can change locale without forcing the creation of a session; this involves the servlet and the engine storing locale information in a Cookie.

Version:
$Id: ApplicationServlet.java 244163 2005-03-17 05:18:32Z pferraro $
Author:
Howard Lewis Ship
See Also:
Serialized Form

Constructor Summary
ApplicationServlet()
           
 
Method Summary
protected  void close(InputStream stream)
          Closes the stream, ignoring any exceptions.
protected  IApplicationSpecification constructApplicationSpecification()
          Invoked from init(ServletConfig) to read and construct the ApplicationSpecification for this servlet.
protected  IApplicationSpecification constructStandinSpecification()
          Invoked from constructApplicationSpecification() when the application doesn't have an explicit specification.
protected  IEngine createEngine(RequestContext context)
          Invoked by getEngine(RequestContext) to create the IEngine instance specific to the application, if not already in the HttpSession.
protected  IPropertySource createPropertySource()
          Creates an instance of IPropertySource used for searching of configuration values.
protected  RequestContext createRequestContext(HttpServletRequest request, HttpServletResponse response)
          Invoked by doService(HttpServletRequest, HttpServletResponse) to create the RequestContext for this request cycle.
protected  IResourceResolver createResourceResolver()
          Invoked from init(ServletConfig) to create a resource resolver for the servlet (which will utlimately be shared and used through the application).
 void destroy()
          Ensures that shared janitor thread is terminated.
 void doGet(HttpServletRequest request, HttpServletResponse response)
          Invokes doService(HttpServletRequest, HttpServletResponse).
 void doPost(HttpServletRequest request, HttpServletResponse response)
          Invokes doService(HttpServletRequest, HttpServletResponse).
protected  void doService(HttpServletRequest request, HttpServletResponse response)
          Handles the GET and POST requests.
 IApplicationSpecification getApplicationSpecification()
          Returns the application specification, which is read by the init(ServletConfig) method.
protected  IResourceLocation getApplicationSpecificationLocation()
          Gets the location of the application specification, if there is one.
protected  String getApplicationSpecificationPath()
          Reads the servlet init parameter org.apache.tapestry.application-specification, which is the location, on the classpath, of the application specification.
protected  IEngine getEngine(RequestContext context)
          Retrieves the engine that will process this request.
protected  String getEngineClassName()
          Returns the name of the class to use when instantiating an engine instance for this application.
protected  Locale getLocaleFromRequest(RequestContext context)
          Determines the Locale for the incoming request.
 IResourceResolver getResourceResolver()
          Returns a resource resolver that can access classes and resources related to the current web application context.
 void init(ServletConfig config)
          Reads the application specification when the servlet is first initialized.
protected  IApplicationSpecification parseApplicationSpecification(IResourceLocation location)
          Invoked from constructApplicationSpecification() to actually parse the stream (with content provided from the path) and convert it into an ApplicationSpecification.
protected  String searchConfiguration(String propertyName)
          Searches for a configuration property in: The servlet's initial parameters The servlet context's initial parameters JVM system properties
protected  void show(Exception ex)
           
 void writeLocaleCookie(Locale locale, IEngine engine, RequestContext cycle)
          Invoked from the engine, just prior to starting to render a response, when the locale has changed.
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPut, doTrace, getLastModified, service, service
 
Methods inherited from class javax.servlet.GenericServlet
getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ApplicationServlet

public ApplicationServlet()
Method Detail

doGet

public void doGet(HttpServletRequest request,
                  HttpServletResponse response)
           throws IOException,
                  ServletException
Invokes doService(HttpServletRequest, HttpServletResponse).

Overrides:
doGet in class HttpServlet
Throws:
IOException
ServletException
Since:
1.0.6

doService

protected void doService(HttpServletRequest request,
                         HttpServletResponse response)
                  throws IOException,
                         ServletException
Handles the GET and POST requests. Performs the following:

Throws:
IOException
ServletException

createRequestContext

protected RequestContext createRequestContext(HttpServletRequest request,
                                              HttpServletResponse response)
                                       throws IOException
Invoked by doService(HttpServletRequest, HttpServletResponse) to create the RequestContext for this request cycle. Some applications may need to replace the default RequestContext with a subclass for particular behavior.

Throws:
IOException
Since:
2.3

show

protected void show(Exception ex)

doPost

public void doPost(HttpServletRequest request,
                   HttpServletResponse response)
            throws IOException,
                   ServletException
Invokes doService(HttpServletRequest, HttpServletResponse).

Overrides:
doPost in class HttpServlet
Throws:
IOException
ServletException

getApplicationSpecification

public IApplicationSpecification getApplicationSpecification()
Returns the application specification, which is read by the init(ServletConfig) method.


getEngine

protected IEngine getEngine(RequestContext context)
                     throws ServletException
Retrieves the engine that will process this request. This comes from one of the following places:

Throws:
ServletException

getLocaleFromRequest

protected Locale getLocaleFromRequest(RequestContext context)
                               throws ServletException
Determines the Locale for the incoming request. This is determined from the locale cookie or, if not set, from the request itself. This may return null if no locale is determined.

Throws:
ServletException

init

public void init(ServletConfig config)
          throws ServletException
Reads the application specification when the servlet is first initialized. All engine instances will have access to the specification via the servlet.

Specified by:
init in interface Servlet
Overrides:
init in class GenericServlet
Throws:
ServletException
See Also:
getApplicationSpecification(), constructApplicationSpecification(), createResourceResolver()

createResourceResolver

protected IResourceResolver createResourceResolver()
                                            throws ServletException
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 DefaultResourceResolver, subclasses may provide a different implementation.

Throws:
ServletException
Since:
2.3
See Also:
getResourceResolver()

constructApplicationSpecification

protected IApplicationSpecification constructApplicationSpecification()
                                                               throws ServletException
Invoked from init(ServletConfig) to read and construct the ApplicationSpecification for this servlet. Invokes getApplicationSpecificationPath(), opens the resource as a stream, then invokes parseApplicationSpecification(IResourceLocation).

This method exists to be overriden in applications where the application specification cannot be loaded from the classpath. Alternately, a subclass could override this method, invoke this implementation, and then add additional data to it (for example, an application where some of the pages are defined in an external source such as a database).

Throws:
ServletException
Since:
2.2

getApplicationSpecificationLocation

protected IResourceLocation getApplicationSpecificationLocation()
                                                         throws ServletException
Gets the location of the application specification, if there is one.

Returns the location of the application specification, or null if not found.

Throws:
ServletException
Since:
3.0

constructStandinSpecification

protected IApplicationSpecification constructStandinSpecification()
Invoked from constructApplicationSpecification() when the application doesn't have an explicit specification. A simple specification is constructed and returned. This is useful for minimal applications and prototypes.

Since:
3.0

parseApplicationSpecification

protected IApplicationSpecification parseApplicationSpecification(IResourceLocation location)
                                                           throws ServletException
Invoked from constructApplicationSpecification() to actually parse the stream (with content provided from the path) and convert it into an ApplicationSpecification.

Throws:
ServletException
Since:
2.2

close

protected void close(InputStream stream)
Closes the stream, ignoring any exceptions.


getApplicationSpecificationPath

protected String getApplicationSpecificationPath()
                                          throws ServletException
Reads the servlet init parameter org.apache.tapestry.application-specification, which is the location, on the classpath, of the application specification.

If the parameter is not set, this method returns null, and a search for the application specification within the servlet context will begin.

Throws:
ServletException
See Also:
getApplicationSpecificationLocation()

createEngine

protected IEngine createEngine(RequestContext context)
                        throws ServletException
Invoked by getEngine(RequestContext) to create the IEngine instance specific to the application, if not already in the HttpSession.

The IEngine instance returned is stored into the HttpSession.

Throws:
ServletException
See Also:
getEngineClassName()

getEngineClassName

protected String getEngineClassName()
Returns the name of the class to use when instantiating an engine instance for this application. If the application specification provides a value, this is returned. Otherwise, a search for the configuration property org.apache.tapestry.engine-class occurs (see searchConfiguration(String)).

If the search is still unsuccessful, then BaseEngine is used.


searchConfiguration

protected String searchConfiguration(String propertyName)
Searches for a configuration property in:

Since:
3.0
See Also:
createPropertySource()

createPropertySource

protected IPropertySource createPropertySource()
Creates an instance of IPropertySource used for searching of configuration values. Subclasses may override this method if they want to change the normal locations that properties are searched for within.

Since:
3.0

writeLocaleCookie

public void writeLocaleCookie(Locale locale,
                              IEngine engine,
                              RequestContext cycle)
Invoked from the engine, just prior to starting to render a response, when the locale has changed. The servlet writes a Cookie so that, on subsequent request cycles, an engine localized to the selected locale is chosen.

At this time, the cookie is not persistent. That may change in subsequent releases.

Since:
1.0.1

getResourceResolver

public IResourceResolver getResourceResolver()
Returns a resource resolver that can access classes and resources related to the current web application context. Relies on Thread.getContextClassLoader(), which is set by most modern servlet containers.

Since:
2.3

destroy

public void destroy()
Ensures that shared janitor thread is terminated.

Specified by:
destroy in interface Servlet
Overrides:
destroy in class GenericServlet
Since:
3.0.3
See Also:
Servlet.destroy()