org.mmbase.servlet
Class MMBaseServlet

Package class diagram package MMBaseServlet
java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
          extended by org.mmbase.servlet.MMBaseServlet
All Implemented Interfaces:
Serializable, Servlet, ServletConfig, MMBaseStarter
Direct Known Subclasses:
BridgeServlet

public class MMBaseServlet
extends HttpServlet
implements MMBaseStarter

MMBaseServlet is a base class for other MMBase servlets (like ImageServlet). Its main goal is to store a MMBase instance for all its descendants, but it can also be used as a serlvet itself, to show MMBase version information.

Since:
MMBase-1.6
Version:
$Id: MMBaseServlet.java 46064 2011-11-23 13:16:46Z michiel $
Author:
Michiel Meeuwissen
See Also:
Serialized Form

Field Summary
protected  MMBase mmbase
          MMBase reference.
protected  int retryAfter
          If MMBase has not been started, a 503 is given, with this value for the 'Retry-After' header.
 
Constructor Summary
MMBaseServlet()
           
 
Method Summary
protected static void associateMapping(String function, String servletMapping, Integer priority)
          Associate a given servlet mapping with the given function.
protected  boolean checkInited(HttpServletResponse res)
          This methods can be (and is) called in the beginning of service.
protected  void decRefCount(HttpServletRequest req)
          Decrease the reference count of the servlet
 void destroy()
           
protected  void doGet(HttpServletRequest req, HttpServletResponse res)
           Default MMBaseServlet implementation serves MMBase version information.
protected  Map<String,Integer> getAssociations()
          On default, servlets are not associated with any function.
static String getBasePath(String function)
           
 MMBase getMMBase()
          Returns the MMBase instance.
protected static String getRequestURL(HttpServletRequest req)
          Return URI with QueryString appended
static String getServletByAssociation(String function)
          Gets the name of the servlet that performs actions associated with the the given function.
static HttpServlet getServletByMapping(String mapping)
          Gets the servlet that belongs to the given mapping
 String getServletInfo()
          Returns information about this servlet.
static List<String> getServletMappings(String servletName)
          Gets all the mappings for a given servlet.
static List<String> getServletMappingsByAssociation(String function)
          Gets all the mappings for a given association.
protected  void incRefCount(HttpServletRequest req)
          Increase the reference count of the servlet (for debugging) and send running servlets to log once every 32 requests
 void init()
          The init of an MMBaseServlet checks if MMBase is running.
 void service(HttpServletRequest req, HttpServletResponse res)
          The service method is extended with calls for the refCount functionality (for performance related debugging).
 void setInitException(ServletException e)
          Called by MMBaseStartThread, if something went wrong during initialization of MMBase.
 void setMMBase(MMBase mmb)
          Sets the mmbase member.
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service
 
Methods inherited from class javax.servlet.GenericServlet
getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

mmbase

protected MMBase mmbase
MMBase reference. While null, servlet does not accept request.


retryAfter

protected int retryAfter
If MMBase has not been started, a 503 is given, with this value for the 'Retry-After' header. See rfc 2616, section 10.5.4. Defaults to 60 seconds, can be configured in web.xml with the 'retry-after' propery on the servlets.

Since:
MMBase-1.7.2
Constructor Detail

MMBaseServlet

public MMBaseServlet()
Method Detail

getAssociations

protected Map<String,Integer> getAssociations()
On default, servlets are not associated with any function. This function is called in the init method.

Returns:
A map of Strings (function) -> Integer (priority). Never null.

getMMBase

public MMBase getMMBase()
Returns the MMBase instance.

Specified by:
getMMBase in interface MMBaseStarter
Returns:
the MMBase instance or null.
Since:
MMBase-1.7

setMMBase

public void setMMBase(MMBase mmb)
Sets the mmbase member. Can be overriden to implement extra initalization for the servlet which needs a running MMBase.

Specified by:
setMMBase in interface MMBaseStarter
Since:
MMBase-1.7

setInitException

public void setInitException(ServletException e)
Called by MMBaseStartThread, if something went wrong during initialization of MMBase. It will be thrown by checkInited then.

