com.sun.grizzly.http.servlet
Class ServletAdapter

java.lang.Object
  extended by com.sun.grizzly.tcp.StaticResourcesAdapter
      extended by com.sun.grizzly.tcp.http11.GrizzlyAdapter
          extended by com.sun.grizzly.http.servlet.ServletAdapter
All Implemented Interfaces:
Adapter
Direct Known Subclasses:
OSGiServletAdapter, WebAppAdapter

public class ServletAdapter
extends GrizzlyAdapter

Adapter class that can initiate a FilterChain and execute its Filter and its Servlet Configuring a GrizzlyWebServer or SelectorThread to use this GrizzlyAdapter implementation add the ability of servicing Servlet as well as static resources. This class can be used to programatically configure a Servlet, Filters, listeners, init parameters, context-param, etc. a application usually defined using the web.xml. See addInitParameter(String, String) addContextParameter(String, String) setProperty(String, Object), addServletListener(String), etc. As an example:


      GrizzlyWebServer ws = new GrizzlyWebServer("/var/www");
        try{
            ServletAdapter sa = new ServletAdapter();
            sa.setRootFolder("/Path/To/Exploded/War/File");
            sa.setServlet(new MyServlet());

            // Set the Servlet's Name
            // Any ServletConfig.getXXX method can be configured using this call.
            // The same apply for ServletContext.getXXX.
            sa.setProperty("display-name","myServlet");
            sa.addListener("foo.bar.myHttpSessionListener");
            sa.addListener(MyOtherHttpSessionListener.class);
            sa.addServletContextListener(new FooServletContextListener());
            sa.addServletContextAttributeListener(new BarServletCtxAttListener());
            sa.addContextParameter("databaseURI","jdbc://");
            sa.addInitParameter("password","hello"); 
            sa.setServletPath("/MyServletPath");
            sa.setContextPath("/myApp");

            ws.addGrizzlyAdapter(sa);

            ws.start();
        } catch (IOException ex){
            // Something when wrong.
        }
 

Author:
Jeanfrancois Arcand

Field Summary
protected  ClassLoader classLoader
           
static int INCREMENT
           
protected  boolean initialize
          Initialize the ServletContext
static String LOAD_ON_STARTUP
           
protected  HashMap<String,Object> properties
          Holder for our configured properties.
static int REQUEST_RESPONSE_NOTES
           
static int SERVLETCONFIG_NOTES
           
protected  javax.servlet.Servlet servletInstance
           
 
Fields inherited from class com.sun.grizzly.tcp.http11.GrizzlyAdapter
ADAPTER_NOTES, ALLOW_BACKSLASH, chunkingDisabled
 
Fields inherited from class com.sun.grizzly.tcp.StaticResourcesAdapter
cache, commitErrorResponse, fileFolders, logger, resourcesContextPath
 
Constructor Summary
  ServletAdapter()
           
  ServletAdapter(javax.servlet.Servlet servlet)
          Create a ServletAdapter which support the specific Servlet
  ServletAdapter(javax.servlet.Servlet servlet, ServletContextImpl servletContext)
           
  ServletAdapter(String publicDirectory)
          Create a new instance which will look for static pages located under publicDirectory folder.
protected ServletAdapter(String publicDirectory, ServletContextImpl servletCtx, Map<String,String> contextParameters, Map<String,String> servletInitParameters, List<String> listeners)
          Convenience constructor.
  ServletAdapter(String publicDirectory, String name)
          Create a new instance which will look for static pages located under publicDirectory folder.
protected ServletAdapter(String publicDirectory, String servletName, ServletContextImpl servletCtx, Map<String,String> contextParameters, Map<String,String> servletInitParameters, boolean initialize)
          Convenience constructor.
protected ServletAdapter(String publicDirectory, String servletName, ServletContextImpl servletCtx, Map<String,String> contextParameters, Map<String,String> servletInitParameters, List<String> listeners)
          Convenience constructor.
protected ServletAdapter(String publicDirectory, String servletName, ServletContextImpl servletCtx, Map<String,String> contextParameters, Map<String,String> servletInitParameters, List<String> listeners, boolean initialize)
          Convenience constructor.
 
Method Summary
 void addContextParameter(String name, String value)
          Add a new servlet context parameter for this servlet.
protected  void addFilter(FilterConfigImpl filterConfig)
          Add a filter to the set of filters that will be executed in this chain.
 void addFilter(javax.servlet.Filter filter, String filterName, Map initParameters)
          Add a Filter to the ServletAdapter.FilterChainImpl
 void addInitParameter(String name, String value)
          Add a new servlet initialization parameter for this servlet.
 void addServletListener(String listenerName)
          Add Servlet listeners that implement EventListener
 void afterService(GrizzlyRequest request, GrizzlyResponse response)
          Once the GrizzlyAdapter.service(com.sun.grizzly.tcp.Request, com.sun.grizzly.tcp.Response) method has been execyuted, the container will call this method to allow any extension to clean up there associated GrizzlyRequest and GrizzlyResponse.
