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