1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.eclipse.jetty.ant;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.net.MalformedURLException;
25 import java.util.ArrayList;
26 import java.util.Iterator;
27 import java.util.List;
28
29 import org.eclipse.jetty.ant.types.Connector;
30 import org.eclipse.jetty.ant.types.ContextHandlers;
31 import org.eclipse.jetty.ant.utils.ServerProxy;
32 import org.eclipse.jetty.ant.utils.TaskLog;
33 import org.eclipse.jetty.security.LoginService;
34 import org.eclipse.jetty.server.Handler;
35 import org.eclipse.jetty.server.RequestLog;
36 import org.eclipse.jetty.server.Server;
37 import org.eclipse.jetty.server.ServerConnector;
38 import org.eclipse.jetty.server.ShutdownMonitor;
39 import org.eclipse.jetty.server.handler.ContextHandler;
40 import org.eclipse.jetty.server.handler.ContextHandlerCollection;
41 import org.eclipse.jetty.server.handler.DefaultHandler;
42 import org.eclipse.jetty.server.handler.HandlerCollection;
43 import org.eclipse.jetty.server.handler.RequestLogHandler;
44 import org.eclipse.jetty.util.Scanner;
45 import org.eclipse.jetty.util.resource.Resource;
46 import org.eclipse.jetty.xml.XmlConfiguration;
47 import org.xml.sax.SAXException;
48
49
50
51
52
53
54
55 public class ServerProxyImpl implements ServerProxy
56 {
57
58
59 private Server server;
60
61
62 private File tempDirectory;
63
64
65 private ContextHandlerCollection contexts;
66
67
68 private File jettyXml;
69
70
71 private List<Connector> connectors;
72
73
74 private RequestLog requestLog;
75
76
77 private List<LoginService> loginServices;
78
79
80 private List<AntWebAppContext> webApplications = new ArrayList<AntWebAppContext>();
81
82
83 private ContextHandlers contextHandlers;
84
85
86 private int scanIntervalSecs;
87
88
89 private int stopPort;
90
91
92 private String stopKey;
93
94
95 private boolean daemon;
96
97
98 private boolean configured = false;
99
100
101
102
103
104
105
106
107
108
109 public static class WebAppScannerListener implements Scanner.BulkListener
110 {
111 AntWebAppContext awc;
112
113 public WebAppScannerListener (AntWebAppContext awc)
114 {
115 this.awc = awc;
116 }
117
118 public void filesChanged(List<String> changedFileNames)
119 {
120 boolean isScanned = false;
121 try
122 {
123 Iterator<String> itor = changedFileNames.iterator();
124 while (!isScanned && itor.hasNext())
125 {
126 isScanned = awc.isScanned(Resource.newResource(itor.next()).getFile());
127 }
128 if (isScanned)
129 {
130 awc.stop();
131 awc.start();
132 }
133 }
134 catch (Exception e)
135 {
136 TaskLog.log(e.getMessage());
137 }
138 }
139
140 }
141
142
143
144
145
146
147 public ServerProxyImpl ()
148 {
149 server = new Server();
150 server.setStopAtShutdown(true);
151 }
152
153
154 public void addWebApplication(AntWebAppContext webApp)
155 {
156 webApplications.add(webApp);
157 }
158
159 public int getStopPort()
160 {
161 return stopPort;
162 }
163
164 public void setStopPort(int stopPort)
165 {
166 this.stopPort = stopPort;
167 }
168
169 public String getStopKey()
170 {
171 return stopKey;
172 }
173
174 public void setStopKey(String stopKey)
175 {
176 this.stopKey = stopKey;
177 }
178
179 public File getJettyXml()
180 {
181 return jettyXml;
182 }
183
184 public void setJettyXml(File jettyXml)
185 {
186 this.jettyXml = jettyXml;
187 }
188
189 public List<Connector> getConnectors()
190 {
191 return connectors;
192 }
193
194 public void setConnectors(List<Connector> connectors)
195 {
196 this.connectors = connectors;
197 }
198
199 public RequestLog getRequestLog()
200 {
201 return requestLog;
202 }
203
204 public void setRequestLog(RequestLog requestLog)
205 {
206 this.requestLog = requestLog;
207 }
208
209 public List<LoginService> getLoginServices()
210 {
211 return loginServices;
212 }
213
214 public void setLoginServices(List<LoginService> loginServices)
215 {
216 this.loginServices = loginServices;
217 }
218
219 public List<AntWebAppContext> getWebApplications()
220 {
221 return webApplications;
222 }
223
224 public void setWebApplications(List<AntWebAppContext> webApplications)
225 {
226 this.webApplications = webApplications;
227 }
228
229
230 public File getTempDirectory()
231 {
232 return tempDirectory;
233 }
234
235
236 public void setTempDirectory(File tempDirectory)
237 {
238 this.tempDirectory = tempDirectory;
239 }
240
241
242
243
244
245 public void start()
246 {
247 try
248 {
249 configure();
250
251 configureWebApps();
252
253 server.start();
254
255 System.setProperty("jetty.ant.server.port","" + ((ServerConnector)server.getConnectors()[0]).getLocalPort());
256
257 String host = ((ServerConnector)server.getConnectors()[0]).getHost();
258
259 if (host == null)
260 {
261 System.setProperty("jetty.ant.server.host", "localhost");
262 }
263 else
264 {
265 System.setProperty("jetty.ant.server.host", host);
266 }
267
268 startScanners();
269
270 TaskLog.log("Jetty AntTask Started");
271
272 if (!daemon)
273 server.join();
274 }
275 catch (InterruptedException e)
276 {
277 new RuntimeException(e);
278 }
279 catch (Exception e)
280 {
281 new RuntimeException(e);
282 }
283 }
284
285
286
287
288
289
290
291 public Object getProxiedObject()
292 {
293 return server;
294 }
295
296
297
298
299
300 public boolean isDaemon()
301 {
302 return daemon;
303 }
304
305
306
307
308
309 public void setDaemon(boolean daemon)
310 {
311 this.daemon = daemon;
312 }
313
314
315
316
317
318 public ContextHandlers getContextHandlers()
319 {
320 return contextHandlers;
321 }
322
323
324
325
326
327 public void setContextHandlers (ContextHandlers contextHandlers)
328 {
329 this.contextHandlers = contextHandlers;
330 }
331
332
333 public int getScanIntervalSecs()
334 {
335 return scanIntervalSecs;
336 }
337
338
339 public void setScanIntervalSecs(int scanIntervalSecs)
340 {
341 this.scanIntervalSecs = scanIntervalSecs;
342 }
343
344
345
346
347
348 private void configure()
349 {
350 if (configured)
351 return;
352
353 configured = true;
354
355 ShutdownMonitor monitor = ShutdownMonitor.getInstance();
356 monitor.setPort(stopPort);
357 monitor.setKey(stopKey);
358 monitor.setExitVm(false);
359
360 if (tempDirectory != null && !tempDirectory.exists())
361 tempDirectory.mkdirs();
362
363
364 applyJettyXml();
365
366
367 if (connectors != null)
368 {
369 for (Connector c:connectors)
370 {
371 ServerConnector jc = new ServerConnector(server);
372
373 jc.setPort(c.getPort());
374 jc.setIdleTimeout(c.getMaxIdleTime());
375
376 server.addConnector(jc);
377 }
378 }
379
380
381 if (loginServices != null)
382 {
383 for (LoginService ls:loginServices)
384 {
385 server.addBean(ls);
386 }
387 }
388
389
390 Resource.setDefaultUseCaches(false);
391
392
393 configureHandlers();
394 }
395
396
397
398
399
400 private void configureHandlers()
401 {
402 RequestLogHandler requestLogHandler = new RequestLogHandler();
403 if (requestLog != null)
404 requestLogHandler.setRequestLog(requestLog);
405
406 contexts = (ContextHandlerCollection) server
407 .getChildHandlerByClass(ContextHandlerCollection.class);
408 if (contexts == null)
409 {
410 contexts = new ContextHandlerCollection();
411 HandlerCollection handlers = (HandlerCollection) server
412 .getChildHandlerByClass(HandlerCollection.class);
413 if (handlers == null)
414 {
415 handlers = new HandlerCollection();
416 server.setHandler(handlers);
417 handlers.setHandlers(new Handler[] { contexts, new DefaultHandler(),
418 requestLogHandler });
419 }
420 else
421 {
422 handlers.addHandler(contexts);
423 }
424 }
425
426
427 if (contextHandlers != null && contextHandlers.getContextHandlers() != null)
428 {
429 for (ContextHandler c:contextHandlers.getContextHandlers())
430 contexts.addHandler(c);
431 }
432 }
433
434
435
436
437
438
439
440 private void applyJettyXml()
441 {
442 if (jettyXml != null && jettyXml.exists())
443 {
444 TaskLog.log("Configuring jetty from xml configuration file = "
445 + jettyXml.getAbsolutePath());
446 XmlConfiguration configuration;
447 try
448 {
449 configuration = new XmlConfiguration(Resource.toURL(jettyXml));
450 configuration.configure(server);
451 }
452 catch (MalformedURLException e)
453 {
454 throw new RuntimeException(e);
455 }
456 catch (SAXException e)
457 {
458 throw new RuntimeException(e);
459 }
460 catch (IOException e)
461 {
462 throw new RuntimeException(e);
463 }
464 catch (Exception e)
465 {
466 throw new RuntimeException(e);
467 }
468 }
469 }
470
471
472
473
474
475 private void startScanners() throws Exception
476 {
477 for (AntWebAppContext awc:webApplications)
478 {
479 if (scanIntervalSecs <= 0)
480 return;
481
482 List<File> scanList = awc.getScanFiles();
483
484 TaskLog.log("Web application '" + awc + "': starting scanner at interval of "
485 + scanIntervalSecs + " seconds.");
486 Scanner.Listener changeListener = new WebAppScannerListener(awc);
487 Scanner scanner = new Scanner();
488 scanner.setScanInterval(scanIntervalSecs);
489 scanner.addListener(changeListener);
490 scanner.setScanDirs(scanList);
491 scanner.setReportExistingFilesOnStartup(false);
492 scanner.start();
493 }
494 }
495
496
497
498
499
500 private void configureWebApps()
501 {
502 for (AntWebAppContext awc:webApplications)
503 {
504 awc.setAttribute(AntWebAppContext.BASETEMPDIR, tempDirectory);
505 if (contexts != null)
506 contexts.addHandler(awc);
507 }
508 }
509
510 }