protected  void configureClassLoader(String applicationPath)
          Create a URLClassLoader which has the capability of loading classes jar under an exploded war application.
protected  void configureServletEnv()
          Configure the ServletContextImpl and ServletConfigImpl
 boolean containsInitParameter(String name)
          if the servlet initialization parameter in present for this servlet.
 void customizeErrorPage(GrizzlyResponse response, String message, int errorCode)
          Customize the error page returned to the client.
 void destroy()
          Destroy this Servlet and its associated ServletContextListener
 void doService(GrizzlyRequest request, GrizzlyResponse response)
           
 ClassLoader getClassLoader()
           
protected  Map<String,String> getContextParameters()
           
 String getContextPath()
          Returns the portion of the request URI that indicates the context of the request.
 String getInitParameter(String name)
          get a servlet initialization parameter for this servlet.
protected  List<String> getListeners()
           
 String getName()
           
 Object getProperty(String name)
          Return a configured property.
protected  ServletContextImpl getServletCtx()
           
 javax.servlet.Servlet getServletInstance()
          Return the Servlet instance used by this ServletAdapter
 String getServletPath()
          Returns the part of this request's URL that calls the servlet.
 boolean isLoadOnStartup()
           
protected  void loadServlet()
          Load a Servlet instance.
 ServletAdapter newServletAdapter(javax.servlet.Servlet servlet)
          Create a new ServletAdapter instance that will share the same ServletContextImpl and Servlet's listener but with an empty map of init-parameters.
 void removeInitParameter(String name)
          Remove a servlet initialization parameter for this servlet.
 void removeProperty(String name)
          Remove a configured property.
 boolean removeServletListener(String listenerName)
          Remove Servlet listeners that implement EventListener
 void service(GrizzlyRequest request, GrizzlyResponse response)
          This method should contains the logic for any http extension to the Grizzly HTTP Webserver.
 void setClassLoader(ClassLoader classLoader)
           
 void setContextPath(String contextPath)
          Programmatically set the context path of the Servlet.
protected  void setDispatcherHelper(DispatcherHelper dispatcherHelper)
           
 void setProperty(String name, Object value)
          Set a configured property.
 void setServletInstance(javax.servlet.Servlet servletInstance)
          Set the Servlet instance used by this ServletAdapter
 void setServletPath(String servletPath)
          Programmatically set the servlet path of the Servlet.
 void start()
          Called when the GrizzlyAdapter's container is started by invoking GrizzlyWebServer#start or when .
 
Methods inherited from class com.sun.grizzly.tcp.http11.GrizzlyAdapter
afterService, isAllowEncodedSlash, isDecodeURL, isHandleStaticResources, service, setAllowEncodedSlash, setDecodeUrl, setHandleStaticResources
 
Methods inherited from class com.sun.grizzly.tcp.StaticResourcesAdapter
addRootFolder, customizedErrorPage, getDefaultContentType, getResourcesContextPath, getRootFolder, getRootFolders, initWebDir, isUseSendFile, service, setDefaultContentType, setLogger, setResourcesContextPath, setRootFolder, setUseSendFile
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REQUEST_RESPONSE_NOTES

public static final int REQUEST_RESPONSE_NOTES
See Also:
Constant Field Values

SERVLETCONFIG_NOTES

public static final int SERVLETCONFIG_NOTES
See Also:
Constant Field Values

LOAD_ON_STARTUP

public static final String LOAD_ON_STARTUP
See Also:
Constant Field Values

servletInstance

protected volatile javax.servlet.Servlet servletInstance

properties

protected HashMap<String,Object> properties
Holder for our configured properties.


initialize

protected boolean initialize
Initialize the ServletContext


classLoader

protected ClassLoader classLoader

INCREMENT

public static final int INCREMENT
See Also:
Constant Field Values
Constructor Detail

ServletAdapter

public ServletAdapter()

ServletAdapter

public ServletAdapter(javax.servlet.Servlet servlet)
Create a ServletAdapter which support the specific Servlet

Parameters:
servlet - Instance to be used by this adapter.

ServletAdapter

public ServletAdapter(String publicDirectory)
Create a new instance which will look for static pages located under publicDirectory folder.

Parameters:
publicDirectory - the folder where the static resource are located.

ServletAdapter

public ServletAdapter(String publicDirectory,
                      String name)
Create a new instance which will look for static pages located under publicDirectory folder.

Parameters:
publicDirectory - the folder where the static resource are located.

