FileDocCategorySizeDatePackage
MemoryDatabasePlugIn.javaAPI DocExample10213Sun Mar 07 19:48:42 GMT 2004org.apache.struts.webapp.example.memory

MemoryDatabasePlugIn

public final class MemoryDatabasePlugIn extends Object implements org.apache.struts.action.PlugIn

MemoryDatabasePlugIn initializes and finalizes the persistent storage of User and Subscription information for the Struts Demonstration Application, using an in-memory database backed by an XML file.

IMPLEMENTATION WARNING - If this web application is run from a WAR file, or in another environment where reading and writing of the web application resource is impossible, the initial contents will be copied to a file in the web application temporary directory provided by the container. This is for demonstration purposes only - you should NOT assume that files written here will survive a restart of your servlet container.

author
Craig R. McClanahan
version
$Revision: 1.8 $ $Date: 2003/02/17 00:31:45 $

Fields Summary
private org.apache.struts.config.ModuleConfig
config
The application configuration for our owning module.
private MemoryUserDatabase
database
The {@link MemoryUserDatabase} object we construct and make available.
private Log
log
Logging output for this plug in instance.
private org.apache.struts.action.ActionServlet
servlet
The {@link ActionServlet} owning this application.
private String
pathname
The web application resource path of our persistent database storage file.
Constructors Summary
Methods Summary
private java.lang.StringcalculatePath()
Calculate and return an absolute pathname to the XML file to contain our persistent storage information.

exception
Exception if an input/output error occurs


        // Can we access the database via file I/O?
        String path = servlet.getServletContext().getRealPath(pathname);
        if (path != null) {
            return (path);
        }

        // Does a copy of this file already exist in our temporary directory
        File dir = (File)
            servlet.getServletContext().getAttribute
            ("javax.servlet.context.tempdir");
        File file = new File(dir, "struts-example-database.xml");
        if (file.exists()) {
            return (file.getAbsolutePath());
        }

        // Copy the static resource to a temporary file and return its path
        InputStream is =
            servlet.getServletContext().getResourceAsStream(pathname);
        BufferedInputStream bis = new BufferedInputStream(is, 1024);
        FileOutputStream os =
            new FileOutputStream(file);
        BufferedOutputStream bos = new BufferedOutputStream(os, 1024);
        byte buffer[] = new byte[1024];
        while (true) {
            int n = bis.read(buffer);
            if (n <= 0) {
                break;
            }
            bos.write(buffer, 0, n);
        }
        bos.close();
        bis.close();
        return (file.getAbsolutePath());

    
public voiddestroy()
Gracefully shut down this database, releasing any resources that were allocated at initialization.


        log.info("Finalizing memory database plug in");

        if (database != null) {
            try {
                database.close();
            } catch (Exception e) {
                log.error("Closing memory database", e);
            }
        }

	servlet.getServletContext().removeAttribute(Constants.DATABASE_KEY);
        database = null;
        servlet = null;
        database = null;
        config = null;

    
public java.lang.StringgetPathname()


       
        return (this.pathname);
    
public voidinit(org.apache.struts.action.ActionServlet servlet, org.apache.struts.config.ModuleConfig config)
Initialize and load our initial database from persistent storage.

param
servlet The ActionServlet for this web application
param
config The ApplicationConfig for our owning module
exception
ServletException if we cannot configure ourselves correctly


        log.info("Initializing memory database plug in from '" +
                 pathname + "'");

        // Remember our associated configuration and servlet
        this.config = config;
        this.servlet = servlet;

        // Construct a new database and make it available
        database = new MemoryUserDatabase();
        try {
            String path = calculatePath();
            if (log.isDebugEnabled()) {
                log.debug(" Loading database from '" + path + "'");
            }
            database.setPathname(path);
            database.open();
        } catch (Exception e) {
            log.error("Opening memory database", e);
            throw new ServletException("Cannot load database from '" +
                                       pathname + "'", e);
        }

        // Make the initialized database available
        servlet.getServletContext().setAttribute(Constants.DATABASE_KEY,
                                                 database);

        // Setup and cache other required data
        setupCache(servlet, config);

    
public voidsetPathname(java.lang.String pathname)

        this.pathname = pathname;
    
protected voidsetupCache(org.apache.struts.action.ActionServlet servlet, org.apache.struts.config.ModuleConfig config)

Cache commonly required data as servlet context attributes.

param
servlet The ActionServlet instance running this webapp
param
config The ModuleConfig for this application module


        // Set up list of server types under "serverTypes"
        ArrayList serverTypes = new ArrayList();
        serverTypes.add(new LabelValueBean("IMAP Protocol", "imap"));
        serverTypes.add(new LabelValueBean("POP3 Protocol", "pop3"));
        servlet.getServletContext().setAttribute("serverTypes", serverTypes);