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