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