org.locomotive.loco.servlet
Class LocoServletContext

java.lang.Object
  |
  +--org.locomotive.loco.servlet.LocoServletContext

public class LocoServletContext
extends java.lang.Object
implements javax.servlet.ServletContext

This class is instantiated for each servlet that the Locomotive initializes. An instance should be created when a servlet is initialized, and it is maintained throughout the life of the servlet.


Constructor Summary
LocoServletContext(ServletRoutingTable servlet_routing_table)
           
 
Method Summary
 java.lang.Object getAttribute(java.lang.String name)
          Returns the value of the named attribute of the network service, or null if the attribute does not exist.
 java.util.Enumeration getAttributeNames()
          returns an enumeration of all the attribute names in the server.
 javax.servlet.ServletContext getContext(java.lang.String uri)
          Returns the ServletContext for the specified uri.
 int getMajorVersion()
          Returns the major version of the Servlet API this engine implements.
 java.lang.String getMimeType(java.lang.String file)
          returns the mime type for a particular file.
 int getMinorVersion()
          Returns the minor version of the Servlet API this engine implements.
 java.lang.String getRealPath(java.lang.String path)
          Returns a String containing the real path that corresponds to a virtual path.
 javax.servlet.RequestDispatcher getRequestDispatcher(java.lang.String uripath)
          This returns a requestDispatcher object that will allow forwarding to another servlet or handler.
 java.net.URL getResource(java.lang.String uri_path)
          Returns a URL to the resource that is mapped to a specified path.
 java.io.InputStream getResourceAsStream(java.lang.String uri_path)
          Returns the resource located at the named path as an InputStream object.
 java.lang.String getServerInfo()
          Returns the following information about the current server, in the following format:
 javax.servlet.Servlet getServlet(java.lang.String name)
          Deprecated. - too dangerous
 java.util.Enumeration getServletNames()
          Deprecated. because it's too dangerous
 ServletRoutingTable getServletRoutingTable()
           
 java.util.Enumeration getServlets()
          Deprecated. because it's too dangerous
 void log(java.lang.Exception e, java.lang.String msg)
          Logs the message and the stack trace at log level 0
 void log(java.lang.String msg)
          Currently logs to the server log- log level 5
 void log(java.lang.String msg, java.lang.Throwable t)
          Logs the message and the stack trace at log level 0
 void removeAttribute(java.lang.String name)
          Removes a server attribute.
 void setAttribute(java.lang.String name, java.lang.Object o)
          Sets a server attribute.
 void setServletRoutingTable(ServletRoutingTable servlet_routing_table)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LocoServletContext

public LocoServletContext(ServletRoutingTable servlet_routing_table)
Method Detail

getAttribute

public java.lang.Object getAttribute(java.lang.String name)
Returns the value of the named attribute of the network service, or null if the attribute does not exist. This looks first for any servlet specific attributes, then looks in the main server config object.
Specified by:
getAttribute in interface javax.servlet.ServletContext

getAttributeNames

public java.util.Enumeration getAttributeNames()
returns an enumeration of all the attribute names in the server. Presenly returns an enumeration of all the config names in the main server config, as well as any attributes added by any servlets since the server started up.
Specified by:
getAttributeNames in interface javax.servlet.ServletContext

setAttribute

public void setAttribute(java.lang.String name,
                         java.lang.Object o)
Sets a server attribute. Does not do so persistently, however, and the change is recognized only by other servlets, not by the main servlet config. To change the main server config object, use Loco.getConfig().changeConfig().
Specified by:
setAttribute in interface javax.servlet.ServletContext

removeAttribute

public void removeAttribute(java.lang.String name)
Removes a server attribute. Only removes attributes which are not part of the main server however- attributes which are part of the main server cannot be removed. Removing main server attributes will cause those attributes to be reset to the values that were in the file when the Locomotive was started up.
Specified by:
removeAttribute in interface javax.servlet.ServletContext

getContext

public javax.servlet.ServletContext getContext(java.lang.String uri)
Returns the ServletContext for the specified uri. Presently this will always return this object
Specified by:
getContext in interface javax.servlet.ServletContext

getMajorVersion

public int getMajorVersion()
Returns the major version of the Servlet API this engine implements.
Specified by:
getMajorVersion in interface javax.servlet.ServletContext

getMinorVersion

public int getMinorVersion()
Returns the minor version of the Servlet API this engine implements.
Specified by:
getMinorVersion in interface javax.servlet.ServletContext

getMimeType

public java.lang.String getMimeType(java.lang.String file)
returns the mime type for a particular file. Currently just uses a bunch of extension heruristics.
Specified by:
getMimeType in interface javax.servlet.ServletContext

getRealPath

public java.lang.String getRealPath(java.lang.String path)
Returns a String containing the real path that corresponds to a virtual path. A virtual path contains a servlet name followed by the name of a file the servlet should act upon, in the form /dir/dir/servlet/file.ext. In this form, file.ext is a filename used instead of the path to the file. The servlet locates the file and translates the file name to the path that locates the file.

The real path the servlet returns is in a form appropriate to the computer and operating system on which the servlet engine is running, including the proper path separators. This method returns null if the servlet engine cannot translate the virtual path to a real path for any reason.

