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