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