1
2
3
4
5
6
7
8
9
10
11
12
13 package org.eclipse.jetty.webapp;
14
15 import java.io.IOException;
16 import java.net.MalformedURLException;
17 import java.util.List;
18
19 import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
20 import org.eclipse.jetty.servlet.ServletHandler;
21 import org.eclipse.jetty.util.log.Log;
22 import org.eclipse.jetty.util.resource.Resource;
23
24
25
26
27
28
29 public class WebXmlConfiguration extends AbstractConfiguration
30 {
31
32
33
34
35
36 @Override
37 public void preConfigure (WebAppContext context) throws Exception
38 {
39
40 String defaultsDescriptor = context.getDefaultsDescriptor();
41 if (defaultsDescriptor != null && defaultsDescriptor.length() > 0)
42 {
43 Resource dftResource = Resource.newSystemResource(defaultsDescriptor);
44 if (dftResource == null)
45 dftResource = context.newResource(defaultsDescriptor);
46 context.getMetaData().setDefaults (dftResource);
47 }
48
49
50 Resource webxml = findWebXml(context);
51 if (webxml != null)
52 {
53 context.getMetaData().setWebXml(webxml);
54 }
55
56
57 for (String overrideDescriptor : context.getOverrideDescriptors())
58 {
59 if (overrideDescriptor != null && overrideDescriptor.length() > 0)
60 {
61 Resource orideResource = Resource.newSystemResource(overrideDescriptor);
62 if (orideResource == null)
63 orideResource = context.newResource(overrideDescriptor);
64 context.getMetaData().addOverride(orideResource);
65 }
66 }
67 }
68
69
70
71
72
73
74 @Override
75 public void configure (WebAppContext context) throws Exception
76 {
77
78 if (context.isStarted())
79 {
80 if (Log.isDebugEnabled()) Log.debug("Cannot configure webapp after it is started");
81 return;
82 }
83
84 context.getMetaData().addDescriptorProcessor(new StandardDescriptorProcessor());
85 }
86
87
88 protected Resource findWebXml(WebAppContext context) throws IOException, MalformedURLException
89 {
90 String descriptor = context.getDescriptor();
91 if (descriptor != null)
92 {
93 Resource web = context.newResource(descriptor);
94 if (web.exists() && !web.isDirectory()) return web;
95 }
96
97 Resource web_inf = context.getWebInf();
98 if (web_inf != null && web_inf.isDirectory())
99 {
100
101 Resource web = web_inf.addPath("web.xml");
102 if (web.exists()) return web;
103 Log.debug("No WEB-INF/web.xml in " + context.getWar() + ". Serving files and default/dynamic servlets only");
104 }
105 return null;
106 }
107
108
109
110 @Override
111 public void deconfigure (WebAppContext context) throws Exception
112 {
113
114
115 ServletHandler _servletHandler = context.getServletHandler();
116
117 _servletHandler.setFilters(null);
118 _servletHandler.setFilterMappings(null);
119 _servletHandler.setServlets(null);
120 _servletHandler.setServletMappings(null);
121
122 context.setEventListeners(null);
123 context.setWelcomeFiles(null);
124
125 if (context.getErrorHandler() instanceof ErrorPageErrorHandler)
126 ((ErrorPageErrorHandler)
127 context.getErrorHandler()).setErrorPages(null);
128
129
130
131
132 }
133 }