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