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