1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
package nl.toolforge.karma.core.cmd; |
20 |
|
|
21 |
|
import nl.toolforge.core.util.listener.ChangeListener; |
22 |
|
import nl.toolforge.core.util.listener.ListenerManager; |
23 |
|
import nl.toolforge.core.util.listener.ListenerManagerException; |
24 |
|
import nl.toolforge.karma.core.KarmaRuntimeException; |
25 |
|
import nl.toolforge.karma.core.boot.WorkingContext; |
26 |
|
import nl.toolforge.karma.core.boot.WorkingContextConfiguration; |
27 |
|
import nl.toolforge.karma.core.cmd.event.CommandFailedEvent; |
28 |
|
import nl.toolforge.karma.core.cmd.event.CommandFinishedEvent; |
29 |
|
import nl.toolforge.karma.core.cmd.event.CommandResponseListener; |
30 |
|
import nl.toolforge.karma.core.cmd.event.CommandStartedEvent; |
31 |
|
import nl.toolforge.karma.core.cmd.event.ErrorEvent; |
32 |
|
import nl.toolforge.karma.core.cmd.event.MessageEvent; |
33 |
|
import nl.toolforge.karma.core.cmd.event.SimpleMessage; |
34 |
|
import nl.toolforge.karma.core.location.LocationException; |
35 |
|
import nl.toolforge.karma.core.manifest.Manifest; |
36 |
|
import nl.toolforge.karma.core.manifest.ManifestException; |
37 |
|
import nl.toolforge.karma.core.manifest.ManifestFactory; |
38 |
|
import nl.toolforge.karma.core.manifest.ManifestLoader; |
39 |
|
import nl.toolforge.karma.core.manifest.ManifestStructure; |
40 |
|
import nl.toolforge.karma.core.module.Module; |
41 |
|
import org.apache.commons.logging.Log; |
42 |
|
import org.apache.commons.logging.LogFactory; |
43 |
|
|
44 |
|
import java.io.File; |
45 |
|
import java.util.ArrayList; |
46 |
|
import java.util.Collection; |
47 |
|
import java.util.HashMap; |
48 |
|
import java.util.Iterator; |
49 |
|
import java.util.Map; |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
public final class CommandContext implements ChangeListener { |
61 |
|
|
62 |
0 |
private static final Log logger = LogFactory.getLog(CommandContext.class); |
63 |
|
|
64 |
|
private Manifest currentManifest; |
65 |
0 |
private Map modificationMap = new HashMap(); |
66 |
0 |
private boolean managed = false; |
67 |
|
|
68 |
|
private static ListenerManager manager; |
69 |
|
|
70 |
0 |
private CommandResponseHandler handler = null; |
71 |
0 |
private WorkingContext workingContext = null; |
72 |
0 |
private CommandResponse commandResponse = null; |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
0 |
public CommandContext(WorkingContext workingContext) { |
78 |
0 |
this.workingContext = workingContext; |
79 |
0 |
} |
80 |
|
|
81 |
|
public WorkingContext getWorkingContext() { |
82 |
0 |
return workingContext; |
83 |
|
} |
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
public synchronized void init(CommandResponseHandler handler, boolean updateStores) throws CommandException { |
94 |
|
|
95 |
0 |
if (handler == null) { |
96 |
0 |
throw new IllegalArgumentException("CommandResponseHandler may not be null."); |
97 |
|
} |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
0 |
commandResponse = new CommandResponse(); |
102 |
0 |
commandResponse.addCommandResponseListener(handler); |
103 |
0 |
setHandler(handler); |
104 |
|
|
105 |
|
|
106 |
|
|
107 |
0 |
Command command = new KarmaInitializationCommand(updateStores); |
108 |
|
|
109 |
0 |
command.setContext(this); |
110 |
0 |
command.registerCommandResponseListener(getHandler()); |
111 |
|
|
112 |
0 |
CommandStartedEvent startEvent = new CommandStartedEvent(command); |
113 |
|
|
114 |
|
|
115 |
|
try { |
116 |
0 |
command.execute(); |
117 |
0 |
} catch (CommandException c) { |
118 |
0 |
commandResponse.addEvent(new ErrorEvent(command, c.getErrorCode(), c.getMessageArguments())); |
119 |
|
|
120 |
0 |
throw c; |
121 |
0 |
} |
122 |
|
|
123 |
0 |
command.deregisterCommandResponseListener(handler); |
124 |
0 |
command.cleanUp(); |
125 |
0 |
} |
126 |
|
|
127 |
|
|
128 |
|
private synchronized void setFileModificationTimes() { |
129 |
|
|
130 |
0 |
Manifest manifest = currentManifest; |
131 |
|
|
132 |
0 |
WorkingContextConfiguration config = workingContext.getConfiguration(); |
133 |
|
|
134 |
0 |
Long lastMod = new Long(class="keyword">new File(config.getManifestStore().getModule().getBaseDir(), manifest.getName() + ".xml").lastModified()); |
135 |
0 |
modificationMap.put(manifest, lastMod); |
136 |
|
|
137 |
|
try { |
138 |
0 |
Collection includes = manifest.getIncludes(); |
139 |
0 |
for (Iterator i = includes.iterator(); i.hasNext();) { |
140 |
|
|
141 |
0 |
manifest = (Manifest) i.next(); |
142 |
|
|
143 |
0 |
lastMod = new Long(class="keyword">new File(config.getManifestStore().getModule().getBaseDir(), manifest.getName() + ".xml").lastModified()); |
144 |
0 |
modificationMap.put(manifest, lastMod); |
145 |
|
} |
146 |
0 |
} catch (Exception e) { |
147 |
0 |
logger.error(e); |
148 |
0 |
modificationMap.clear(); |
149 |
0 |
} |
150 |
0 |
} |
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
public synchronized void process() { |
157 |
|
|
158 |
0 |
boolean reload = false; |
159 |
|
|
160 |
|
try { |
161 |
|
|
162 |
0 |
Collection manifests = new ArrayList(); |
163 |
0 |
manifests.add(currentManifest); |
164 |
0 |
manifests.addAll(currentManifest.getIncludes()); |
165 |
|
|
166 |
0 |
for (Iterator i = manifests.iterator(); i.hasNext();) { |
167 |
|
|
168 |
0 |
Manifest m = (Manifest) i.next(); |
169 |
0 |
long lastMod = ((Long) modificationMap.get(m)).class="keyword">longValue(); |
170 |
|
|
171 |
0 |
WorkingContextConfiguration config = workingContext.getConfiguration(); |
172 |
|
|
173 |
|
|
174 |
0 |
File f = new File(config.getManifestStore().getModule().getBaseDir(), m.getName() + ".xml"); |
175 |
0 |
if (!f.exists()) { |
176 |
0 |
currentManifest = null; |
177 |
0 |
throw new ManifestException(ManifestException.MANIFEST_FILE_NOT_FOUND, class="keyword">new Object[] {m.getName()}); |
178 |
|
} |
179 |
|
|
180 |
0 |
if (f.lastModclass="keyword">ified() > lastMod) { |
181 |
0 |
reload = true; |
182 |
0 |
break; |
183 |
|
} |
184 |
|
} |
185 |
|
|
186 |
0 |
if (reload) { |
187 |
|
|
188 |
|
|
189 |
|
|
190 |
0 |
ManifestStructure reloadedStructure = |
191 |
|
getWorkingContext().getManifestLoader().load(currentManifest.getName()); |
192 |
0 |
currentManifest = new ManifestFactory().create(workingContext, reloadedStructure); |
193 |
|
|
194 |
0 |
setFileModificationTimes(); |
195 |
|
|
196 |
0 |
String message = "\nManifest " + getCurrentManifest().getName() + " has changed on disk. Reloaded automatically.\n"; |
197 |
0 |
logger.info(message); |
198 |
|
|
199 |
0 |
commandResponse.addEvent(new MessageEvent(class="keyword">new SimpleMessage(message))); |
200 |
|
|
201 |
0 |
return; |
202 |
|
} |
203 |
|
|
204 |
0 |
} catch (ManifestException m) { |
205 |
|
|
206 |
|
|
207 |
|
|
208 |
0 |
managed = false; |
209 |
0 |
manager.suspendListener(this); |
210 |
|
|
211 |
0 |
logger.error(m); |
212 |
|
|
213 |
|
|
214 |
|
|
215 |
0 |
throw new KarmaRuntimeException(m.getErrorCode(), m.getMessageArguments()); |
216 |
0 |
} catch (Exception e) { |
217 |
|
|
218 |
0 |
managed = false; |
219 |
0 |
manager.suspendListener(this); |
220 |
|
|
221 |
0 |
logger.error("Error while processing manifests during automatic reload; " + e.getMessage(), e); |
222 |
0 |
} |
223 |
0 |
} |
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
|
228 |
|
|
229 |
|
|
230 |
|
public Manifest getCurrentManifest() { |
231 |
0 |
return currentManifest; |
232 |
|
} |
233 |
|
|
234 |
|
|
235 |
|
|
236 |
|
|
237 |
|
|
238 |
|
|
239 |
|
|
240 |
|
|
241 |
|
public void changeCurrentManifest(String manifestName) throws ManifestException, LocationException { |
242 |
|
|
243 |
0 |
ManifestFactory manifestFactory = new ManifestFactory(); |
244 |
0 |
ManifestLoader loader = new ManifestLoader(workingContext); |
245 |
0 |
Manifest newManifest = manifestFactory.create(workingContext, loader.load(manifestName)); |
246 |
|
|
247 |
|
|
248 |
|
|
249 |
0 |
currentManifest = newManifest; |
250 |
|
|
251 |
0 |
register(); |
252 |
0 |
} |
253 |
|
|
254 |
|
|
255 |
|
|
256 |
|
|
257 |
|
|
258 |
|
|
259 |
|
public void changeCurrentManifest(Manifest newManifest) { |
260 |
0 |
currentManifest = newManifest; |
261 |
|
|
262 |
0 |
if (currentManclass="keyword">ifest != null) { |
263 |
0 |
register(); |
264 |
|
} |
265 |
0 |
} |
266 |
|
|
267 |
|
|
268 |
|
|
269 |
|
|
270 |
|
synchronized void register() { |
271 |
|
|
272 |
0 |
setFileModificationTimes(); |
273 |
|
|
274 |
0 |
if (!managed) { |
275 |
|
|
276 |
0 |
manager = ListenerManager.getInstance(); |
277 |
|
try { |
278 |
0 |
manager.register(this); |
279 |
0 |
} catch (ListenerManagerException e) { |
280 |
0 |
logger.error(e); |
281 |
0 |
throw new KarmaRuntimeException(e.getMessage()); |
282 |
0 |
} |
283 |
|
|
284 |
0 |
manager.start(); |
285 |
|
|
286 |
0 |
managed = true; |
287 |
|
} |
288 |
0 |
} |
289 |
|
|
290 |
|
|
291 |
|
|
292 |
|
|
293 |
|
|
294 |
|
|
295 |
|
public Collection getAllManifests() { |
296 |
0 |
return workingContext.getManifestCollector().getAllManifests(); |
297 |
|
} |
298 |
|
|
299 |
|
|
300 |
|
|
301 |
|
|
302 |
|
|
303 |
|
|
304 |
|
|
305 |
|
|
306 |
|
|
307 |
|
|
308 |
|
public void execute(String commandLine) throws CommandException { |
309 |
|
|
310 |
0 |
Command command = null; |
311 |
|
try { |
312 |
0 |
command = CommandFactory.getInstance().getCommand(commandLine); |
313 |
0 |
} catch (CommandException c) { |
314 |
0 |
logger.error(c.getMessage()); |
315 |
0 |
commandResponse.addEvent(new ErrorEvent(c.getErrorCode(), c.getMessageArguments())); |
316 |
0 |
throw c; |
317 |
0 |
} catch (CommandLoadException e) { |
318 |
0 |
logger.error(e.getMessage()); |
319 |
0 |
throw new CommandException(e, e.getErrorCode(), e.getMessageArguments()); |
320 |
0 |
} |
321 |
0 |
execute(command); |
322 |
0 |
} |
323 |
|
|
324 |
|
|
325 |
|
|
326 |
|
|
327 |
|
|
328 |
|
|
329 |
|
|
330 |
|
public void execute(Command command) throws CommandException { |
331 |
|
|
332 |
0 |
if (command == null) { |
333 |
0 |
throw new IllegalArgumentException("Invalid command; command cannot be null."); |
334 |
|
} |
335 |
|
|
336 |
|
|
337 |
|
|
338 |
0 |
command.setContext(this); |
339 |
0 |
command.registerCommandResponseListener(getHandler()); |
340 |
|
|
341 |
|
|
342 |
|
|
343 |
|
|
344 |
0 |
CommandStartedEvent startEvent = new CommandStartedEvent(command); |
345 |
0 |
commandResponse.addEvent(startEvent); |
346 |
|
|
347 |
|
try { |
348 |
0 |
command.execute(); |
349 |
0 |
} catch (CommandException c) { |
350 |
0 |
logger.error(c.getMessage()); |
351 |
0 |
commandResponse.addEvent(new ErrorEvent(command, c.getErrorCode(), c.getMessageArguments())); |
352 |
0 |
commandResponse.addEvent(new CommandFailedEvent(command, c)); |
353 |
0 |
throw c; |
354 |
0 |
} |
355 |
0 |
commandResponse.addEvent(new CommandFinishedEvent(command, startEvent.getTime())); |
356 |
|
|
357 |
0 |
command.deregisterCommandResponseListener(getHandler()); |
358 |
0 |
command.cleanUp(); |
359 |
0 |
} |
360 |
|
|
361 |
|
private void setHandler(CommandResponseHandler handler) { |
362 |
0 |
this.handler = handler; |
363 |
0 |
} |
364 |
|
|
365 |
|
private CommandResponseListener getHandler() { |
366 |
0 |
return handler; |
367 |
|
} |
368 |
|
|
369 |
|
|
370 |
|
|
371 |
|
|
372 |
|
|
373 |
|
|
374 |
|
public boolean isManifestLoaded() { |
375 |
0 |
return currentManifest != null; |
376 |
|
} |
377 |
|
|
378 |
|
|
379 |
|
|
380 |
|
|
381 |
|
|
382 |
|
|
383 |
|
|
384 |
|
|
385 |
|
|
386 |
|
|
387 |
0 |
|
388 |
0 |
|
389 |
|
|
390 |
0 |
public File getLocalPath(Module module) { |
391 |
|
|
392 |
0 |
File localPath = new File(getBase(), module.getName()); |
393 |
0 |
logger.debug("getLocalPath() = " + localPath.getPath()); |
394 |
|
|
395 |
0 |
return localPath; |
396 |
|
} |
397 |
|
|
398 |
|
|
399 |
|
|
400 |
|
|
401 |
|
private File getBase() { |
402 |
0 |
return new File(workingContext.getProjectBaseDirectory(), getCurrentManifest().getName()); |
403 |
|
} |
404 |
|
|
405 |
|
|
406 |
|
|
407 |
|
|
408 |
|
|
409 |
|
|
410 |
|
public void setWorkingContext(WorkingContext workingContext) { |
411 |
0 |
this.workingContext = workingContext; |
412 |
0 |
} |
413 |
|
} |