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.io.IOException;
24 import java.net.MalformedURLException;
25 import java.util.Locale;
26
27 import org.eclipse.jetty.deploy.App;
28 import org.eclipse.jetty.deploy.util.FileID;
29 import org.eclipse.jetty.server.handler.ContextHandler;
30 import org.eclipse.jetty.util.URIUtil;
31 import org.eclipse.jetty.util.resource.Resource;
32 import org.eclipse.jetty.webapp.WebAppContext;
33
34
35
36
37
38
39
40
41
42
43
44 public class WebAppProvider extends ScanningAppProvider
45 {
46 private boolean _extractWars = false;
47 private boolean _parentLoaderPriority = false;
48 private String _defaultsDescriptor;
49 private Filter _filter;
50 private File _tempDirectory;
51 private String[] _configurationClasses;
52
53 public static class Filter implements FilenameFilter
54 {
55 private File _contexts;
56
57 public boolean accept(File dir, String name)
58 {
59 if (!dir.exists())
60 {
61 return false;
62 }
63 String lowername = name.toLowerCase(Locale.ENGLISH);
64
65 File file = new File(dir,name);
66
67 if (!file.isDirectory() && !lowername.endsWith(".war"))
68 {
69 return false;
70 }
71
72
73 if (lowername.startsWith("."))
74 return false;
75
76 if (file.isDirectory())
77 {
78
79 if (new File(dir,name+".war").exists() ||
80 new File(dir,name+".WAR").exists())
81
82 return false;
83
84
85 if ("cvs".equals(lowername) || "cvsroot".equals(lowername))
86 return false;
87 }
88
89
90 if (_contexts!=null)
91 {
92 String context=name;
93 if (!file.isDirectory())
94 {
95 context=context.substring(0,context.length()-4);
96 }
97 if (new File(_contexts,context+".xml").exists() ||
98 new File(_contexts,context+".XML").exists() )
99 {
100 return false;
101 }
102 }
103
104 return true;
105 }
106 }
107
108
109 public WebAppProvider()
110 {
111 super(new Filter());
112 _filter=(Filter)_filenameFilter;
113 setScanInterval(0);
114 }
115
116
117
118
119
120 public boolean isExtractWars()
121 {
122 return _extractWars;
123 }
124
125
126
127
128
129 public void setExtractWars(boolean extractWars)
130 {
131 _extractWars = extractWars;
132 }
133
134
135
136
137
138 public boolean isParentLoaderPriority()
139 {
140 return _parentLoaderPriority;
141 }
142
143
144
145
146
147 public void setParentLoaderPriority(boolean parentLoaderPriority)
148 {
149 _parentLoaderPriority = parentLoaderPriority;
150 }
151
152
153
154
155
156 public String getDefaultsDescriptor()
157 {
158 return _defaultsDescriptor;
159 }
160
161
162
163
164
165 public void setDefaultsDescriptor(String defaultsDescriptor)
166 {
167 _defaultsDescriptor = defaultsDescriptor;
168 }
169
170
171 public String getContextXmlDir()
172 {
173 return _filter._contexts==null?null:_filter._contexts.toString();
174 }
175
176
177
178
179
180
181
182
183
184
185
186
187 public void setContextXmlDir(String contextsDir)
188 {
189 try
190 {
191 _filter._contexts=Resource.newResource(contextsDir).getFile();
192 }
193 catch (MalformedURLException e)
194 {
195 throw new RuntimeException(e);
196 }
197 catch (IOException e)
198 {
199 throw new RuntimeException(e);
200 }
201 }
202
203
204
205
206
207
208 public void setConfigurationClasses(String[] configurations)
209 {
210 _configurationClasses = configurations==null?null:(String[])configurations.clone();
211 }
212
213
214
215
216
217 public String[] getConfigurationClasses()
218 {
219 return _configurationClasses;
220 }
221
222
223
224
225
226
227
228
229 public void setTempDir(File directory)
230 {
231 _tempDirectory = directory;
232 }
233
234
235
236
237
238
239 public File getTempDir()
240 {
241 return _tempDirectory;
242 }
243
244
245 public ContextHandler createContextHandler(final App app) throws Exception
246 {
247 Resource resource = Resource.newResource(app.getOriginId());
248 File file = resource.getFile();
249 if (!resource.exists())
250 throw new IllegalStateException("App resouce does not exist "+resource);
251
252 String context = file.getName();
253
254 if (file.isDirectory())
255 {
256
257 }
258 else if (FileID.isWebArchiveFile(file))
259 {
260
261 context = context.substring(0,context.length() - 4);
262 }
263 else
264 {
265 throw new IllegalStateException("unable to create ContextHandler for "+app);
266 }
267
268
269 if (context.endsWith("/") && context.length() > 0)
270 {
271 context = context.substring(0,context.length() - 1);
272 }
273
274
275 WebAppContext wah = new WebAppContext();
276 wah.setDisplayName(context);
277
278
279 if (context.equalsIgnoreCase("root"))
280 {
281 context = URIUtil.SLASH;
282 }
283 else if (context.toLowerCase(Locale.ENGLISH).startsWith("root-"))
284 {
285 int dash=context.toLowerCase(Locale.ENGLISH).indexOf('-');
286 String virtual = context.substring(dash+1);
287 wah.setVirtualHosts(new String[]{virtual});
288 context = URIUtil.SLASH;
289 }
290
291
292 if (context.charAt(0) != '/')
293 {
294 context = "/" + context;
295 }
296
297
298 wah.setContextPath(context);
299 wah.setWar(file.getAbsolutePath());
300 if (_defaultsDescriptor != null)
301 {
302 wah.setDefaultsDescriptor(_defaultsDescriptor);
303 }
304 wah.setExtractWAR(_extractWars);
305 wah.setParentLoaderPriority(_parentLoaderPriority);
306 if (_configurationClasses != null)
307 {
308 wah.setConfigurationClasses(_configurationClasses);
309 }
310
311 if (_tempDirectory != null)
312 {
313
314
315
316
317
318
319
320 wah.setAttribute(WebAppContext.BASETEMPDIR,_tempDirectory);
321 }
322 return wah;
323 }
324
325 }