1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts.apps.scriptingmailreader.plugin;
19
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.struts.action.ActionServlet;
24 import org.apache.struts.action.PlugIn;
25 import org.apache.struts.apps.scriptingmailreader.Constants;
26 import org.apache.struts.apps.mailreader.dao.impl.memory.MemoryUserDatabase;
27 import org.apache.struts.config.ModuleConfig;
28
29 import javax.servlet.ServletException;
30 import java.io.BufferedInputStream;
31 import java.io.BufferedOutputStream;
32 import java.io.File;
33 import java.io.FileOutputStream;
34 import java.io.InputStream;
35
36 /***
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
40 * XML file.</p>
41 *
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>
49 *
50 * @version $Rev: 421490 $ $Date: 2006-07-12 20:46:13 -0700 (Wed, 12 Jul 2006) $
51 */
52
53 public final class MemoryDatabasePlugIn implements PlugIn {
54
55
56
57 /***
58 * The {@link MemoryUserDatabase} object we construct and make available.
59 */
60 private MemoryUserDatabase database = null;
61
62
63 /***
64 * Logging output for this plug in instance.
65 */
66 private Log log = LogFactory.getLog(this.getClass());
67
68
69 /***
70 * The {@link ActionServlet} owning this application.
71 */
72 private ActionServlet servlet = null;
73
74
75
76 /***
77 * The web application resource path of our persistent database
78 * storage file.
79 */
80 private String pathname = "/WEB-INF/database.xml";
81
82 public String getPathname() {
83 return (this.pathname);
84 }
85
86 public void setPathname(String pathname) {
87 this.pathname = pathname;
88 }
89
90
91
92
93 /***
94 * Gracefully shut down this database, releasing any resources
95 * that were allocated at initialization.
96 */
97 public void destroy() {
98
99 log.info("Finalizing memory database plug in");
100
101 if (database != null) {
102 try {
103 database.close();
104 } catch (Exception e) {
105 log.error("Closing memory database", e);
106 }
107 }
108
109 servlet.getServletContext().removeAttribute(Constants.DATABASE_KEY);
110 database = null;
111 servlet = null;
112 database = null;
113 }
114
115
116 /***
117 * Initialize and load our initial database from persistent storage.
118 *
119 * @param servlet The ActionServlet for this web application
120 * @param config The ApplicationConfig for our owning module
121 * @throws ServletException if we cannot configure ourselves correctly
122 */
123 public void init(ActionServlet servlet, ModuleConfig config)
124 throws ServletException {
125
126 log.info("Initializing memory database plug in from '" +
127 pathname + "'");
128
129
130 this.servlet = servlet;
131
132
133 database = new MemoryUserDatabase();
134 try {
135 String path = calculatePath();
136 if (log.isDebugEnabled()) {
137 log.debug(" Loading database from '" + path + "'");
138 }
139 database.setPathname(path);
140 database.open();
141 } catch (Exception e) {
142 log.error("Opening memory database", e);
143 throw new ServletException("Cannot load database from '" +
144 pathname + "'", e);
145 }
146
147
148 servlet.getServletContext().setAttribute(Constants.DATABASE_KEY,
149 database);
150
151 }
152
153
154
155 /***
156 * Calculate and return an absolute pathname to the XML file to contain
157 * our persistent storage information.
158 *
159 * @throws Exception if an input/output error occurs
160 */
161 private String calculatePath() throws Exception {
162
163
164 String path = servlet.getServletContext().getRealPath(pathname);
165 if (path != null) {
166 return (path);
167 }
168
169
170 File dir = (File)
171 servlet.getServletContext().getAttribute
172 ("javax.servlet.context.tempdir");
173 File file = new File(dir, "struts-example-database.xml");
174 if (file.exists()) {
175 return (file.getAbsolutePath());
176 }
177
178
179 InputStream is =
180 servlet.getServletContext().getResourceAsStream(pathname);
181 BufferedInputStream bis = new BufferedInputStream(is, 1024);
182 FileOutputStream os =
183 new FileOutputStream(file);
184 BufferedOutputStream bos = new BufferedOutputStream(os, 1024);
185 byte buffer[] = new byte[1024];
186 while (true) {
187 int n = bis.read(buffer);
188 if (n <= 0) {
189 break;
190 }
191 bos.write(buffer, 0, n);
192 }
193 bos.close();
194 bis.close();
195 return (file.getAbsolutePath());
196
197 }
198
199 }