com.taursys.servlet
Class ServletFormFactory

java.lang.Object
  extended bycom.taursys.servlet.ServletFormFactory

public class ServletFormFactory
extends java.lang.Object

This Factory creates and recycles servlet forms for an application. This class was designed to be used by a ServletApp. It will typically be setup in the init method of the ServletApp. Below is an example of typical usage:

 public class MyMainServlet extends ServletApp {

   public void init(ServletConfig config) throws ServletException {
     super.init(config);
     getFactory().addPackage("/","com.taursys.examples.simpleweb");
     getFactory().setDefaultFormName("com.taursys.examples.simpleweb.ShowHidePage");
     getFactory().setDefaultClassLoader(getClass().getClassLoader());
     // Set default logging
     Debug.setLoggerAdapter(new SimpleLogger(Debug.DEBUG));
   }
 }
 


Constructor Summary
ServletFormFactory()
          Creates a new ServletFormFactory with a defaultFormName of com.taursys.servlet.DefaultMessageForm.
 
Method Summary
 void addPackage(java.lang.String path, java.lang.String packageName)
          Adds the given path and packageName to the packages table.
 ServletForm createServletForm(java.lang.String url)
          Returns a ServletForm based on the given url.
 java.lang.ClassLoader getDefaultClassLoader()
          Get the default ClassLoader used to load and instantiate ServletForms.
 java.lang.String getDefaultFormName()
          Gets the default ServletForm name for this factory.
 java.lang.String getServletFormSuffix()
          Gets the current suffix for ServletForms.
 java.lang.String parseClassName(java.lang.String url)
          Parses given url and constructs fully qualified ServletForm class name.
 void recycle(ServletForm usedServletForm)
          Recycle will put given servletForm back into pool if servletForm supports recycling.
 void setDefaultClassLoader(java.lang.ClassLoader newDefaultClassLoader)
          Set the default ClassLoader used to load and instantiate ServletForms.
 void setDefaultFormName(java.lang.String newDefaultFormName)
          Sets the default ServletForm name for this factory.
 void setServletFormSuffix(java.lang.String newServletFormSuffix)
          Sets the suffix for ServletForms.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ServletFormFactory

public ServletFormFactory()
Creates a new ServletFormFactory with a defaultFormName of com.taursys.servlet.DefaultMessageForm. Also sets the defaultClassLoader to this class's classLoader

Method Detail

createServletForm

public ServletForm createServletForm(java.lang.String url)
                              throws ServletFormNotFoundException
Returns a ServletForm based on the given url. This method will first see if there are any recycled forms in the servletForms pool. If so, it will remove that form from the pool and return it. If none are in the pool, it will create a new one. This method depends on the parseClassName method to determine the fully qualified class name of the ServletForm based on the given url.

Parameters:
url - containing the encoded ServletForm name
Returns:
a ServletForm of the specific class requested in the url
Throws:
ServletFormNotFoundException - if cannot parse url, package is not registered, or ServletForm class is not found.

recycle

public void recycle(ServletForm usedServletForm)
Recycle will put given servletForm back into pool if servletForm supports recycling. The servletForm's recycle method returns true if it supports recycling.

Parameters:
usedServletForm - to recycle (if it supports recycling)

parseClassName

public java.lang.String parseClassName(java.lang.String url)
                                throws ServletFormNotFoundException
Parses given url and constructs fully qualified ServletForm class name. Returns the defaultFormName if the given url is null or "/". The url is broken into 2 parts: path and form name. The package name is retrieved from the packages table using the path as the key. The class name is returned as the package name plus the form name (without the servletFormSuffix). Examples:

Assuming 2 entries in package table:

Example translations:

Parameters:
url - path to parse - usually obtained through request.getPathInfo()
Returns:
fully qualified class name string
Throws:
ServletFormNotFoundException - if path not found or form does not end in servletFormSuffix.

addPackage

public void addPackage(java.lang.String path,
                       java.lang.String packageName)
Adds the given path and packageName to the packages table. The packages table is used in the parseClassName method to determine the fully qualified class name for a given path/ServletFormName.

Parameters:
path - that form names will be registered under
packageName - full package name for forms in given path

setDefaultFormName

public void setDefaultFormName(java.lang.String newDefaultFormName)
Sets the default ServletForm name for this factory. This must be a fully qualified class name.

Parameters:
newDefaultFormName - for this factory

getDefaultFormName

public java.lang.String getDefaultFormName()
Gets the default ServletForm name for this factory.

Returns:
the defaultFormName for this factory

setServletFormSuffix

public void setServletFormSuffix(java.lang.String newServletFormSuffix)
Sets the suffix for ServletForms. The default is ".sf". The suffix cannot be null, but it can be blank if no suffix is wanted.

Parameters:
newServletFormSuffix - the suffix to use
Throws:
java.lang.IllegalArgumentException - if passed a null value.

getServletFormSuffix

public java.lang.String getServletFormSuffix()
Gets the current suffix for ServletForms. The default is ".sf".

Returns:
the current suffix for ServletForms.

setDefaultClassLoader

public void setDefaultClassLoader(java.lang.ClassLoader newDefaultClassLoader)
Set the default ClassLoader used to load and instantiate ServletForms. This loader is initially set to the ClassLoader for this class. Your ServletApp may need to set this property if the MapperXML classes are loaded from a different ClassLoader than your application classes. If you deploy your web application with mapperxml.jar in the /WEB-INF/lib directory, then you will probably not need to set this property. You will most commonly set this property within your ServletApp's init method (example):
   getFactory().setDefaultClassLoader(getClass().getClassLoader());
 

Parameters:
newDefaultClassLoader - to use to load and instantiate ServletForms.

getDefaultClassLoader

public java.lang.ClassLoader getDefaultClassLoader()
Get the default ClassLoader used to load and instantiate ServletForms. This loader is initially set to the ClassLoader for this class.

Returns:
the default ClassLoader to use to load and instantiate ServletForms.


Copyright © 2007 Martin T Phelan. All Rights Reserved.