1 // ========================================================================
2 // Copyright (c) 2000-2009 Mort Bay Consulting Pty. Ltd.
3 // ------------------------------------------------------------------------
4 // All rights reserved. This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v1.0
6 // and Apache License v2.0 which accompanies this distribution.
7 // The Eclipse Public License is available at
8 // http://www.eclipse.org/legal/epl-v10.html
9 // The Apache License v2.0 is available at
10 // http://www.opensource.org/licenses/apache2.0.php
11 // You may elect to redistribute this code under either of these licenses.
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 * JettyWebConfiguration.
27 *
28 * Looks for Xmlconfiguration files in WEB-INF. Searches in order for the first of jettyX-web.xml, jetty-web.xml or web-jetty.xml
29 *
30 *
31 *
32 */
33 public class JettyWebXmlConfiguration extends AbstractConfiguration
34 {
35 private static final Logger LOG = Log.getLogger(JettyWebXmlConfiguration.class);
36
37 /** The value of this property points to the WEB-INF directory of
38 * the web-app currently installed.
39 * it is passed as a property to the jetty-web.xml file */
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 * Configure
48 * Apply web-jetty.xml configuration
49 * @see Configuration#configure(WebAppContext)
50 */
51 @Override
52 public void configure (WebAppContext context) throws Exception
53 {
54 //cannot configure if the _context is already started
55 if (context.isStarted())
56 {
57 LOG.debug("Cannot configure webapp after it is started");
58 return;
59 }
60
61 LOG.debug("Configuring web-jetty.xml");
62
63 Resource web_inf = context.getWebInf();
64 // handle any WEB-INF descriptors
65 if(web_inf!=null&&web_inf.isDirectory())
66 {
67 // do jetty.xml file
68 Resource jetty=web_inf.addPath("jetty7-web.xml");
69 if(!jetty.exists())
70 jetty=web_inf.addPath(JETTY_WEB_XML);
71 if(!jetty.exists())
72 jetty=web_inf.addPath("web-jetty.xml");
73
74 if(jetty.exists())
75 {
76 // No server classes while configuring
77 String[] old_server_classes = context.getServerClasses();
78 try
79 {
80 context.setServerClasses(null);
81 if(LOG.isDebugEnabled())
82 LOG.debug("Configure: "+jetty);
83
84 XmlConfiguration jetty_config = (XmlConfiguration)context.getAttribute(XML_CONFIGURATION);
85
86 if (jetty_config==null)
87 {
88 jetty_config=new XmlConfiguration(jetty.getURL());
89 }
90 else
91 {
92 context.removeAttribute(XML_CONFIGURATION);
93 }
94 setupXmlConfiguration(context,jetty_config, web_inf);
95 try
96 {
97 jetty_config.configure(context);
98 }
99 catch (ClassNotFoundException e)
100 {
101 LOG.warn("Unable to process jetty-web.xml", e);
102 }
103 }
104 finally
105 {
106 if (context.getServerClasses()==null)
107 context.setServerClasses(old_server_classes);
108 }
109 }
110 }
111 }
112
113 /**
114 * Configures some well-known properties before the XmlConfiguration reads
115 * the configuration.
116 * @param jetty_config The configuration object.
117 */
118 private void setupXmlConfiguration(WebAppContext context, XmlConfiguration jetty_config, Resource web_inf)
119 {
120 setupXmlConfiguration(jetty_config,web_inf);
121 }
122
123 /**
124 * Configures some well-known properties before the XmlConfiguration reads
125 * the configuration.
126 * @param jetty_config The configuration object.
127 */
128 private void setupXmlConfiguration(XmlConfiguration jetty_config, Resource web_inf)
129 {
130 Map<String,String> props = jetty_config.getProperties();
131 props.put(PROPERTY_THIS_WEB_INF_URL, String.valueOf(web_inf.getURL()));
132 }
133 }