2
* Copyright 1999-2002,2004 The Apache Software Foundation.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
18
package org.apache.struts.webapp.example2.memory;
21
import java.io.BufferedInputStream;
22
import java.io.BufferedOutputStream;
24
import java.io.FileOutputStream;
25
import java.io.InputStream;
26
import java.util.ArrayList;
27
import javax.servlet.ServletException;
28
import org.apache.commons.logging.Log;
29
import org.apache.commons.logging.LogFactory;
30
import org.apache.struts.action.ActionServlet;
31
import org.apache.struts.action.PlugIn;
32
import org.apache.struts.config.ModuleConfig;
33
import org.apache.struts.util.LabelValueBean;
34
import org.apache.struts.webapp.example2.Constants;
37
* <p><strong>MemoryDatabasePlugIn</strong> initializes and finalizes the
38
* persistent storage of User and Subscription information for the Struts
39
* Demonstration Application, using an in-memory database backed by an
42
* <p><strong>IMPLEMENTATION WARNING</strong> - If this web application is run
43
* from a WAR file, or in another environment where reading and writing of the
44
* web application resource is impossible, the initial contents will be copied
45
* to a file in the web application temporary directory provided by the
46
* container. This is for demonstration purposes only - you should
47
* <strong>NOT</strong> assume that files written here will survive a restart
48
* of your servlet container.</p>
50
* @author Craig R. McClanahan
51
* @version $Rev: 54934 $ $Date: 2004-10-16 18:07:50 +0100 (Sat, 16 Oct 2004) $
54
public final class MemoryDatabasePlugIn implements PlugIn {
57
// ----------------------------------------------------- Instance Variables
61
* The application configuration for our owning module.
63
private ModuleConfig config = null;
67
* The {@link MemoryUserDatabase} object we construct and make available.
69
private MemoryUserDatabase database = null;
73
* Logging output for this plug in instance.
75
private Log log = LogFactory.getLog(this.getClass());
79
* The {@link ActionServlet} owning this application.
81
private ActionServlet servlet = null;
84
// ------------------------------------------------------------- Properties
88
* The web application resource path of our persistent database
91
private String pathname = "/WEB-INF/database.xml";
93
public String getPathname() {
94
return (this.pathname);
97
public void setPathname(String pathname) {
98
this.pathname = pathname;
102
// --------------------------------------------------------- PlugIn Methods
106
* Gracefully shut down this database, releasing any resources
107
* that were allocated at initialization.
109
public void destroy() {
111
log.info("Finalizing memory database plug in");
113
if (database != null) {
116
} catch (Exception e) {
117
log.error("Closing memory database", e);
121
servlet.getServletContext().removeAttribute(Constants.DATABASE_KEY);
131
* Initialize and load our initial database from persistent storage.
133
* @param servlet The ActionServlet for this web application
134
* @param config The ApplicationConfig for our owning module
136
* @exception ServletException if we cannot configure ourselves correctly
138
public void init(ActionServlet servlet, ModuleConfig config)
139
throws ServletException {
141
log.info("Initializing memory database plug in from '" +
144
// Remember our associated configuration and servlet
145
this.config = config;
146
this.servlet = servlet;
148
// Construct a new database and make it available
149
database = new MemoryUserDatabase();
151
String path = calculatePath();
152
if (log.isDebugEnabled()) {
153
log.debug(" Loading database from '" + path + "'");
155
database.setPathname(path);
157
} catch (Exception e) {
158
log.error("Opening memory database", e);
159
throw new ServletException("Cannot load database from '" +
163
// Make the initialized database available
164
servlet.getServletContext().setAttribute(Constants.DATABASE_KEY,
167
// Setup and cache other required data
168
setupCache(servlet, config);
173
// --------------------------------------------------------- Public Methods
176
// ------------------------------------------------------ Protected Methods
180
* <p>Cache commonly required data as servlet context attributes.</p>
182
* @param servlet The <code>ActionServlet</code> instance running
184
* @param config The <code>ModuleConfig</code> for this application module
186
protected void setupCache(ActionServlet servlet, ModuleConfig config) {
188
// Set up list of server types under "serverTypes"
189
ArrayList serverTypes = new ArrayList();
190
serverTypes.add(new LabelValueBean("IMAP Protocol", "imap"));
191
serverTypes.add(new LabelValueBean("POP3 Protocol", "pop3"));
192
servlet.getServletContext().setAttribute("serverTypes", serverTypes);
199
// -------------------------------------------------------- Private Methods
203
* Calculate and return an absolute pathname to the XML file to contain
204
* our persistent storage information.
206
* @exception Exception if an input/output error occurs
208
private String calculatePath() throws Exception {
210
// Can we access the database via file I/O?
211
String path = servlet.getServletContext().getRealPath(pathname);
216
// Does a copy of this file already exist in our temporary directory
218
servlet.getServletContext().getAttribute
219
("javax.servlet.context.tempdir");
220
File file = new File(dir, "struts-example-database.xml");
222
return (file.getAbsolutePath());
225
// Copy the static resource to a temporary file and return its path
227
servlet.getServletContext().getResourceAsStream(pathname);
228
BufferedInputStream bis = new BufferedInputStream(is, 1024);
229
FileOutputStream os =
230
new FileOutputStream(file);
231
BufferedOutputStream bos = new BufferedOutputStream(os, 1024);
232
byte buffer[] = new byte[1024];
234
int n = bis.read(buffer);
238
bos.write(buffer, 0, n);
242
return (file.getAbsolutePath());