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 @Override
249 public ContextHandler createContextHandler(final App app) throws Exception
250 {
251 Resource resource = Resource.newResource(app.getOriginId());
252 File file = resource.getFile();
253 if (!resource.exists())
254 throw new IllegalStateException("App resouce does not exist "+resource);
255
256 String context = file.getName();
257
258 if (resource.exists() && FileID.isXmlFile(file))
259 {
260 XmlConfiguration xmlc = new XmlConfiguration(resource.getURI().toURL())
261 {
262 @Override
263 public void initializeDefaults(Object context)
264 {
265 super.initializeDefaults(context);
266
267 if (context instanceof WebAppContext)
268 {
269 WebAppContext webapp = (WebAppContext)context;
270 webapp.setParentLoaderPriority(_parentLoaderPriority);
271 if (_defaultsDescriptor != null)
272 webapp.setDefaultsDescriptor(_defaultsDescriptor);
273 }
274 }
275 };
276
277 xmlc.getIdMap().put("Server", getDeploymentManager().getServer());
278 xmlc.getProperties().put("jetty.home",System.getProperty("jetty.home","."));
279 xmlc.getProperties().put("jetty.base",System.getProperty("jetty.base","."));
280 xmlc.getProperties().put("jetty.webapp",file.getCanonicalPath());
281 xmlc.getProperties().put("jetty.webapps",file.getParentFile().getCanonicalPath());
282
283 if (getConfigurationManager() != null)
284 xmlc.getProperties().putAll(getConfigurationManager().getProperties());
285 return (ContextHandler)xmlc.configure();
286 }
287 else if (file.isDirectory())
288 {
289
290 }
291 else if (FileID.isWebArchiveFile(file))
292 {
293
294 context = context.substring(0,context.length() - 4);
295 }
296 else
297 {
298 throw new IllegalStateException("unable to create ContextHandler for "+app);
299 }
300
301
302 if (context.endsWith("/") && context.length() > 0)
303 {
304 context = context.substring(0,context.length() - 1);
305 }
306
307
308 WebAppContext webAppContext = new WebAppContext();
309 webAppContext.setDisplayName(context);
310
311
312 if (context.equalsIgnoreCase("root"))
313 {
314 context = URIUtil.SLASH;
315 }
316 else if (context.toLowerCase(Locale.ENGLISH).startsWith("root-"))
317 {
318 int dash=context.toLowerCase(Locale.ENGLISH).indexOf('-');
319 String virtual = context.substring(dash+1);
320 webAppContext.setVirtualHosts(new String[]{virtual});
321 context = URIUtil.SLASH;
322 }
323
324
325 if (context.charAt(0) != '/')
326 {
327 context = "/" + context;
328 }
329
330
331 webAppContext.setContextPath(context);
332 webAppContext.setWar(file.getAbsolutePath());
333 if (_defaultsDescriptor != null)
334 {
335 webAppContext.setDefaultsDescriptor(_defaultsDescriptor);
336 }
337 webAppContext.setExtractWAR(_extractWars);
338 webAppContext.setParentLoaderPriority(_parentLoaderPriority);
339 if (_configurationClasses != null)
340 {
341 webAppContext.setConfigurationClasses(_configurationClasses);
342 }
343
344 if (_tempDirectory != null)
345 {
346
347
348
349
350
351
352
353 webAppContext.setAttribute(WebAppContext.BASETEMPDIR, _tempDirectory);
354 }
355 return webAppContext;
356 }
357
358
359
360 @Override
361 protected void fileChanged(String filename) throws Exception
362 {
363 File file = new File(filename);
364 if (!file.exists())
365 return;
366
367 File parent = file.getParentFile();
368
369
370 if (file.isDirectory())
371 {
372
373 if (exists(file.getName()+".xml")||exists(file.getName()+".XML"))
374 return;
375
376
377 if (exists(file.getName()+".war")||exists(file.getName()+".WAR"))
378 return;
379
380 super.fileChanged(filename);
381 return;
382 }
383
384
385 String lowname = file.getName().toLowerCase(Locale.ENGLISH);
386
387 if (lowname.endsWith(".war"))
388 {
389 String name = file.getName();
390 String base=name.substring(0,name.length()-4);
391 String xmlname = base+".xml";
392 if (exists(xmlname))
393 {
394
395 File xml = new File (parent, xmlname);
396 super.fileChanged(xml.getCanonicalPath());
397 return;
398 }
399
400 xmlname = base+".XML";
401 if (exists(xmlname))
402 {
403
404 File xml = new File(parent, xmlname);
405 super.fileChanged(xml.getCanonicalPath());
406 return;
407 }
408
409
410 super.fileChanged(filename);
411 return;
412 }
413
414
415 if (lowname.endsWith(".xml"))
416 super.fileChanged(filename);
417 }
418
419
420 @Override
421 protected void fileAdded(String filename) throws Exception
422 {
423 File file = new File(filename);
424 if (!file.exists())
425 return;
426
427
428 if (file.isDirectory())
429 {
430
431 if (exists(file.getName()+".xml")||exists(file.getName()+".XML"))
432 return;
433
434
435 if (exists(file.getName()+".war")||exists(file.getName()+".WAR"))
436 return;
437
438 super.fileAdded(filename);
439 return;
440 }
441
442
443
444 String lowname = file.getName().toLowerCase(Locale.ENGLISH);
445 if (lowname.endsWith(".war"))
446 {
447 String name = file.getName();
448 String base=name.substring(0,name.length()-4);
449
450 if (exists(base+".xml")||exists(base+".XML"))
451 return;
452
453 super.fileAdded(filename);
454 return;
455 }
456
457
458 if (lowname.endsWith(".xml"))
459 super.fileAdded(filename);
460 }
461
462
463
464 @Override
465 protected void fileRemoved(String filename) throws Exception
466 {
467 File file = new File(filename);
468
469
470 if (file.isDirectory())
471 {
472
473 if (exists(file.getName()+".xml")||exists(file.getName()+".XML"))
474 return;
475
476
477 if (exists(file.getName()+".war")||exists(file.getName()+".WAR"))
478 return;
479
480 super.fileRemoved(filename);
481 return;
482 }
483
484
485 String lowname = file.getName().toLowerCase(Locale.ENGLISH);
486 if (lowname.endsWith(".war"))
487 {
488
489 String name = file.getName();
490 String base=name.substring(0,name.length()-4);
491 if (exists(base+".xml")||exists(base+".XML"))
492 return;
493
494 super.fileRemoved(filename);
495 return;
496 }
497
498
499 if (lowname.endsWith(".xml"))
500 super.fileRemoved(filename);
501 }
502
503 }