1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.webapp;
20
21 import java.util.Map;
22
23 import org.eclipse.jetty.util.log.Log;
24 import org.eclipse.jetty.util.log.Logger;
25 import org.eclipse.jetty.util.resource.Resource;
26 import org.eclipse.jetty.xml.XmlConfiguration;
27
28
29
30
31
32
33
34
35
36
37
38 public class JettyWebXmlConfiguration extends AbstractConfiguration
39 {
40 private static final Logger LOG = Log.getLogger(JettyWebXmlConfiguration.class);
41
42
43
44
45 public static final String PROPERTY_THIS_WEB_INF_URL = "this.web-inf.url";
46
47
48 public static final String XML_CONFIGURATION = "org.eclipse.jetty.webapp.JettyWebXmlConfiguration";
49 public static final String JETTY_WEB_XML = "jetty-web.xml";
50
51
52
53
54
55
56 @Override
57 public void configure (WebAppContext context) throws Exception
58 {
59
60 if (context.isStarted())
61 {
62 LOG.debug("Cannot configure webapp after it is started");
63 return;
64 }
65
66 LOG.debug("Configuring web-jetty.xml");
67
68 Resource web_inf = context.getWebInf();
69
70 if(web_inf!=null&&web_inf.isDirectory())
71 {
72
73 Resource jetty=web_inf.addPath("jetty8-web.xml");
74 if(!jetty.exists())
75 jetty=web_inf.addPath(JETTY_WEB_XML);
76 if(!jetty.exists())
77 jetty=web_inf.addPath("web-jetty.xml");
78
79 if(jetty.exists())
80 {
81
82 String[] old_server_classes = context.getServerClasses();
83 try
84 {
85 context.setServerClasses(null);
86 if(LOG.isDebugEnabled())
87 LOG.debug("Configure: "+jetty);
88
89 XmlConfiguration jetty_config = (XmlConfiguration)context.getAttribute(XML_CONFIGURATION);
90
91 if (jetty_config==null)
92 {
93 jetty_config=new XmlConfiguration(jetty.getURL());
94 }
95 else
96 {
97 context.removeAttribute(XML_CONFIGURATION);
98 }
99 setupXmlConfiguration(jetty_config, web_inf);
100 try
101 {
102 jetty_config.configure(context);
103 }
104 catch (ClassNotFoundException e)
105 {
106 LOG.warn("Unable to process jetty-web.xml", e);
107 }
108 }
109 finally
110 {
111 if (old_server_classes != null)
112 context.setServerClasses(old_server_classes);
113 }
114 }
115 }
116 }
117
118
119
120
121
122
123
124 private void setupXmlConfiguration(XmlConfiguration jetty_config, Resource web_inf)
125 {
126 Map<String,String> props = jetty_config.getProperties();
127
128 props.put(PROPERTY_THIS_WEB_INF_URL, String.valueOf(web_inf.getURL()));
129 }
130 }