1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.mortbay.jetty.plugin;
18
19
20 import java.io.File;
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.List;
24
25 import org.apache.maven.plugin.AbstractMojo;
26 import org.apache.maven.plugin.MojoExecutionException;
27 import org.apache.maven.plugin.MojoFailureException;
28 import org.apache.maven.project.MavenProject;
29 import org.mortbay.jetty.plugin.util.ConsoleScanner;
30 import org.mortbay.jetty.plugin.util.JettyPluginServer;
31 import org.mortbay.jetty.plugin.util.PluginLog;
32 import org.mortbay.jetty.plugin.util.SystemProperties;
33 import org.mortbay.jetty.plugin.util.SystemProperty;
34 import org.mortbay.util.Scanner;
35
36
37
38
39
40
41
42
43 public abstract class AbstractJettyMojo extends AbstractMojo
44 {
45
46
47
48 protected JettyPluginServer server;
49
50
51
52
53
54
55 protected Jetty6PluginWebAppContext webAppConfig;
56
57
58
59
60
61
62
63
64
65
66 protected MavenProject project;
67
68
69
70
71
72
73
74
75
76
77 protected String contextPath;
78
79
80
81
82
83
84
85
86
87 protected File tmpDirectory;
88
89
90
91
92
93
94
95
96
97 protected File webDefaultXml;
98
99
100
101
102
103
104
105
106
107 protected File overrideWebXml;
108
109
110
111
112
113
114
115
116
117 protected int scanIntervalSeconds;
118
119
120
121
122
123
124
125
126
127
128 protected String reload;
129
130
131
132
133
134
135
136
137 protected SystemProperties systemProperties;
138
139
140
141
142
143
144
145
146 protected File jettyConfig;
147
148
149
150
151
152
153 protected int stopPort;
154
155
156
157
158
159
160 protected String stopKey;
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 protected boolean daemon;
176
177
178
179
180 protected Scanner scanner;
181
182
183
184
185 protected ArrayList scanList;
186
187
188
189
190 protected ArrayList scannerListeners;
191
192
193
194
195
196 protected Thread consoleScanner;
197
198
199 public String PORT_SYSPROPERTY = "jetty.port";
200
201
202
203
204 public abstract Object[] getConfiguredUserRealms();
205
206
207
208
209 public abstract Object[] getConfiguredConnectors();
210
211 public abstract Object getConfiguredRequestLog();
212
213
214 public abstract void checkPomConfiguration() throws MojoExecutionException;
215
216
217
218 public abstract void configureScanner () throws MojoExecutionException;
219
220
221 public abstract void applyJettyXml () throws Exception;
222
223
224
225
226
227
228 public abstract JettyPluginServer createServer() throws Exception;
229
230
231 public abstract void finishConfigurationBeforeStart() throws Exception;
232
233
234 public MavenProject getProject()
235 {
236 return this.project;
237 }
238
239 public File getTmpDirectory()
240 {
241 return this.tmpDirectory;
242 }
243
244
245 public File getWebDefaultXml()
246 {
247 return this.webDefaultXml;
248 }
249
250 public File getOverrideWebXml()
251 {
252 return this.overrideWebXml;
253 }
254
255
256
257
258 public String getContextPath()
259 {
260 return this.contextPath;
261 }
262
263
264
265
266 public int getScanIntervalSeconds()
267 {
268 return this.scanIntervalSeconds;
269 }
270
271
272 public File getJettyXmlFile ()
273 {
274 return this.jettyConfig;
275 }
276
277
278 public JettyPluginServer getServer ()
279 {
280 return this.server;
281 }
282
283 public void setServer (JettyPluginServer server)
284 {
285 this.server = server;
286 }
287
288
289 public void setScanList (ArrayList list)
290 {
291 this.scanList = new ArrayList(list);
292 }
293
294 public ArrayList getScanList ()
295 {
296 return this.scanList;
297 }
298
299
300 public void setScannerListeners (ArrayList listeners)
301 {
302 this.scannerListeners = new ArrayList(listeners);
303 }
304
305 public ArrayList getScannerListeners ()
306 {
307 return this.scannerListeners;
308 }
309
310 public Scanner getScanner ()
311 {
312 return scanner;
313 }
314
315 public void execute() throws MojoExecutionException, MojoFailureException
316 {
317 getLog().info("Configuring Jetty for project: " + getProject().getName());
318 PluginLog.setLog(getLog());
319 checkPomConfiguration();
320 startJetty();
321 }
322
323
324 public void startJetty () throws MojoExecutionException
325 {
326 try
327 {
328 getLog().debug("Starting Jetty Server ...");
329
330 printSystemProperties();
331 setServer(createServer());
332
333
334
335 applyJettyXml ();
336
337 JettyPluginServer plugin=getServer();
338
339
340
341
342
343 Object[] configuredConnectors = getConfiguredConnectors();
344
345 plugin.setConnectors(configuredConnectors);
346 Object[] connectors = plugin.getConnectors();
347
348 if (connectors == null|| connectors.length == 0)
349 {
350
351 configuredConnectors = new Object[] { plugin.createDefaultConnector(System.getProperty(PORT_SYSPROPERTY, null)) };
352 plugin.setConnectors(configuredConnectors);
353 }
354
355
356
357 if (getConfiguredRequestLog() != null)
358 getServer().setRequestLog(getConfiguredRequestLog());
359
360
361 getServer().configureHandlers();
362 configureWebApplication();
363 getServer().addWebApplication(webAppConfig);
364
365
366
367 Object[] configuredRealms = getConfiguredUserRealms();
368 for (int i = 0; (configuredRealms != null) && i < configuredRealms.length; i++)
369 getLog().debug(configuredRealms[i].getClass().getName() + ": "+ configuredRealms[i].toString());
370
371 plugin.setUserRealms(configuredRealms);
372
373
374
375 finishConfigurationBeforeStart();
376
377 if(stopPort>0 && stopKey!=null)
378 {
379 System.setProperty("STOP.PORT", String.valueOf(stopPort));
380 System.setProperty("STOP.KEY", stopKey);
381 org.mortbay.start.Monitor.monitor();
382 }
383
384 server.start();
385
386 getLog().info("Started Jetty Server");
387
388
389 configureScanner ();
390 startScanner();
391
392
393 startConsoleScanner();
394
395
396 if (!daemon)
397 {
398 server.join();
399 }
400 }
401 catch (Exception e)
402 {
403 throw new MojoExecutionException("Failure", e);
404 }
405 finally
406 {
407 if (!daemon)
408 {
409 getLog().info("Jetty server exiting.");
410 }
411 }
412
413 }
414
415
416 public abstract void restartWebApp(boolean reconfigureScanner) throws Exception;
417
418
419
420
421
422
423
424 public void configureWebApplication () throws Exception
425 {
426
427
428 if (webAppConfig == null)
429 {
430 webAppConfig = new Jetty6PluginWebAppContext();
431 webAppConfig.setContextPath((getContextPath().startsWith("/") ? getContextPath() : "/"+ getContextPath()));
432 if (getTmpDirectory() != null)
433 webAppConfig.setTempDirectory(getTmpDirectory());
434 if (getWebDefaultXml() != null)
435 webAppConfig.setDefaultsDescriptor(getWebDefaultXml().getCanonicalPath());
436 if (getOverrideWebXml() != null)
437 webAppConfig.setOverrideDescriptor(getOverrideWebXml().getCanonicalPath());
438 }
439
440
441 getLog().info("Context path = " + webAppConfig.getContextPath());
442 getLog().info("Tmp directory = "+ " determined at runtime");
443 getLog().info("Web defaults = "+(webAppConfig.getDefaultsDescriptor()==null?" jetty default":webAppConfig.getDefaultsDescriptor()));
444 getLog().info("Web overrides = "+(webAppConfig.getOverrideDescriptor()==null?" none":webAppConfig.getOverrideDescriptor()));
445
446 }
447
448
449
450
451
452
453
454 private void startScanner()
455 {
456
457
458 if (getScanIntervalSeconds() <= 0) return;
459
460
461 if ( "manual".equalsIgnoreCase( reload ) )
462 {
463
464
465 getLog().warn("scanIntervalSeconds is set to " + scanIntervalSeconds + " but will be IGNORED due to manual reloading");
466 return;
467 }
468
469 scanner = new Scanner();
470 scanner.setReportExistingFilesOnStartup(false);
471 scanner.setScanInterval(getScanIntervalSeconds());
472 scanner.setScanDirs(getScanList());
473 scanner.setRecursive(true);
474 List listeners = getScannerListeners();
475 Iterator itor = (listeners==null?null:listeners.iterator());
476 while (itor!=null && itor.hasNext())
477 scanner.addListener((Scanner.Listener)itor.next());
478 getLog().info("Starting scanner at interval of " + getScanIntervalSeconds()+ " seconds.");
479 scanner.start();
480 }
481
482
483
484
485 protected void startConsoleScanner()
486 {
487 if ( "manual".equalsIgnoreCase( reload ) )
488 {
489 getLog().info("Console reloading is ENABLED. Hit ENTER on the console to restart the context.");
490 consoleScanner = new ConsoleScanner(this);
491 consoleScanner.start();
492 }
493
494 }
495
496 private void printSystemProperties ()
497 {
498
499 if (getLog().isDebugEnabled())
500 {
501 if (systemProperties != null)
502 {
503 Iterator itor = systemProperties.getSystemProperties().iterator();
504 while (itor.hasNext())
505 {
506 SystemProperty prop = (SystemProperty)itor.next();
507 getLog().debug("Property "+prop.getName()+"="+prop.getValue()+" was "+ (prop.isSet() ? "set" : "skipped"));
508 }
509 }
510 }
511 }
512
513
514
515
516
517
518
519 public File findJettyWebXmlFile (File webInfDir)
520 {
521 if (webInfDir == null)
522 return null;
523 if (!webInfDir.exists())
524 return null;
525
526 File f = new File (webInfDir, "jetty-web.xml");
527 if (f.exists())
528 return f;
529
530
531 f = new File (webInfDir, "web-jetty.xml");
532 if (f.exists())
533 return f;
534 f = new File (webInfDir, "jetty6-web.xml");
535 if (f.exists())
536 return f;
537
538 return null;
539 }
540 }