Specified by:
getRealPath in interface javax.servlet.ServletContext
Parameters:
path - a String specifying a virtual path, in the form /dir/dir/servlet/file.ext
Returns:
a String specifying the real path, with path separators appropriate for the system on which the servlet engine is running.

getResource

public java.net.URL getResource(java.lang.String uri_path)
                         throws java.net.MalformedURLException
Returns a URL to the resource that is mapped to a specified path. The path must begin with a "/" and is interpreted as relative to the current context root.

This method allows the servlet container to make a resource available to servlets from any source. Resources can be located on a local or remote file system, in a database, or in a .war file.

The servlet container must implement the URL handlers and URLConnection objects that are necessary to access the resource.

Currently, there are no custom URLStreamHandlers nor URLConnection classes that come with the Locomotive. There are, however, at least a couple "standard" ones that the Locomotive can use that come with the JDK (or JRE). These include URLStreamHandlers for reading local files, files stored in .zip or .jar archive files (including the Servlet 2.2 .war files), HTTP-accessable files, FTP-able files. Not all JDKs or JREs come with all of these, and it's rather tough to find out exactly which ones are available to any given JDK/JRE. It looks as if file and http URLStreamHandlers are always available, though, so we should at least be able to use those.

This method returns null if no resource is mapped to the pathname.

Some containers may allow writing to the URL returned by this method using the methods of the URL class. Currently, the Locomotive supports this as long as the URLStreamHandler you try it on supports writing.

The resource content is returned directly, so be aware that requesting a .jsp page returns the JSP source code. Use a RequestDispatcher instead to include results of an execution.

This method has a different purpose than java.lang.Class.getResource, which looks up resources based on a class loader. This method does not use class loaders.

Specified by:
getResource in interface javax.servlet.ServletContext
Parameters:
uri_path - a String specifying the path to the resource (like "/dir/dir/file.ext")
Returns:
the resource located at the named path, or null if there is no resource at that path
Throws:
java.net.MalformedURLException - if the pathname is not given in the correct form

getResourceAsStream

public java.io.InputStream getResourceAsStream(java.lang.String uri_path)
Returns the resource located at the named path as an InputStream object.

The data in the InputStream can be of any type or length. The path must be specified according to the rules given in getResource. This method returns null if no resource exists atthe specified path.

Meta-information such as content length and content type that is available via getResource method is lost when using this method.

The servlet container must implement the URL handlers and URLConnection objects necessary to access the resource.

This method is different from java.lang.Class.getResourceAsStream, which uses a class loader. This method allows servlet containers to make a resource available to a servlet from any location, without using a class loader.

Specified by:
getResourceAsStream in interface javax.servlet.ServletContext
Parameters:
uri_path - a String specifying the path to the resource (like "/dir/dir/file.ext")
Returns:
the InputStream taken from the URLConnection to the resource, or null if no resource exists at the specified path.

getRequestDispatcher

public javax.servlet.RequestDispatcher getRequestDispatcher(java.lang.String uripath)
This returns a requestDispatcher object that will allow forwarding to another servlet or handler.
Specified by:
getRequestDispatcher in interface javax.servlet.ServletContext
Parameters:
uripath - the path that denotes the Service to dispatch the request to

getServerInfo

public java.lang.String getServerInfo()
Returns the following information about the current server, in the following format:
 SYSTEM_TAG: SYSTEM_INSTANCE_TAG, Locomotive v (loco version), 
 Servlet engine v (major version).(minor version).
 
Here's an example:
 COMPANY_X: sv_one, Locomotive v4.1.0, Servlet Engine v2.1
 
Specified by:
getServerInfo in interface javax.servlet.ServletContext

log

public void log(java.lang.String msg)
Currently logs to the server log- log level 5
Specified by:
log in interface javax.servlet.ServletContext

log

public void log(java.lang.String msg,
                java.lang.Throwable t)
Logs the message and the stack trace at log level 0
Specified by:
log in interface javax.servlet.ServletContext

log

public void log(java.lang.Exception e,
                java.lang.String msg)
Logs the message and the stack trace at log level 0
Specified by:
log in interface javax.servlet.ServletContext

getServlet

public javax.servlet.Servlet getServlet(java.lang.String name)
Deprecated. - too dangerous

returns a servlet keyed by the name. Deemed too dangerous by the new Servlet API- always returns null.
Specified by:
getServlet in interface javax.servlet.ServletContext

getServletNames

public java.util.Enumeration getServletNames()
Deprecated. because it's too dangerous

returns the names of all the servlets presently registered. Also consided too dangerous. Returns an empty enumeration.
Specified by:
getServletNames in interface javax.servlet.ServletContext

getServlets

public java.util.Enumeration getServlets()
Deprecated. because it's too dangerous

returns an enumeration of the registered servlets. Deemed too dangerous. Returns an empty enumeration.
Specified by:
getServlets in interface javax.servlet.ServletContext

getServletRoutingTable

public ServletRoutingTable getServletRoutingTable()

setServletRoutingTable

public void setServletRoutingTable(ServletRoutingTable servlet_routing_table)