View Javadoc
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  
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   * JettyWebConfiguration.
32   *
33   * Looks for XmlConfiguration files in WEB-INF.  Searches in order for the first of jetty6-web.xml, jetty-web.xml or web-jetty.xml
34   *
35   *
36   *
37   */
38  public class JettyWebXmlConfiguration extends AbstractConfiguration
39  {
40      private static final Logger LOG = Log.getLogger(JettyWebXmlConfiguration.class);
41  
42      /** The value of this property points to the WEB-INF directory of
43       * the web-app currently installed.
44       * it is passed as a property to the jetty-web.xml file */
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       * Configure
53       * Apply web-jetty.xml configuration
54       * @see Configuration#configure(WebAppContext)
55       */
56      @Override
57      public void configure (WebAppContext context) throws Exception
58      {
59          //cannot configure if the _context is already started
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          // handle any WEB-INF descriptors
70          if(web_inf!=null&&web_inf.isDirectory())
71          {
72              // do jetty.xml file
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                  if(LOG.isDebugEnabled())
82                      LOG.debug("Configure: "+jetty);
83  
84                  Object xml_attr=context.getAttribute(XML_CONFIGURATION);
85                  context.removeAttribute(XML_CONFIGURATION);
86                  final XmlConfiguration jetty_config = xml_attr instanceof XmlConfiguration
87                      ?(XmlConfiguration)xml_attr
88                      :new XmlConfiguration(jetty.getURI().toURL());
89                  setupXmlConfiguration(jetty_config, web_inf);
90                  
91                  try
92                  {
93                      WebAppClassLoader.runWithServerClassAccess(()->{jetty_config.configure(context);return null;});
94                  }
95                  catch(Exception e)
96                  {
97                      LOG.warn("Error applying {}",jetty);
98                      throw e;
99                  }
100             }
101         }
102     }
103 
104     /**
105      * Configures some well-known properties before the XmlConfiguration reads
106      * the configuration.
107      * @param jetty_config The configuration object.
108      * @param web_inf the WEB-INF location
109      */
110     private void setupXmlConfiguration(XmlConfiguration jetty_config, Resource web_inf)
111     {
112         Map<String,String> props = jetty_config.getProperties();
113         // TODO - should this be an id rather than a property?
114         props.put(PROPERTY_THIS_WEB_INF_URL, String.valueOf(web_inf.getURI()));
115     }
116 }