1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.maven.plugin;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.net.MalformedURLException;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29 import java.util.TreeSet;
30
31 import org.eclipse.jetty.plus.webapp.EnvConfiguration;
32 import org.eclipse.jetty.util.URIUtil;
33 import org.eclipse.jetty.util.log.Log;
34 import org.eclipse.jetty.util.log.Logger;
35 import org.eclipse.jetty.util.resource.Resource;
36 import org.eclipse.jetty.util.resource.ResourceCollection;
37 import org.eclipse.jetty.webapp.Configuration;
38 import org.eclipse.jetty.webapp.FragmentConfiguration;
39 import org.eclipse.jetty.webapp.JettyWebXmlConfiguration;
40 import org.eclipse.jetty.webapp.MetaInfConfiguration;
41 import org.eclipse.jetty.webapp.WebAppContext;
42 import org.eclipse.jetty.webapp.WebInfConfiguration;
43 import org.eclipse.jetty.webapp.WebXmlConfiguration;
44
45
46
47
48
49
50
51
52
53
54 public class JettyWebAppContext extends WebAppContext
55 {
56 private static final Logger LOG = Log.getLogger(JettyWebAppContext.class);
57
58 private static final String DEFAULT_CONTAINER_INCLUDE_JAR_PATTERN = ".*/javax.servlet-[^/]*\\.jar$|.*/servlet-api-[^/]*\\.jar$";
59 private static final String WEB_INF_CLASSES_PREFIX = "/WEB-INF/classes";
60 private static final String WEB_INF_LIB_PREFIX = "/WEB-INF/lib";
61
62 private File _classes = null;
63 private File _testClasses = null;
64 private final List<File> _webInfClasses = new ArrayList<File>();
65 private final List<File> _webInfJars = new ArrayList<File>();
66 private final Map<String, File> _webInfJarMap = new HashMap<String, File>();
67 private final EnvConfiguration _envConfig;
68 private List<File> _classpathFiles;
69 private String _jettyEnvXml;
70 private List<Overlay> _overlays;
71
72
73
74
75
76
77
78 private String _containerIncludeJarPattern = null;
79
80
81
82
83
84 private String _webInfIncludeJarPattern = null;
85
86
87
88
89
90
91
92
93 private boolean _baseAppFirst = true;
94
95
96
97 public JettyWebAppContext ()
98 throws Exception
99 {
100 super();
101 setConfigurations(new Configuration[]{
102 new MavenWebInfConfiguration(),
103 new WebXmlConfiguration(),
104 new MetaInfConfiguration(),
105 new FragmentConfiguration(),
106 _envConfig = new EnvConfiguration(),
107 new org.eclipse.jetty.plus.webapp.PlusConfiguration(),
108 new MavenAnnotationConfiguration(),
109 new JettyWebXmlConfiguration()
110 });
111
112 super.setCopyWebInf(false);
113 }
114 public void setContainerIncludeJarPattern(String pattern)
115 {
116 _containerIncludeJarPattern = pattern;
117 }
118
119 public String getContainerIncludeJarPattern()
120 {
121 return _containerIncludeJarPattern;
122 }
123
124
125 public String getWebInfIncludeJarPattern()
126 {
127 return _webInfIncludeJarPattern;
128 }
129 public void setWebInfIncludeJarPattern(String pattern)
130 {
131 _webInfIncludeJarPattern = pattern;
132 }
133
134
135 public List<File> getClassPathFiles()
136 {
137 return this._classpathFiles;
138 }
139
140
141 public void setJettyEnvXml (String jettyEnvXml)
142 {
143 this._jettyEnvXml = jettyEnvXml;
144 }
145
146 public String getJettyEnvXml()
147 {
148 return this._jettyEnvXml;
149 }
150
151
152 public void setClasses(File dir)
153 {
154 _classes = dir;
155 }
156
157 public File getClasses()
158 {
159 return _classes;
160 }
161
162 public void setWebInfLib (List<File> jars)
163 {
164 _webInfJars.addAll(jars);
165 }
166
167
168 public void setTestClasses (File dir)
169 {
170 _testClasses = dir;
171 }
172
173
174 public File getTestClasses ()
175 {
176 return _testClasses;
177 }
178
179
180
181
182
183
184 public void setOverlays (List<Overlay> overlays)
185 {
186 _overlays = overlays;
187 }
188
189 public List<Overlay> getOverlays()
190 {
191 return _overlays;
192 }
193
194
195 public void setBaseAppFirst(boolean value)
196 {
197 _baseAppFirst = value;
198 }
199
200
201 public boolean getBaseAppFirst()
202 {
203 return _baseAppFirst;
204 }
205
206
207
208
209
210
211
212 public void setResourceBases(String[] resourceBases)
213 {
214 List<String> resources = new ArrayList<String>();
215 for (String rl:resourceBases)
216 {
217 String[] rs = rl.split(" *, *");
218 for (String r:rs)
219 resources.add(r);
220 }
221
222 setBaseResource(new ResourceCollection(resources.toArray(new String[resources.size()])));
223 }
224
225 public List<File> getWebInfLib()
226 {
227 return _webInfJars;
228 }
229
230 public void doStart () throws Exception
231 {
232
233
234
235
236
237 String tmp = _containerIncludeJarPattern;
238 if (tmp==null || "".equals(tmp))
239 tmp = (String)getAttribute(WebInfConfiguration.CONTAINER_JAR_PATTERN);
240
241 tmp = addPattern(tmp, DEFAULT_CONTAINER_INCLUDE_JAR_PATTERN);
242 setAttribute(WebInfConfiguration.CONTAINER_JAR_PATTERN, tmp);
243
244
245
246
247 if (_webInfIncludeJarPattern != null)
248 setAttribute(WebInfConfiguration.WEBINF_JAR_PATTERN, _webInfIncludeJarPattern);
249
250
251 if (_testClasses != null)
252 _webInfClasses.add(_testClasses);
253 if (_classes != null)
254 _webInfClasses.add(_classes);
255
256
257 _classpathFiles = new ArrayList<File>();
258 _classpathFiles.addAll(_webInfClasses);
259 _classpathFiles.addAll(_webInfJars);
260
261
262 _webInfJarMap.clear();
263 for (File file : _webInfJars)
264 {
265
266 String fileName = file.getName();
267 if (fileName.endsWith(".jar"))
268 _webInfJarMap.put(fileName, file);
269 }
270
271 if (this._jettyEnvXml != null)
272 _envConfig.setJettyEnvXml(Resource.toURL(new File(this._jettyEnvXml)));
273
274
275 super.doStart();
276 }
277
278 public void doStop () throws Exception
279 {
280 if (_classpathFiles != null)
281 _classpathFiles.clear();
282 _classpathFiles = null;
283
284 _classes = null;
285 _testClasses = null;
286
287 if (_webInfJarMap != null)
288 _webInfJarMap.clear();
289
290 _webInfClasses.clear();
291 _webInfJars.clear();
292
293
294
295
296
297 Thread.currentThread().sleep(500L);
298 super.doStop();
299 }
300
301 @Override
302 public Resource getResource(String uriInContext) throws MalformedURLException
303 {
304 Resource resource = null;
305
306 resource = super.getResource(uriInContext);
307
308
309 if ((resource == null || !resource.exists()) && uriInContext != null && _classes != null)
310 {
311 String uri = URIUtil.canonicalPath(uriInContext);
312 if (uri == null)
313 return null;
314
315 try
316 {
317
318 if (uri.startsWith(WEB_INF_CLASSES_PREFIX))
319 {
320 if (uri.equalsIgnoreCase(WEB_INF_CLASSES_PREFIX) || uri.equalsIgnoreCase(WEB_INF_CLASSES_PREFIX+"/"))
321 {
322
323
324 if (_classes != null)
325 return Resource.newResource(_classes);
326 else if (_testClasses != null)
327 return Resource.newResource(_testClasses);
328 }
329 else
330 {
331
332 Resource res = null;
333 int i=0;
334 while (res == null && (i < _webInfClasses.size()))
335 {
336 String newPath = uri.replace(WEB_INF_CLASSES_PREFIX, _webInfClasses.get(i).getPath());
337 res = Resource.newResource(newPath);
338 if (!res.exists())
339 {
340 res = null;
341 i++;
342 }
343 }
344 return res;
345 }
346 }
347 else if (uri.startsWith(WEB_INF_LIB_PREFIX))
348 {
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428