| < Day Day Up > |
|
Struts Plugins are modular extensions to the Struts Controller. Introduced in Struts 1.1, they are defined by the org.apache.struts.action.Plugin interface. Struts Plugins are useful when you're allocating resources or preparing connections to databases or even JNDI resources. We look at an example of loading application properties on startup later in this section.
This interface, like the Java Servlet architecture, defines two methods that must be implemented by all used-defined Plugins: init() and destroy(). These are the lifecycle methods of a Struts Plugin.
The current Struts Project 1.1 is prepackaged with a couple of good Plugin examples: org.apache.struts.tiles.TilesPlugin, which is used to initialize the Tiles framework, and org.apache.struts.validator.ValidatorPlugin, which initializes the Commons Validator.
The init() method of a Struts Plugin is called when the JSP/Servlet container starts the Struts Web application containing the Plugin. It has the following signature:
public void init(ActionServlet servlet, ApplicationConfig applicationConfig) throws javax.servlet.ServletException;
This method is used to load and initialize resources that are required by your Plugin. Notice that the init() method receives a reference to the ActionServlet and the ApplicationConfig when invoked. The ActionServlet reference allows you to reference any Controller information, and the ApplicationConfig object provides access to the configuration information describing a Struts application. The init() method marks the beginning of a Plugin's life.
The destroy() method of a Struts Plugin is called whenever the JSP/Servlet container stops the Struts Web application containing the Plugin. It has the following signature:
public void destroy();
This method is convenient when you're reclaiming or closing resources that were allocated in the Plugin.init() method. This method marks the end of a Plugin's life.
Now that we have discussed what a Plugin is, let's look at an example Plugin implementation. As we stated earlier, all Plugins must implement the two Plugin methods init() and destroy(). To develop your own Plugin, you must complete the following steps. These steps describe the minimum actions that you must complete when creating a new Plugin:
Create a class that implements the org.apache.struts.action.Plugin interface.
Add a default empty constructor to the Plugin implementation. You must have a default constructor to ensure that the ActionServlet properly creates your Plugin.
Implement both the init() and destroy() methods and your implementation.
Compile the new Plugin and move it into the Web application's classpath.
Add a <plug-in> element to the application's struts-config.xml file describing the new Plugin. We look at this step in the next section.
Listing 4.1 contains an example Plugin implementation.
Listing 4.1: WroxPlugin.java.
package ch04; import java.util.Properties; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.ServletContext; import org.apache.struts.action.PlugIn; import org.apache.struts.config.ApplicationConfig; import org.apache.struts.action.ActionServlet; public class WroxPlugin implements PlugIn { public static final String PROPERTIES = "PROPERTIES"; public WroxPlugin() { } public void init(ActionServlet servlet, ApplicationConfig applicationConfig) throws javax.servlet.ServletException { System.err.println("---->The Plugin is starting<----"); Properties properties = new Properties(); try { // Build a file object referening the properties file // to be loaded File file = new File("PATH TO PROPERTIES FILE"); // Create an input stream FileInputStream fis = new FileInputStream(file); // load the properties properties.load(fis); // Get a reference to the ServletContext ServletContext context = servlet.getServletContext(); // Add the loaded properties to the ServletContext // for retrieval throughout the rest of the Application context.setAttribute(PROPERTIES, properties); } catch (FileNotFoundException fnfe) { throw new ServletException(fnfe.getMessage()); } catch (IOException ioe) { throw new ServletException(ioe.getMessage()); } } public void destroy() { // We don't have anything to clean up, so // just log the fact that the Plugin is shutting down System.err.println ("----->The Plugin is stopping<----"); } }
As you look over Listing 4.1, you will see just how straightforward Plugin development can be. This example shows a simple Plugin that implements the init() method (which contains the property-loading logic) and the destroy() method (which cleans up after the init() method). The purpose of this Plugin is to make a set of properties available upon application startup. Next, let's see how you can make the ch04.WroxPlugin available to your Struts application.
In this section, we show you how to deploy and configure a Plugin. For this example, let's use our application from Chapter 3 as the host of our new Plugin. To deploy and configure our ch04.WroxPlugin, you must:
Compile and move the Plugin class file into your application's WEB-INF/classes/ch04/ directory.
Add a <plug-in> element to your struts-config.xml file. An example <plug-in> entry, describing the previously defined Plugin, is shown in the following code snippet:
<plug-in className="ch04.WroxPlugin"/>
The <plug-in> element should be the last element in the struts-config.xml.
Restart the Struts Web application.
You can find this complete application in the ch04/src/ directory of this text's source distribution.
When this deployment is complete, this Plugin will begin its life when the hosting application restarts--you should be able to see its output in the console window of your Tomcat instance.
| < Day Day Up > |
|