Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Building a Java servlet framework using reflection, Part 1

Reflective code provides more functionality in fewer lines of code.

  • Print
  • Feedback

Page 2 of 4

Similarly, it is possible to create objects to handle presentation-related issues using an interface similar to the following:

 public interface PresentationInterface
 {
    public void initializePresentation(ProxyInterface proxy) throws IOException;
    public void output(String text) throws IOException;
    public void finalizePresentation() throws IOException;
 }


Notice that this interface is implemented by any object responsible for presentation-related activities. Also notice the difference between the two interfaces. The PresentationInterface lists a method with a parameter called PresentationService. The PresentationServicesInterface is the key to the framework, as it is the controller in the framework.

The implementor of the ProxyInterface is the single point of contact for all platform-dependent functionality. The Presentation object does not need to understand the details of implementing the presentation layer. Whether it is through applets, servlets, or sockets, the methods of the interface must be satisfied.

The ProxyInterface shown below can obtain references for the other objects involved in the request-response pattern.

 public interface ProxyInterface
 {
    public DataInterface getDataObject(String name);
    public PresentationInterface getPresentationObject(String name); 
    public BusinessInterface getBusinessObject(String name);
 }


Because this interface is quite generic, it may be used as a proxy for requests made to servlets or other server-side applications, such as Netscape Application Server's AppLogics. The intention is to create a structure such that all the information specific to the deployment platform is contained in the implementation of the ProxyInterface, or in a small subset of classes. This allows developers to write code independent of proprietary methods or variables, thereby increasing the efficiency of the development team.

The last component of our expandable framework is the BusinessInterface:

 public interface BusinessInterface 
 {
    public void init(ProxyInterface proxy, Hashtable parameters) throws Exception;
 }


An implementation of this interface is spawned for every request made to the framework. This interface contains a method through which the input parameters pass from the servlet to the business object. In addition, the proxy is passed to the business object in order to provide it with the tools necessary to perform its task.

We have created a framework through which we can launch business objects to perform the individual actions of the users -- including all data and presentation issues -- without any knowledge of the individual platform upon which they reside. Now, how do we know which business object to load?

Dynamic business objects loading

The Java Reflection API provides an easy mechanism with which to execute this framework. In this first part of our series, Reflection itself is not used. For now, we will use the class.forName(...) method to load the appropriate business object.

    private static BusinessInterface loadBusinessObject(String actionName)
    {
        BusinessInterface bi = null;
        
        try
        {
            // attempt to retrieve the class from the class loader
            Class c = Class.forName(actionName + BUSINESS_OBJECT);
            
            // Initialize a class array with no elements (used in the constructor)
            Class[] parameterTypes = new Class[0];
            
            // Locate the constructor with no arguments
            Constructor constructor = c.getConstructor(parameterTypes);
 
            // Initialize an object array with no elements (used in the newInstance method)
            Object[] initargs = new Object[0];
            
            // Load the new instance of the business interface implementor
            bi = (BusinessInterface) constructor.newInstance(initargs);
 
        }
        catch (Exception e)
        {
            // If there is an error, create an ErrorBusinessObject (for now)
            bi = (BusinessInterface) new ErrorBusinessObject();
 
            e.printStackTrace();
        }
        
        return bi;
    }


This method is contained in the BusinessObjectFactory class and dynamically loads the business object based upon the action name provided by the proxy. This method attempts to load the business object and, if any exceptions occur, will create an object to handle the error conditions. It assumes a default constructor for this example, but can be tailored to suit any development efforts as needed.

  • Print
  • Feedback

Resources