1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.transaction.file; |
18 | |
|
19 | |
import java.io.ByteArrayInputStream; |
20 | |
import java.io.InputStream; |
21 | |
import java.io.OutputStream; |
22 | |
|
23 | |
import org.apache.commons.transaction.util.LoggerFacade; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public class VirtualAdminCommandsFileResourceManager extends |
33 | |
FileResourceManager implements ResourceManager, |
34 | |
ResourceManagerErrorCodes { |
35 | |
|
36 | 1 | protected String virtualAdminPath = null; |
37 | |
|
38 | |
public String getVirtualAdminPath() { |
39 | 10 | return virtualAdminPath; |
40 | |
} |
41 | |
|
42 | |
public void setVirtualAdminPath(String virutalAdminPath) { |
43 | 1 | this.virtualAdminPath = virutalAdminPath; |
44 | 1 | } |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public VirtualAdminCommandsFileResourceManager(String storeDir, |
60 | |
String workDir, boolean urlEncodePath, LoggerFacade logger) { |
61 | 0 | this(storeDir, workDir, urlEncodePath, logger, false); |
62 | 0 | } |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public VirtualAdminCommandsFileResourceManager(String storeDir, |
81 | |
String workDir, boolean urlEncodePath, LoggerFacade logger, |
82 | |
boolean debug) { |
83 | 1 | this(storeDir, workDir, urlEncodePath ? new URLEncodeIdMapper() : null, |
84 | |
new NoOpTransactionIdToPathMapper(), logger, debug); |
85 | 1 | } |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
public VirtualAdminCommandsFileResourceManager(String storeDir, |
105 | |
String workDir, ResourceIdToPathMapper idMapper, |
106 | |
LoggerFacade logger, boolean debug) { |
107 | 0 | this(storeDir, workDir, idMapper, new NoOpTransactionIdToPathMapper(), |
108 | |
logger, debug); |
109 | 0 | } |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
public VirtualAdminCommandsFileResourceManager(String storeDir, |
129 | |
String workDir, ResourceIdToPathMapper idMapper, |
130 | |
TransactionIdToPathMapper txIdMapper, LoggerFacade logger, |
131 | |
boolean debug) { |
132 | 1 | super(workDir, storeDir, idMapper, txIdMapper, logger, debug); |
133 | 1 | } |
134 | |
|
135 | |
public boolean resourceExists(Object resourceId) |
136 | |
throws ResourceManagerException { |
137 | 0 | if (isVirtualAdminId(resourceId)) { |
138 | 0 | logger |
139 | |
.logFine("Faking existence of virtual administration command"); |
140 | 0 | return true; |
141 | |
} |
142 | |
|
143 | 0 | return super.resourceExists(resourceId); |
144 | |
} |
145 | |
|
146 | |
public boolean resourceExists(Object txId, Object resourceId) |
147 | |
throws ResourceManagerException { |
148 | 0 | if (isVirtualAdminId(resourceId)) { |
149 | 0 | logger |
150 | |
.logFine("Faking existence of virtual administration command"); |
151 | 0 | return true; |
152 | |
} |
153 | |
|
154 | 0 | return super.resourceExists(txId, resourceId); |
155 | |
} |
156 | |
|
157 | |
public void deleteResource(Object txId, Object resourceId) |
158 | |
throws ResourceManagerException { |
159 | |
|
160 | 0 | checkForVirtualAdminCommand(resourceId); |
161 | |
|
162 | 0 | super.deleteResource(txId, resourceId); |
163 | 0 | } |
164 | |
|
165 | |
public void deleteResource(Object txId, Object resourceId, |
166 | |
boolean assureOnly) throws ResourceManagerException { |
167 | |
|
168 | 0 | checkForVirtualAdminCommand(resourceId); |
169 | |
|
170 | 0 | super.deleteResource(txId, resourceId, assureOnly); |
171 | 0 | } |
172 | |
|
173 | |
public void createResource(Object txId, Object resourceId) |
174 | |
throws ResourceManagerException { |
175 | |
|
176 | 0 | checkForVirtualAdminCommand(resourceId); |
177 | |
|
178 | 0 | super.createResource(txId, resourceId); |
179 | 0 | } |
180 | |
|
181 | |
public void createResource(Object txId, Object resourceId, |
182 | |
boolean assureOnly) throws ResourceManagerException { |
183 | |
|
184 | 0 | checkForVirtualAdminCommand(resourceId); |
185 | |
|
186 | 0 | super.createResource(txId, resourceId, assureOnly); |
187 | 0 | } |
188 | |
|
189 | |
public void copyResource(Object txId, Object fromResourceId, |
190 | |
Object toResourceId, boolean overwrite) |
191 | |
throws ResourceManagerException { |
192 | |
|
193 | 0 | checkForVirtualAdminCommand(fromResourceId); |
194 | 0 | checkForVirtualAdminCommand(toResourceId); |
195 | |
|
196 | 0 | super.copyResource(txId, fromResourceId, toResourceId, overwrite); |
197 | 0 | } |
198 | |
|
199 | |
public void moveResource(Object txId, Object fromResourceId, |
200 | |
Object toResourceId, boolean overwrite) |
201 | |
throws ResourceManagerException { |
202 | |
|
203 | 0 | checkForVirtualAdminCommand(fromResourceId); |
204 | 0 | checkForVirtualAdminCommand(toResourceId); |
205 | |
|
206 | 0 | super.moveResource(txId, fromResourceId, toResourceId, overwrite); |
207 | 0 | } |
208 | |
|
209 | |
public InputStream readResource(Object resourceId) |
210 | |
throws ResourceManagerException { |
211 | |
|
212 | 2 | if (isVirtualAdminId(resourceId)) { |
213 | 2 | logger.logWarning("Issuing virtual admin command" + resourceId); |
214 | 2 | return executeAdminCommand(resourceId); |
215 | |
} |
216 | |
|
217 | 0 | return super.readResource(resourceId); |
218 | |
} |
219 | |
|
220 | |
public InputStream readResource(Object txId, Object resourceId) |
221 | |
throws ResourceManagerException { |
222 | |
|
223 | 0 | if (isVirtualAdminId(resourceId)) { |
224 | 0 | String message = "You must not call virtual admin commands (" |
225 | |
+ resourceId + ") from within transactions!"; |
226 | 0 | logger.logSevere(message); |
227 | 0 | throw new ResourceManagerException(message); |
228 | |
} |
229 | |
|
230 | 0 | return super.readResource(txId, resourceId); |
231 | |
} |
232 | |
|
233 | |
protected void checkForVirtualAdminCommand(Object resourceId) |
234 | |
throws ResourceManagerException { |
235 | 0 | if (isVirtualAdminId(resourceId)) { |
236 | 0 | String message = "You must not make modification calls to virtual admin commands (" |
237 | |
+ resourceId + ")!"; |
238 | 0 | logger.logSevere(message); |
239 | 0 | throw new ResourceManagerException(message); |
240 | |
} |
241 | 0 | } |
242 | |
|
243 | |
protected boolean isVirtualAdminId(Object resourceId) { |
244 | 4 | return (getVirtualAdminPath() != null && resourceId.toString() |
245 | |
.startsWith(getVirtualAdminPath())); |
246 | |
} |
247 | |
|
248 | |
protected InputStream executeAdminCommand(Object resourceId) { |
249 | 2 | StringBuffer sb = new StringBuffer(); |
250 | |
|
251 | 2 | if (!isVirtualAdminId(resourceId)) { |
252 | 0 | String message = "Internal error: " + resourceId.toString() |
253 | |
+ " is no administration command, but is supposed to!"; |
254 | 0 | sb.append(message); |
255 | 0 | logger.logSevere(message); |
256 | 0 | } else { |
257 | 2 | String command = resourceId.toString().substring( |
258 | |
getVirtualAdminPath().length()); |
259 | 2 | logger.logInfo("Processing admin command " + command); |
260 | |
|
261 | |
|
262 | |
try { |
263 | 2 | if (isAKnowCommand(command)) { |
264 | 2 | if (command.equals("recover")) { |
265 | 2 | recover(); |
266 | |
} |
267 | |
|
268 | 1 | String message = "Command " + command |
269 | |
+ " terminated successfully"; |
270 | 1 | sb.append(message); |
271 | 1 | logger.logInfo(message); |
272 | 1 | } else { |
273 | 0 | String message = "Command " + command + " unknown"; |
274 | 0 | sb.append(message); |
275 | 0 | logger.logWarning(message); |
276 | |
|
277 | |
} |
278 | 1 | } catch (ResourceManagerSystemException e) { |
279 | 1 | String message = "Command " + command |
280 | |
+ " failed with the following message: " |
281 | |
+ e.getMessage(); |
282 | 1 | sb.append(message); |
283 | 1 | logger.logSevere(message, e); |
284 | 1 | } |
285 | |
|
286 | |
} |
287 | 2 | ByteArrayInputStream baIs = new ByteArrayInputStream(sb.toString() |
288 | |
.getBytes()); |
289 | 2 | return baIs; |
290 | |
|
291 | |
} |
292 | |
|
293 | |
protected boolean isAKnowCommand(String command) { |
294 | 2 | return command.equals("recover"); |
295 | |
} |
296 | |
|
297 | |
public OutputStream writeResource(Object txId, Object resourceId, |
298 | |
boolean append) throws ResourceManagerException { |
299 | |
|
300 | 0 | checkForVirtualAdminCommand(resourceId); |
301 | |
|
302 | 0 | return super.writeResource(txId, resourceId, append); |
303 | |
} |
304 | |
} |