Specified by:
setInitException in interface MMBaseStarter
Since:
MMBase-1.7

init

public void init()
          throws ServletException
The init of an MMBaseServlet checks if MMBase is running. It not then it is started.

Overrides:
init in class GenericServlet
Throws:
ServletException

getServletByMapping

public static HttpServlet getServletByMapping(String mapping)
Gets the servlet that belongs to the given mapping

Parameters:
mapping - the mapping used to access the servlet
Returns:
the Servlet that handles the mapping

getServletMappings

public static List<String> getServletMappings(String servletName)
Gets all the mappings for a given servlet. So, this is a method to obtain info from web.xml.

Parameters:
servletName - the name of the servlet
Returns:
an unmodifiable list of servlet mappings for this servlet

getServletMappingsByAssociation

public static List<String> getServletMappingsByAssociation(String function)
Gets all the mappings for a given association. Use this to find out how to call a servlet to handle a certain type of operation or data (i.e 'images', 'attachments').

Parameters:
function - the function that identifies the type of association
Returns:
an unmodifiable list of servlet mappings associated with the function

getServletByAssociation

public static String getServletByAssociation(String function)
Gets the name of the servlet that performs actions associated with the the given function. Use this to find a servlet to handle a certain type of operation or data (i.e 'imageservlet', 'myimageservlet', 'images');

Parameters:
function - the function that identifies the type of association
Returns:
the name of the servlet associated with the function, or null if there is none

getBasePath

public static String getBasePath(String function)
Since:
MMBase-1.8.5

associateMapping

protected static void associateMapping(String function,
                                       String servletMapping,
                                       Integer priority)
Associate a given servlet mapping with the given function. Use this to set a servlet mapping to call for a certain type of operation or data (i.e 'image-processing'); For now, only one servlet mapping can be registered.

Parameters:
function - the function that identifies the type of association
servletMapping - mapping of the servlet to associate with the function
priority - priority of this association, the association only occurs if no servlet or servletmapping with higher priority for the same function is present already

doGet

protected void doGet(HttpServletRequest req,
                     HttpServletResponse res)
              throws ServletException,
                     IOException
Default MMBaseServlet implementation serves MMBase version information. Any other MMBase servlet will probably override this method.

Overrides:
doGet in class HttpServlet
Throws:
ServletException
IOException

checkInited

protected boolean checkInited(HttpServletResponse res)
                       throws ServletException,
                              IOException
This methods can be (and is) called in the beginning of service. It sends an UNAVAILABLE error if MMBase has not bee started, or throws an exeption if that was unsuccessful.

Returns:
A boolean. If false, then service must return immediately (because mmbase has not been inited yet).
Throws:
ServletException
IOException
Since:
MMBase-1.7.2

service

public void service(HttpServletRequest req,
                    HttpServletResponse res)
             throws ServletException,
                    IOException
The service method is extended with calls for the refCount functionality (for performance related debugging). So you can simply override doGet in extension classes, and this stays working, without having to think about it.

Overrides:
service in class HttpServlet
Throws:
ServletException
IOException

getServletInfo

public String getServletInfo()
Returns information about this servlet. Don't forget to override it.

Specified by:
getServletInfo in interface Servlet
Overrides:
getServletInfo in class GenericServlet

getRequestURL

protected static String getRequestURL(HttpServletRequest req)
Return URI with QueryString appended

Parameters:
req - The HttpServletRequest.

decRefCount

protected void decRefCount(HttpServletRequest req)
Decrease the reference count of the servlet

Parameters:
req - The HttpServletRequest.

incRefCount

protected void incRefCount(HttpServletRequest req)
Increase the reference count of the servlet (for debugging) and send running servlets to log once every 32 requests

Parameters:
req - The HttpServletRequest.
Bad-constant:
31 should be configurable.

destroy

public void destroy()
Specified by:
destroy in interface Servlet
Overrides:
destroy in class GenericServlet


MMBase2 Core 2.0-SNAPSHOT - 2014-09-22T23:08