001: /*
002: * $Id: MemoryDatabasePlugIn.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.apps.mailreader.plugin;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.apache.struts.action.ActionServlet;
026: import org.apache.struts.action.PlugIn;
027: import org.apache.struts.apps.mailreader.Constants;
028: import org.apache.struts.apps.mailreader.dao.impl.memory.MemoryUserDatabase;
029: import org.apache.struts.config.ModuleConfig;
030:
031: import javax.servlet.ServletException;
032: import java.io.BufferedInputStream;
033: import java.io.BufferedOutputStream;
034: import java.io.File;
035: import java.io.FileOutputStream;
036: import java.io.InputStream;
037:
038: /**
039: * <p><strong>MemoryDatabasePlugIn</strong> initializes and finalizes the
040: * persistent storage of User and Subscription information for the Struts
041: * Demonstration Application, using an in-memory database backed by an
042: * XML file.</p>
043: *
044: * <p><strong>IMPLEMENTATION WARNING</strong> - If this web application is run
045: * from a WAR file, or in another environment where reading and writing of the
046: * web application resource is impossible, the initial contents will be copied
047: * to a file in the web application temporary directory provided by the
048: * container. This is for demonstration purposes only - you should
049: * <strong>NOT</strong> assume that files written here will survive a restart
050: * of your servlet container.</p>
051: *
052: * @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $
053: */
054:
055: public final class MemoryDatabasePlugIn implements PlugIn {
056:
057: // ---- Instance Variables ----
058:
059: /**
060: * The {@link MemoryUserDatabase} object we construct and make available.
061: */
062: private MemoryUserDatabase database = null;
063:
064: /**
065: * Logging output for this plug in instance.
066: */
067: private Log log = LogFactory.getLog(this .getClass());
068:
069: /**
070: * The {@link ActionServlet} owning this application.
071: */
072: private ActionServlet servlet = null;
073:
074: // ---- Properties ----
075:
076: /**
077: * The web application resource path of our persistent database
078: * storage file.
079: */
080: private String pathname = "/WEB-INF/database.xml";
081:
082: public String getPathname() {
083: return (this .pathname);
084: }
085:
086: public void setPathname(String pathname) {
087: this .pathname = pathname;
088: }
089:
090: // ---- PlugIn Methods ----
091:
092: /**
093: * Gracefully shut down this database, releasing any resources
094: * that were allocated at initialization.
095: */
096: public void destroy() {
097:
098: log.info("Finalizing memory database plug in");
099:
100: if (database != null) {
101: try {
102: database.close();
103: } catch (Exception e) {
104: log.error("Closing memory database", e);
105: }
106: }
107:
108: servlet.getServletContext().removeAttribute(
109: Constants.DATABASE_KEY);
110: database = null;
111: servlet = null;
112: database = null;
113: }
114:
115: /**
116: * Initialize and load our initial database from persistent storage.
117: *
118: * @param servlet The ActionServlet for this web application
119: * @param config The ApplicationConfig for our owning module
120: * @throws ServletException if we cannot configure ourselves correctly
121: */
122: public void init(ActionServlet servlet, ModuleConfig config)
123: throws ServletException {
124:
125: log.info("Initializing memory database plug in from '"
126: + pathname + "'");
127:
128: // Remember our associated configuration and servlet
129: this .servlet = servlet;
130:
131: // Construct a new database and make it available
132: database = new MemoryUserDatabase();
133: try {
134: String path = calculatePath();
135: if (log.isDebugEnabled()) {
136: log.debug(" Loading database from '" + path + "'");
137: }
138: database.setPathname(path);
139: database.open();
140: } catch (Exception e) {
141: log.error("Opening memory database", e);
142: throw new ServletException("Cannot load database from '"
143: + pathname + "'", e);
144: }
145:
146: // Make the initialized database available
147: servlet.getServletContext().setAttribute(
148: Constants.DATABASE_KEY, database);
149:
150: }
151:
152: // ---- Private Methods ----
153:
154: /**
155: * Calculate and return an absolute pathname to the XML file to contain
156: * our persistent storage information.
157: *
158: * @throws Exception if an input/output error occurs
159: */
160: private String calculatePath() throws Exception {
161:
162: // Can we access the database via file I/O?
163: String path = servlet.getServletContext().getRealPath(pathname);
164: if (path != null) {
165: return (path);
166: }
167:
168: // Does a copy of this file already exist in our temporary directory
169: File dir = (File) servlet.getServletContext().getAttribute(
170: "javax.servlet.context.tempdir");
171: File file = new File(dir, "struts-example-database.xml");
172: if (file.exists()) {
173: return (file.getAbsolutePath());
174: }
175:
176: // Copy the static resource to a temporary file and return its path
177: InputStream is = servlet.getServletContext()
178: .getResourceAsStream(pathname);
179: BufferedInputStream bis = new BufferedInputStream(is, 1024);
180: FileOutputStream os = new FileOutputStream(file);
181: BufferedOutputStream bos = new BufferedOutputStream(os, 1024);
182: byte buffer[] = new byte[1024];
183: while (true) {
184: int n = bis.read(buffer);
185: if (n <= 0) {
186: break;
187: }
188: bos.write(buffer, 0, n);
189: }
190: bos.close();
191: bis.close();
192: return (file.getAbsolutePath());
193:
194: }
195:
196: }
|