1 //
2 // ========================================================================
3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
8 //
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
11 //
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
14 //
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
17 //
18
19 package org.eclipse.jetty.webapp;
20
21 import java.util.Map;
22 import java.util.concurrent.Callable;
23
24 import org.eclipse.jetty.util.log.Log;
25 import org.eclipse.jetty.util.log.Logger;
26 import org.eclipse.jetty.util.resource.Resource;
27 import org.eclipse.jetty.xml.XmlConfiguration;
28
29
30 /**
31 *
32 * JettyWebConfiguration.
33 *
34 * Looks for XmlConfiguration files in WEB-INF. Searches in order for the first of jetty6-web.xml, jetty-web.xml or web-jetty.xml
35 *
36 *
37 *
38 */
39 public class JettyWebXmlConfiguration extends AbstractConfiguration
40 {
41 private static final Logger LOG = Log.getLogger(JettyWebXmlConfiguration.class);
42
43 /** The value of this property points to the WEB-INF directory of
44 * the web-app currently installed.
45 * it is passed as a property to the jetty-web.xml file */
46 public static final String PROPERTY_THIS_WEB_INF_URL = "this.web-inf.url";
47
48
49 public static final String XML_CONFIGURATION = "org.eclipse.jetty.webapp.JettyWebXmlConfiguration";
50 public static final String JETTY_WEB_XML = "jetty-web.xml";
51
52 /**
53 * Configure
54 * Apply web-jetty.xml configuration
55 * @see Configuration#configure(WebAppContext)
56 */
57 @Override
58 public void configure (WebAppContext context) throws Exception
59 {
60 //cannot configure if the _context is already started
61 if (context.isStarted())
62 {
63 LOG.debug("Cannot configure webapp after it is started");
64 return;
65 }
66
67 LOG.debug("Configuring web-jetty.xml");
68
69 Resource web_inf = context.getWebInf();
70 // handle any WEB-INF descriptors
71 if(web_inf!=null&&web_inf.isDirectory())
72 {
73 // do jetty.xml file
74 Resource jetty=web_inf.addPath("jetty8-web.xml");
75 if(!jetty.exists())
76 jetty=web_inf.addPath(JETTY_WEB_XML);
77 if(!jetty.exists())
78 jetty=web_inf.addPath("web-jetty.xml");
79
80 if(jetty.exists())
81 {
82 if(LOG.isDebugEnabled())
83 LOG.debug("Configure: "+jetty);
84
85 Object xml_attr=context.getAttribute(XML_CONFIGURATION);
86 context.removeAttribute(XML_CONFIGURATION);
87
88 final XmlConfiguration jetty_config = xml_attr instanceof XmlConfiguration
89 ?(XmlConfiguration)xml_attr
90 :new XmlConfiguration(jetty.getURI().toURL());
91 setupXmlConfiguration(jetty_config, web_inf);
92
93 try
94 {
95 final XmlConfiguration config=jetty_config;
96 context.runWithoutCheckingServerClasses(new Callable<Void>()
97 {
98 @Override
99 public Void call() throws Exception
100 {
101 config.configure(context);
102 return null;
103 }
104 });
105 }
106 catch(Exception e)
107 {
108 LOG.warn("Error applying {}",jetty);
109 throw e;
110 }
111 }
112 }
113 }
114
115 /**
116 * Configures some well-known properties before the XmlConfiguration reads
117 * the configuration.
118 * @param jetty_config The configuration object.
119 * @param web_inf the WEB-INF location
120 */
121 private void setupXmlConfiguration(XmlConfiguration jetty_config, Resource web_inf)
122 {
123 Map<String,String> props = jetty_config.getProperties();
124 // TODO - should this be an id rather than a property?
125 props.put(PROPERTY_THIS_WEB_INF_URL, String.valueOf(web_inf.getURL()));
126 }
127 }