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.handler.ContextHandler;
29 import org.eclipse.jetty.util.URIUtil;
30 import org.eclipse.jetty.util.annotation.ManagedAttribute;
31 import org.eclipse.jetty.util.annotation.ManagedObject;
32 import org.eclipse.jetty.util.resource.Resource;
33 import org.eclipse.jetty.webapp.WebAppContext;
34 import org.eclipse.jetty.xml.XmlConfiguration;
35
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 @ManagedObject("Provider for start-up deployement of webapps based on presence in directory")
62 public class WebAppProvider extends ScanningAppProvider
63 {
64 private boolean _extractWars = false;
65 private boolean _parentLoaderPriority = false;
66 private ConfigurationManager _configurationManager;
67 private String _defaultsDescriptor;
68 private File _tempDirectory;
69 private String[] _configurationClasses;
70
71 public class Filter implements FilenameFilter
72 {
73 @Override
74 public boolean accept(File dir, String name)
75 {
76 if (!dir.exists())
77 {
78 return false;
79 }
80 String lowername = name.toLowerCase(Locale.ENGLISH);
81
82 File file = new File(dir,name);
83
84
85 if (lowername.startsWith("."))
86 return false;
87
88
89 if (file.isDirectory())
90 {
91
92 if (lowername.endsWith(".d"))
93 return false;
94
95
96 if (exists(name+".war")||exists(name+".WAR"))
97 return false;
98
99
100 if (exists(name+".xml")||exists(name+".XML"))
101 return false;
102
103
104 if ("cvs".equals(lowername) || "cvsroot".equals(lowername))
105 return false;
106
107
108 return true;
109 }
110
111
112 if (lowername.endsWith(".war"))
113 {
114 String base=name.substring(0,name.length()-4);
115
116 if (exists(base+".xml")||exists(base+".XML"))
117 return false;
118
119
120 return true;
121 }
122
123
124 if (lowername.endsWith(".xml"))
125 return true;
126
127 return false;
128 }
129 }
130
131
132 public WebAppProvider()
133 {
134 super();
135 setFilenameFilter(new Filter());
136 setScanInterval(0);
137 }
138
139
140
141
142
143 @ManagedAttribute("extract war files")
144 public boolean isExtractWars()
145 {
146 return _extractWars;
147 }
148
149
150
151
152
153 public void setExtractWars(boolean extractWars)
154 {
155 _extractWars = extractWars;
156 }
157
158
159
160
161
162 @ManagedAttribute("parent classloader has priority")
163 public boolean isParentLoaderPriority()
164 {
165 return _parentLoaderPriority;
166 }
167
168
169
170
171
172 public void setParentLoaderPriority(boolean parentLoaderPriority)
173 {
174 _parentLoaderPriority = parentLoaderPriority;
175 }
176
177
178
179
180
181 @ManagedAttribute("default descriptor for webapps")
182 public String getDefaultsDescriptor()
183 {
184 return _defaultsDescriptor;
185 }
186
187
188
189
190
191 public void setDefaultsDescriptor(String defaultsDescriptor)
192 {
193 _defaultsDescriptor = defaultsDescriptor;
194 }
195
196
197 public ConfigurationManager getConfigurationManager()
198 {
199 return _configurationManager;
200 }
201
202
203
204
205
206 public void setConfigurationManager(ConfigurationManager configurationManager)
207 {
208 _configurationManager = configurationManager;
209 }
210
211
212
213
214
215 public void setConfigurationClasses(String[] configurations)
216 {
217 _configurationClasses = configurations==null?null:(String[])configurations.clone();
218 }
219
220
221
222
223
224 @ManagedAttribute("configuration classes for webapps to be processed through")
225 public String[] getConfigurationClasses()
226 {
227 return _configurationClasses;
228 }
229
230
231
232
233
234
235
236
237 public void setTempDir(File directory)
238 {
239 _tempDirectory = directory;
240 }
241
242
243
244
245
246
247 @ManagedAttribute("temp directory for use, null if no user set temp directory")
248 public File getTempDir()
249 {
250 return _tempDirectory;
251 }
252
253
254 @Override
255 public ContextHandler createContextHandler(final App app) throws Exception
256 {
257 Resource resource = Resource.newResource(app.getOriginId());
258 File file = resource.getFile();
259 if (!resource.exists())
260 throw new IllegalStateException("App resouce does not exist "+resource);
261
262 String context = file.getName();
263
264 if (resource.exists() && FileID.isXmlFile(file))
265 {
266 XmlConfiguration xmlc = new XmlConfiguration(resource.getURL())
267 {
268 @Override
269 public void initializeDefaults(Object context)
270 {
271 super.initializeDefaults(context);
272
273 if (context instanceof WebAppContext)
274 {
275 WebAppContext webapp = (WebAppContext)context;
276 webapp.setParentLoaderPriority(_parentLoaderPriority);
277 if (_defaultsDescriptor != null)
278 webapp.setDefaultsDescriptor(_defaultsDescriptor);
279 }
280 }
281 };
282
283 xmlc.getIdMap().put("Server", getDeploymentManager().getServer());
284 xmlc.getProperties().put("jetty.home",System.getProperty("jetty.home","."));
285 xmlc.getProperties().put("jetty.base",System.getProperty("jetty.base","."));
286 xmlc.getProperties().put("jetty.webapp",file.getCanonicalPath());
287 xmlc.getProperties().put("jetty.webapps",file.getParentFile().getCanonicalPath());
288
289 if (getConfigurationManager() != null)
290 xmlc.getProperties().putAll(getConfigurationManager().getProperties());
291 return (ContextHandler)xmlc.configure();
292 }
293 else if (file.isDirectory())
294 {
295
296 }
297 else if (FileID.isWebArchiveFile(file))
298 {
299
300 context = context.substring(0,context.length() - 4);
301 }
302 else
303 {
304 throw new IllegalStateException("unable to create ContextHandler for "+app);
305 }
306
307
308 if (context.endsWith("/") && context.length() > 0)
309 {
310 context = context.substring(0,context.length() - 1);
311 }
312
313
314 WebAppContext webAppContext = new WebAppContext();
315 webAppContext.setDisplayName(context);
316
317
318 if (context.equalsIgnoreCase("root"))
319 {
320 context = URIUtil.SLASH;
321 }
322 else if (context.toLowerCase(Locale.ENGLISH).startsWith("root-"))
323 {
324 int dash=context.toLowerCase(Locale.ENGLISH).indexOf('-');
325 String virtual = context.substring(dash+1);
326 webAppContext.setVirtualHosts(new String[]{virtual});
327 context = URIUtil.SLASH;
328 }
329
330
331 if (context.charAt(0) != '/')
332 {
333 context = "/" + context;
334 }
335
336
337 webAppContext.setContextPath(context);
338 webAppContext.setWar(file.getAbsolutePath());
339 if (_defaultsDescriptor != null)
340 {
341 webAppContext.setDefaultsDescriptor(_defaultsDescriptor);
342 }
343 webAppContext.setExtractWAR(_extractWars);
344 webAppContext.setParentLoaderPriority(_parentLoaderPriority);
345 if (_configurationClasses != null)
346 {
347 webAppContext.setConfigurationClasses(_configurationClasses);
348 }
349
350 if (_tempDirectory != null)
351 {
352
353
354
355
356
357
358
359 webAppContext.setAttribute(WebAppContext.BASETEMPDIR, _tempDirectory);
360 }
361 return webAppContext;
362 }
363
364 }