ServletAdapter

protected ServletAdapter(String publicDirectory,
                         ServletContextImpl servletCtx,
                         Map<String,String> contextParameters,
                         Map<String,String> servletInitParameters,
                         List<String> listeners)
Convenience constructor.

Parameters:
publicDirectory - The folder where the static resource are located.
servletCtx - ServletContextImpl to be used by new instance.
contextParameters - Context parameters.
servletInitParameters - servlet initialization parameres.
listeners - Listeners.

ServletAdapter

protected ServletAdapter(String publicDirectory,
                         String servletName,
                         ServletContextImpl servletCtx,
                         Map<String,String> contextParameters,
                         Map<String,String> servletInitParameters,
                         List<String> listeners)
Convenience constructor.

Parameters:
publicDirectory - The folder where the static resource are located.
servletCtx - ServletContextImpl to be used by new instance.
contextParameters - Context parameters.
servletInitParameters - servlet initialization parameres.
listeners - Listeners.

ServletAdapter

protected ServletAdapter(String publicDirectory,
                         String servletName,
                         ServletContextImpl servletCtx,
                         Map<String,String> contextParameters,
                         Map<String,String> servletInitParameters,
                         List<String> listeners,
                         boolean initialize)
Convenience constructor.

Parameters:
publicDirectory - The folder where the static resource are located.
servletCtx - ServletContextImpl to be used by new instance.
contextParameters - Context parameters.
servletInitParameters - servlet initialization parameres.
listeners - Listeners.
initialize - false only when the #newServletAdapter() is invoked.

ServletAdapter

protected ServletAdapter(String publicDirectory,
                         String servletName,
                         ServletContextImpl servletCtx,
                         Map<String,String> contextParameters,
                         Map<String,String> servletInitParameters,
                         boolean initialize)
Convenience constructor.

Parameters:
publicDirectory - The folder where the static resource are located.
servletCtx - ServletContextImpl to be used by new instance.
contextParameters - Context parameters.
servletInitParameters - servlet initialization parameres.
initialize - false only when the #newServletAdapter() is invoked.

ServletAdapter

public ServletAdapter(javax.servlet.Servlet servlet,
                      ServletContextImpl servletContext)
Method Detail

start

public void start()
Called when the GrizzlyAdapter's container is started by invoking GrizzlyWebServer#start or when . By default, it does nothing.

Overrides:
start in class GrizzlyAdapter

configureClassLoader

protected void configureClassLoader(String applicationPath)
                             throws IOException
Create a URLClassLoader which has the capability of loading classes jar under an exploded war application.

Parameters:
applicationPath - Application class path.
Throws:
IOException - I/O error.

service

public void service(GrizzlyRequest request,
                    GrizzlyResponse response)
This method should contains the logic for any http extension to the Grizzly HTTP Webserver.

Specified by:
service in class GrizzlyAdapter
Parameters:
request - The GrizzlyRequest
response - The GrizzlyResponse

doService

public void doService(GrizzlyRequest request,
                      GrizzlyResponse response)

customizeErrorPage

public void customizeErrorPage(GrizzlyResponse response,
                               String message,
                               int errorCode)
Customize the error page returned to the client.

Parameters:
response - the GrizzlyResponse
message - the Http error message
errorCode - the error code.

loadServlet

protected void loadServlet()
                    throws javax.servlet.ServletException
Load a Servlet instance.

Throws:
javax.servlet.ServletException - If failed to Servlet.init(javax.servlet.ServletConfig).

configureServletEnv

protected void configureServletEnv()
                            throws javax.servlet.ServletException
Configure the ServletContextImpl and ServletConfigImpl

Throws:
javax.servlet.ServletException - Error while configuring Servlet.

afterService

public void afterService(GrizzlyRequest request,
                         GrizzlyResponse response)
                  throws Exception
Once the GrizzlyAdapter.service(com.sun.grizzly.tcp.Request, com.sun.grizzly.tcp.Response) method has been execyuted, the container will call this method to allow any extension to clean up there associated GrizzlyRequest and GrizzlyResponse.

Overrides:
afterService in class GrizzlyAdapter
Parameters:
request - The GrizzlyRequest
response - The GrizzlyResponse
Throws:
Exception

addInitParameter

public void addInitParameter(String name,
                             String value)
Add a new servlet initialization parameter for this servlet.

Parameters:
name - Name of this initialization parameter to add
value - Value of this initialization parameter to add

removeInitParameter

public void removeInitParameter(String name)
Remove a servlet initialization parameter for this servlet.

Parameters:
name - Name of this initialization parameter to remove

getInitParameter

public String getInitParameter(String name)
get a servlet initialization parameter for this servlet.

Parameters:
name - Name of this initialization parameter to retreive

