1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.apps.mailreader.plugin;
22
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.struts.action.ActionServlet;
27 import org.apache.struts.action.PlugIn;
28 import org.apache.struts.apps.mailreader.Constants;
29 import org.apache.struts.apps.mailreader.dao.impl.memory.MemoryUserDatabase;
30 import org.apache.struts.config.ModuleConfig;
31
32 import javax.servlet.ServletException;
33 import java.io.BufferedInputStream;
34 import java.io.BufferedOutputStream;
35 import java.io.File;
36 import java.io.FileOutputStream;
37 import java.io.InputStream;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 public final class MemoryDatabasePlugIn implements PlugIn {
57
58
59
60
61
62
63 private MemoryUserDatabase database = null;
64
65
66
67
68
69 private Log log = LogFactory.getLog(this.getClass());
70
71
72
73
74
75 private ActionServlet servlet = null;
76
77
78
79
80
81
82
83 private String pathname = "/WEB-INF/database.xml";
84
85 public String getPathname() {
86 return (this.pathname);
87 }
88
89 public void setPathname(String pathname) {
90 this.pathname = pathname;
91 }
92
93
94
95
96
97
98
99
100 public void destroy() {
101
102 log.info("Finalizing memory database plug in");
103
104 if (database != null) {
105 try {
106 database.close();
107 } catch (Exception e) {
108 log.error("Closing memory database", e);
109 }
110 }
111
112 servlet.getServletContext().removeAttribute(Constants.DATABASE_KEY);
113 database = null;
114 servlet = null;
115 database = null;
116 }
117
118
119
120
121
122
123
124
125
126 public void init(ActionServlet servlet, ModuleConfig config)
127 throws ServletException {
128
129 log.info("Initializing memory database plug in from '" +
130 pathname + "'");
131
132
133 this.servlet = servlet;
134
135
136 database = new MemoryUserDatabase();
137 try {
138 String path = calculatePath();
139 if (log.isDebugEnabled()) {
140 log.debug(" Loading database from '" + path + "'");
141 }
142 database.setPathname(path);
143 database.open();
144 } catch (Exception e) {
145 log.error("Opening memory database", e);
146 throw new ServletException("Cannot load database from '" +
147 pathname + "'", e);
148 }
149
150
151 servlet.getServletContext().setAttribute(Constants.DATABASE_KEY,
152 database);
153
154 }
155
156
157
158
159
160
161
162
163
164 private String calculatePath() throws Exception {
165
166
167 String path = servlet.getServletContext().getRealPath(pathname);
168 if (path != null) {
169 return (path);
170 }
171
172
173 File dir = (File)
174 servlet.getServletContext().getAttribute
175 ("javax.servlet.context.tempdir");
176 File file = new File(dir, "struts-example-database.xml");
177 if (file.exists()) {
178 return (file.getAbsolutePath());
179 }
180
181
182 InputStream is =
183 servlet.getServletContext().getResourceAsStream(pathname);
184 BufferedInputStream bis = new BufferedInputStream(is, 1024);
185 FileOutputStream os =
186 new FileOutputStream(file);
187 BufferedOutputStream bos = new BufferedOutputStream(os, 1024);
188 byte buffer[] = new byte[1024];
189 while (true) {
190 int n = bis.read(buffer);
191 if (n <= 0) {
192 break;
193 }
194 bos.write(buffer, 0, n);
195 }
196 bos.close();
197 bis.close();
198 return (file.getAbsolutePath());
199
200 }
201
202 }