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