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