2.17.2: 2011-06-17

net.sf.basedb.clients.web.extensions
Class ServletLoader

java.lang.Object
  extended by net.sf.basedb.clients.web.extensions.ServletLoader

public class ServletLoader
extends Object

Loads servlet definitions from an XML file. The file format is a simplified version of the format allowed for web.xml files in Tomcat. We have kept only the <servlet> tag and it's subtags.

Version:
2.7
Author:
nicklas
Last modified
$Date: 2008-09-11 22:05:50 +0200 (Thu, 11 Sep 2008) $

Field Summary
private static String namespace
          The name of the servlets namespace.
private static String schemaFileURL
          The URL pointing to the servlets.xsd schema.
private  List<ServletWrapper> servlets
           
private  Document validatedDom
           
 
Constructor Summary
ServletLoader()
          Create a new XML loader instance.
 
Method Summary
 List<ServletWrapper> getServlets()
          Get a list with all loaded servlet definitions.
 boolean hasValidFile()
          Checks if an XML file has passed validation in the validateXmlFile(InputStream, String) method.
protected  Document loadDocument(InputStream xmlFile, String filename)
          Load and validate the XML file.
 void loadLastValidatedFile(ClassLoader classLoader, boolean clear)
          Continue loading servlet definitions from the last validated XML file.
protected  Class<? extends Servlet> loadServletClass(String className, ClassLoader classLoader)
          Load the servlet class.
protected  int loadServlets(Document dom, ClassLoader classLoader)
          Load the servlet definitions from an XML document.
 void loadXmlFile(InputStream xmlFile, String filename, ClassLoader classLoader, boolean clear)
          Load a servlet definition XML file.
 void validateXmlFile(InputStream xmlFile, String filename)
          Validate an XML file against the servlet definition schema.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

schemaFileURL

private static final String schemaFileURL
The URL pointing to the servlets.xsd schema.


namespace

private static final String namespace
The name of the servlets namespace.

See Also:
Constant Field Values

validatedDom

private Document validatedDom

servlets

private List<ServletWrapper> servlets
Constructor Detail

ServletLoader

public ServletLoader()
Create a new XML loader instance.

Method Detail

loadXmlFile

public void loadXmlFile(InputStream xmlFile,
                        String filename,
                        ClassLoader classLoader,
                        boolean clear)
                 throws JDOMException,
                        IOException,
                        ClassNotFoundException,
                        NoSuchMethodException,
                        IllegalAccessException
Load a servlet definition XML file. This method may be called multiple times with different XML files. The loading may also be done by a two-step process where the first step validates the file and the second step loads the extensions. See validateXmlFile(InputStream, String) and loadLastValidatedFile(ClassLoader, boolean).

Parameters:
xmlFile - An input stream to read the XML data from
filename - The original filename the stream is coming from, or null if not known. This value is only used when generating error messages
classLoader - The classloader to use when loading classes that are named in the XML file, or null to use the default classloader (=the same class loader that loaded the BASE core classes)
clear - TRUE to clear all already loaded servlets before loading the servlets in this file
Throws:
JDOMException - If validation of the XML file fails
IOException - If there is an error reading the XML file
ClassNotFoundException - If a servlet class named in the XML file can't be found
NoSuchMethodException - If a servlet class doesn't implement a public, no-argument constructor
IllegalAccessException - If a servlet class constructor isn't public
See Also:
validateXmlFile(InputStream, String), loadLastValidatedFile(ClassLoader, boolean)

validateXmlFile

public void validateXmlFile(InputStream xmlFile,
                            String filename)
                     throws IOException,
                            JDOMException
Validate an XML file against the servlet definition schema. If the file is valid you may continue to load the servlet definitions. See loadLastValidatedFile(ClassLoader, boolean).

Parameters:
xmlFile - An input stream to read the XML data from
filename - The original filename the stream is coming from, or null if not known. This value is only used when generating error messages
Throws:
JDOMException - If validation of the XML file fails
IOException - If there is an error reading the XML file

loadLastValidatedFile

public void loadLastValidatedFile(ClassLoader classLoader,
                                  boolean clear)
                           throws ClassNotFoundException,
                                  IllegalAccessException,
                                  NoSuchMethodException
Continue loading servlet definitions from the last validated XML file. This is the second step in a two-step process and requires that validateXmlFile(InputStream, String) has been successfully called first.

Parameters:
classLoader - The classloader to use when loading classes that are named in the XML file, or null to use the default classloader (=the same class loader that loaded the BASE core classes)
clear - TRUE to clear all already loaded servlets before loading the servlets in this file
Throws:
ClassNotFoundException - If a servlet class named in the XML file can't be found
NoSuchMethodException - If a servlet class doesn't implement a public, no-argument constructor
IllegalAccessException - If a servlet class constructor isn't public

hasValidFile

public boolean hasValidFile()
Checks if an XML file has passed validation in the validateXmlFile(InputStream, String) method. If so, the loadLastValidatedFile(ClassLoader, boolean) can be called to continue loading the servlet definitions.

Note that once the file has been loaded this flag is reset to FALSE.

Returns:
TRUE if a file has been validated, FALSE otherwise

getServlets

public List<ServletWrapper> getServlets()
Get a list with all loaded servlet definitions.


loadDocument

protected Document loadDocument(InputStream xmlFile,
                                String filename)
                         throws IOException,
                                JDOMException
Load and validate the XML file.

Returns:
A JDOM document object
Throws:
IOException
JDOMException

loadServlets

protected int loadServlets(Document dom,
                           ClassLoader classLoader)
                    throws ClassNotFoundException,
                           NoSuchMethodException,
                           IllegalAccessException
Load the servlet definitions from an XML document.

Throws:
ClassNotFoundException
NoSuchMethodException
IllegalAccessException

loadServletClass

protected Class<? extends Servlet> loadServletClass(String className,
                                                    ClassLoader classLoader)
                                             throws ClassNotFoundException,
                                                    NoSuchMethodException,
                                                    IllegalAccessException
Load the servlet class. The class must implement the Servlet interface and have a public, no-argument contstructor

Parameters:
className - The name of the servlet class
classLoader - The classloader to use, or null to use the BASE core classloader
Returns:
An initialised factory
Throws:
ClassNotFoundException
NoSuchMethodException
IllegalAccessException

2.17.2: 2011-06-17