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