1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.deploy.providers;
20
21 import java.io.File;
22 import java.io.FilenameFilter;
23 import java.util.Locale;
24
25 import org.eclipse.jetty.deploy.App;
26 import org.eclipse.jetty.deploy.ConfigurationManager;
27 import org.eclipse.jetty.deploy.util.FileID;
28 import org.eclipse.jetty.server.Server;
29 import org.eclipse.jetty.server.handler.ContextHandler;
30 import org.eclipse.jetty.util.URIUtil;
31 import org.eclipse.jetty.util.annotation.ManagedAttribute;
32 import org.eclipse.jetty.util.annotation.ManagedObject;
33 import org.eclipse.jetty.util.resource.Resource;
34 import org.eclipse.jetty.webapp.WebAppContext;
35 import org.eclipse.jetty.xml.XmlConfiguration;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 @ManagedObject("Provider for start-up deployement of webapps based on presence in directory")
63 public class WebAppProvider extends ScanningAppProvider
64 {
65 private boolean _extractWars = false;
66 private boolean _parentLoaderPriority = false;
67 private ConfigurationManager _configurationManager;
68 private String _defaultsDescriptor;
69 private File _tempDirectory;
70 private String[] _configurationClasses;
71
72 public class Filter implements FilenameFilter
73 {
74 @Override
75 public boolean accept(File dir, String name)
76 {
77 if (!dir.exists())
78 {
79 return false;
80 }
81 String lowername = name.toLowerCase(Locale.ENGLISH);
82
83 File file = new File(dir,name);
84
85
86 if (lowername.startsWith("."))
87 return false;
88
89
90 if (file.isDirectory())
91 {
92
93 if (lowername.endsWith(".d"))
94 return false;
95
96
97 if (exists(name+".war")||exists(name+".WAR"))
98 return false;
99
100
101 if (exists(name+".xml")||exists(name+".XML"))
102 return false;
103
104
105 if ("cvs".equals(lowername) || "cvsroot".equals(lowername))
106 return false;
107
108
109 return true;
110 }
111
112
113 if (lowername.endsWith(".war"))
114 {
115
116 return true;
117 }
118
119
120 if (lowername.endsWith(".xml"))
121 return true;
122
123 return false;
124 }
125 }
126
127
128 public WebAppProvider()
129 {
130 super();
131 setFilenameFilter(new Filter());
132 setScanInterval(0);
133 }
134
135
136
137
138
139 @ManagedAttribute("extract war files")
140 public boolean isExtractWars()
141 {
142 return _extractWars;
143 }
144
145
146
147
148
149 public void setExtractWars(boolean extractWars)
150 {
151 _extractWars = extractWars;
152 }
153
154
155
156
157
158 @ManagedAttribute("parent classloader has priority")
159 public boolean isParentLoaderPriority()
160 {
161 return _parentLoaderPriority;
162 }
163
164
165
166
167
168 public void setParentLoaderPriority(boolean parentLoaderPriority)
169 {
170 _parentLoaderPriority = parentLoaderPriority;
171 }
172
173
174
175
176
177 @ManagedAttribute("default descriptor for webapps")
178 public String getDefaultsDescriptor()
179 {
180 return _defaultsDescriptor;
181 }
182
183
184
185
186
187 public void setDefaultsDescriptor(String defaultsDescriptor)
188 {
189 _defaultsDescriptor = defaultsDescriptor;
190 }
191
192
193 public ConfigurationManager getConfigurationManager()
194 {
195 return _configurationManager;
196 }
197
198
199
200
201
202 public void setConfigurationManager(ConfigurationManager configurationManager)
203 {
204 _configurationManager = configurationManager;
205 }
206
207
208
209
210
211 public void setConfigurationClasses(String[] configurations)
212 {
213 _configurationClasses = configurations==null?null:(String[])configurations.clone();
214 }
215
216
217 @ManagedAttribute("configuration classes for webapps to be processed through")
218 public String[] getConfigurationClasses()
219 {
220 return _configurationClasses;
221 }
222
223
224
225
226
227
228
229
230 public void setTempDir(File directory)
231 {
232 _tempDirectory = directory;
233 }
234
235
236
237
238
239
240
241 @ManagedAttribute("temp directory for use, null if no user set temp directory")
242 public File getTempDir()
243 {
244 return _tempDirectory;
245 }
246
247
248 protected void initializeWebAppContextDefaults(WebAppContext webapp)
249 {
250 if (_defaultsDescriptor != null)
251 webapp.setDefaultsDescriptor(_defaultsDescriptor);
252 webapp.setExtractWAR(_extractWars);
253 webapp.setParentLoaderPriority(_parentLoaderPriority);
254 if (_configurationClasses != null)
255 webapp.setConfigurationClasses(_configurationClasses);
256
257 if (_tempDirectory != null)
258 {
259
260
261
262
263
264
265 webapp.setAttribute(WebAppContext.BASETEMPDIR, _tempDirectory);
266 }
267 }
268
269
270 @Override
271 public ContextHandler createContextHandler(final App app) throws Exception
272 {
273 Resource resource = Resource.newResource(app.getOriginId());
274 File file = resource.getFile();
275 if (!resource.exists())
276 throw new IllegalStateException("App resouce does not exist "+resource);
277
278 String context = file.getName();
279
280 if (resource.exists() && FileID.isXmlFile(file))
281 {
282 XmlConfiguration xmlc = new XmlConfiguration(resource.getURI().toURL())
283 {
284 @Override
285 public void initializeDefaults(Object context)
286 {
287 super.initializeDefaults(context);
288
289 if (context instanceof WebAppContext)
290 {
291 WebAppContext webapp = (WebAppContext)context;
292 initializeWebAppContextDefaults(webapp);
293 }
294 }
295 };
296
297 xmlc.getIdMap().put("Server", getDeploymentManager().getServer());
298 xmlc.getProperties().put("jetty.home",System.getProperty("jetty.home","."));
299 xmlc.getProperties().put("jetty.base",System.getProperty("jetty.base","."));
300 xmlc.getProperties().put("jetty.webapp",file.getCanonicalPath());
301 xmlc.getProperties().put("jetty.webapps",file.getParentFile().getCanonicalPath());
302
303 if (getConfigurationManager() != null)
304 xmlc.getProperties().putAll(getConfigurationManager().getProperties());
305 return (ContextHandler)xmlc.configure();
306 }
307 else if (file.isDirectory())
308 {
309
310 }
311 else if (FileID.isWebArchiveFile(file))
312 {
313
314 context = context.substring(0,context.length() - 4);
315 }
316 else
317 {
318 throw new IllegalStateException("unable to create ContextHandler for "+app);
319 }
320
321
322 if (context.endsWith("/") && context.length() > 0)
323 {
324 context = context.substring(0,context.length() - 1);
325 }
326
327
328 WebAppContext webAppContext = new WebAppContext();
329 webAppContext.setDisplayName(context);
330
331
332 if (context.equalsIgnoreCase("root"))
333 {
334 context = URIUtil.SLASH;
335 }
336 else if (context.toLowerCase(Locale.ENGLISH).startsWith("root-"))
337 {
338 int dash=context.toLowerCase(Locale.ENGLISH).indexOf('-');
339 String virtual = context.substring(dash+1);
340 webAppContext.setVirtualHosts(new String[]{virtual});
341 context = URIUtil.SLASH;
342 }
343
344
345 if (context.charAt(0) != '/')
346 {
347 context = "/" + context;
348 }
349
350 webAppContext.setContextPath(context);
351 webAppContext.setWar(file.getAbsolutePath());
352 initializeWebAppContextDefaults(webAppContext);
353
354 return webAppContext;
355 }
356
357
358
359 @Override
360 protected void fileChanged(String filename) throws Exception
361 {
362 File file = new File(filename);
363 if (!file.exists())
364 return;
365
366 File parent = file.getParentFile();
367
368
369 if (file.isDirectory())
370 {
371
372 if (exists(file.getName()+".xml")||exists(file.getName()+".XML"))
373 return;
374
375
376 if (exists(file.getName()+".war")||exists(file.getName()+".WAR"))
377 return;
378
379 super.fileChanged(filename);
380 return;
381 }
382
383
384 String lowname = file.getName().toLowerCase(Locale.ENGLISH);
385
386 if (lowname.endsWith(".war"))
387 {
388 String name = file.getName();
389 String base=name.substring(0,name.length()-4);
390 String xmlname = base+".xml";
391 if (exists(xmlname))
392 {
393
394 File xml = new File (parent, xmlname);
395 super.fileChanged(xml.getCanonicalPath());
396 return;
397 }
398
399 xmlname = base+".XML";
400 if (exists(xmlname))
401 {
402
403 File xml = new File(parent, xmlname);
404 super.fileChanged(xml.getCanonicalPath());
405 return;
406 }
407
408
409 super.fileChanged(filename);
410 return;
411 }
412
413
414 if (lowname.endsWith(".xml"))
415 super.fileChanged(filename);
416 }
417
418
419 @Override
420 protected void fileAdded(String filename) throws Exception
421 {
422 File file = new File(filename);
423 if (!file.exists())
424 return;
425
426
427 if (file.isDirectory())
428 {
429
430 if (exists(file.getName()+".xml")||exists(file.getName()+".XML"))
431 return;
432
433
434 if (exists(file.getName()+".war")||exists(file.getName()+".WAR"))
435 return;
436
437 super.fileAdded(filename);
438 return;
439 }
440
441
442
443 String lowname = file.getName().toLowerCase(Locale.ENGLISH);
444 if (lowname.endsWith(".war"))
445 {
446 String name = file.getName();
447 String base=name.substring(0,name.length()-4);
448
449 if (exists(base+".xml")||exists(base+".XML"))
450 return;
451
452 super.fileAdded(filename);
453 return;
454 }
455
456
457 if (lowname.endsWith(".xml"))
458 super.fileAdded(filename);
459 }
460
461
462
463 @Override
464 protected void fileRemoved(String filename) throws Exception
465 {
466 File file = new File(filename);
467
468
469 if (file.isDirectory())
470 {
471
472 if (exists(file.getName()+".xml")||exists(file.getName()+".XML"))
473 return;
474
475
476 if (exists(file.getName()+".war")||exists(file.getName()+".WAR"))
477 return;
478
479 super.fileRemoved(filename);
480 return;
481 }
482
483
484 String lowname = file.getName().toLowerCase(Locale.ENGLISH);
485 if (lowname.endsWith(".war"))
486 {
487
488 String name = file.getName();
489 String base=name.substring(0,name.length()-4);
490 if (exists(base+".xml")||exists(base+".XML"))
491 return;
492
493 super.fileRemoved(filename);
494 return;
495 }
496
497
498 if (lowname.endsWith(".xml"))
499 super.fileRemoved(filename);
500 }
501
502 }