containsInitParameter

public boolean containsInitParameter(String name)
if the servlet initialization parameter in present for this servlet.

Parameters:
name - Name of this initialization parameter

addContextParameter

public void addContextParameter(String name,
                                String value)
Add a new servlet context parameter for this servlet.

Parameters:
name - Name of this initialization parameter to add
value - Value of this initialization parameter to add

addFilter

public void addFilter(javax.servlet.Filter filter,
                      String filterName,
                      Map initParameters)
Add a Filter to the ServletAdapter.FilterChainImpl

Parameters:
filter - an instance of Filter
filterName - the Filter's name
initParameters - the Filter init parameters.

getServletInstance

public javax.servlet.Servlet getServletInstance()
Return the Servlet instance used by this ServletAdapter

Returns:
Servlet isntance.

setServletInstance

public void setServletInstance(javax.servlet.Servlet servletInstance)
Set the Servlet instance used by this ServletAdapter

Parameters:
servletInstance - an instance of Servlet.

getServletPath

public String getServletPath()
Returns the part of this request's URL that calls the servlet. This path starts with a "/" character and includes either the servlet name or a path to the servlet, but does not include any extra path information or a query string. Same as the value of the CGI variable SCRIPT_NAME.

This method will return an empty string ("") if the servlet used to process this request was matched using the "/*" pattern.

Returns:
a String containing the name or path of the servlet being called, as specified in the request URL, decoded, or an empty string if the servlet used to process the request is matched using the "/*" pattern.

setServletPath

public void setServletPath(String servletPath)
Programmatically set the servlet path of the Servlet.

Parameters:
servletPath - Path of Servlet.

getContextPath

public String getContextPath()
Returns the portion of the request URI that indicates the context of the request. The context path always comes first in a request URI. The path starts with a "/" character but does not end with a "/" character. For servlets in the default (root) context, this method returns "". The container does not decode this string.

It is possible that a servlet container may match a context by more than one context path. In such cases this method will return the actual context path used by the request and it may differ from the path returned by the ServletContext.getContextPath() method. The context path returned by ServletContext.getContextPath() should be considered as the prime or preferred context path of the application.

Returns:
a String specifying the portion of the request URI that indicates the context of the request
See Also:
ServletContext.getContextPath()

setContextPath

public void setContextPath(String contextPath)
Programmatically set the context path of the Servlet.

Parameters:
contextPath - Context path.

addServletListener

public void addServletListener(String listenerName)
Add Servlet listeners that implement EventListener

Parameters:
listenerName - name of a Servlet listener

removeServletListener

public boolean removeServletListener(String listenerName)
Remove Servlet listeners that implement EventListener

Parameters:
listenerName - name of a Servlet listener to remove

getProperty

public Object getProperty(String name)
Return a configured property. Property apply to ServletContextImpl and ServletConfigImpl

Parameters:
name - Name of property to get.
Returns:
Value of property.

setProperty

public void setProperty(String name,
                        Object value)
Set a configured property. Property apply to ServletContextImpl and ServletConfigImpl. Use this method to map what's you usually have in a web.xml like display-name, context-param, etc.

Parameters:
name - Name of the property to set
value - of the property.

removeProperty

public void removeProperty(String name)
Remove a configured property. Property apply to ServletContextImpl and ServletConfigImpl

Parameters:
name - Property name to remove.

isLoadOnStartup

public boolean isLoadOnStartup()
Returns:
is the servlet will be loaded at startup

destroy

public void destroy()
Destroy this Servlet and its associated ServletContextListener

Overrides:
destroy in class GrizzlyAdapter

newServletAdapter

public ServletAdapter newServletAdapter(javax.servlet.Servlet servlet)
Create a new ServletAdapter instance that will share the same ServletContextImpl and Servlet's listener but with an empty map of init-parameters.

Parameters:
servlet - - The Servlet associated with the ServletAdapter
Returns:
a new ServletAdapter

getName

public String getName()
Overrides:
getName in class GrizzlyAdapter

setDispatcherHelper

protected void setDispatcherHelper(DispatcherHelper dispatcherHelper)
Overrides:
setDispatcherHelper in class GrizzlyAdapter

getServletCtx

protected ServletContextImpl getServletCtx()

getListeners

protected List<String> getListeners()

getContextParameters

protected Map<String,String> getContextParameters()

getClassLoader

public ClassLoader getClassLoader()

setClassLoader

public void setClassLoader(ClassLoader classLoader)

addFilter

protected void addFilter(FilterConfigImpl filterConfig)
Add a filter to the set of filters that will be executed in this chain.

Parameters:
filterConfig - The FilterConfig for the servlet to be executed


Copyright © 2013 Oracle Corporation. All Rights Reserved.