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                  // No server classes while configuring
82                  String[] old_server_classes = context.getServerClasses();
83                  try
84                  {
85                      context.setServerClasses(null);
86                      if(LOG.isDebugEnabled())
87                          LOG.debug("Configure: "+jetty);
88  
89                      XmlConfiguration jetty_config = (XmlConfiguration)context.getAttribute(XML_CONFIGURATION);
90  
91                      if (jetty_config==null)
92                      {
93                          jetty_config=new XmlConfiguration(jetty.getURL());
94                      }
95                      else
96                      {
97                          context.removeAttribute(XML_CONFIGURATION);
98                      }
99                      setupXmlConfiguration(jetty_config, web_inf);
100                     try
101                     {
102                         jetty_config.configure(context);
103                     }
104                     catch (ClassNotFoundException e)
105                     {
106                         LOG.warn("Unable to process jetty-web.xml", e);
107                     }
108                 }
109                 finally
110                 {
111                     if (old_server_classes != null)
112                         context.setServerClasses(old_server_classes);
113                 }
114             }
115         }
116     }
117 
118     /**
119      * Configures some well-known properties before the XmlConfiguration reads
120      * the configuration.
121      * @param jetty_config The configuration object.
122      * @param web_inf the WEB-INF location
123      */
124     private void setupXmlConfiguration(XmlConfiguration jetty_config, Resource web_inf)
125     {
126         Map<String,String> props = jetty_config.getProperties();
127         // TODO - should this be an id rather than a property?
128         props.put(PROPERTY_THIS_WEB_INF_URL, String.valueOf(web_inf.getURL()));
129     }
130 }