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 }
57
58
59 for (String overrideDescriptor : context.getOverrideDescriptors())
60 {
61 if (overrideDescriptor != null && overrideDescriptor.length() > 0)
62 {
63 Resource orideResource = Resource.newSystemResource(overrideDescriptor);
64 if (orideResource == null)
65 orideResource = context.newResource(overrideDescriptor);
66 context.getMetaData().addOverride(orideResource);
67 }
68 }
69 }
70
71
72
73
74
75
76 @Override
77 public void configure (WebAppContext context) throws Exception
78 {
79
80 if (context.isStarted())
81 {
82 LOG.debug("Cannot configure webapp after it is started");
83 return;
84 }
85
86 context.getMetaData().addDescriptorProcessor(new StandardDescriptorProcessor());
87 }
88
89
90 protected Resource findWebXml(WebAppContext context) throws IOException, MalformedURLException
91 {
92 String descriptor = context.getDescriptor();
93 if (descriptor != null)
94 {
95 Resource web = context.newResource(descriptor);
96 if (web.exists() && !web.isDirectory()) return web;
97 }
98
99 Resource web_inf = context.getWebInf();
100 if (web_inf != null && web_inf.isDirectory())
101 {
102
103 Resource web = web_inf.addPath("web.xml");
104 if (web.exists()) return web;
105 if (LOG.isDebugEnabled())
106 LOG.debug("No WEB-INF/web.xml in " + context.getWar() + ". Serving files and default/dynamic servlets only");
107 }
108 return null;
109 }
110
111
112
113 @Override
114 public void deconfigure (WebAppContext context) throws Exception
115 {
116
117
118 ServletHandler _servletHandler = context.getServletHandler();
119
120 _servletHandler.setFilters(null);
121 _servletHandler.setFilterMappings(null);
122 _servletHandler.setServlets(null);
123 _servletHandler.setServletMappings(null);
124
125 context.setEventListeners(null);
126 context.setWelcomeFiles(null);
127
128 if (context.getErrorHandler() instanceof ErrorPageErrorHandler)
129 ((ErrorPageErrorHandler)
130 context.getErrorHandler()).setErrorPages(null);
131
132
133
134
135 }
136 }