View Javadoc

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.HashMap;
17  import java.util.Map;
18  
19  import org.eclipse.jetty.util.log.Log;
20  import org.eclipse.jetty.util.log.Logger;
21  import org.eclipse.jetty.util.resource.Resource;
22  import org.eclipse.jetty.xml.XmlConfiguration;
23  
24  
25  /**
26   * 
27   * JettyWebConfiguration.
28   * 
29   * Looks for Xmlconfiguration files in WEB-INF.  Searches in order for the first of jetty6-web.xml, jetty-web.xml or web-jetty.xml
30   *
31   * 
32   *
33   */
34  public class JettyWebXmlConfiguration extends AbstractConfiguration
35  {
36      private static final Logger LOG = Log.getLogger(JettyWebXmlConfiguration.class);
37  
38      /** The value of this property points to the WEB-INF directory of
39       * the web-app currently installed.
40       * it is passed as a property to the jetty-web.xml file */
41      public static final String PROPERTY_THIS_WEB_INF_URL = "this.web-inf.url";
42  
43  
44      public static final String XML_CONFIGURATION = "org.eclipse.jetty.webapp.JettyWebXmlConfiguration";
45      public static final String JETTY_WEB_XML = "jetty-web.xml";
46      
47      /** 
48       * Configure
49       * Apply web-jetty.xml configuration
50       * @see Configuration#configure(WebAppContext)
51       */
52      @Override
53      public void configure (WebAppContext context) throws Exception
54      {
55          //cannot configure if the _context is already started
56          if (context.isStarted())
57          {
58              LOG.debug("Cannot configure webapp after it is started");
59              return;
60          }
61          
62          LOG.debug("Configuring web-jetty.xml");
63          
64          Resource web_inf = context.getWebInf();
65          // handle any WEB-INF descriptors
66          if(web_inf!=null&&web_inf.isDirectory())
67          {
68              // do jetty.xml file
69              Resource jetty=web_inf.addPath("jetty8-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                  // No server classes while configuring 
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                      XmlConfiguration jetty_config = (XmlConfiguration)context.getAttribute(XML_CONFIGURATION);
86                      
87                      if (jetty_config==null)
88                      {
89                          jetty_config=new XmlConfiguration(jetty.getURL());
90                      }
91                      else
92                      {
93                          context.removeAttribute(XML_CONFIGURATION);
94                      }
95                      setupXmlConfiguration(context,jetty_config, web_inf);
96                      try
97                      {
98                          jetty_config.configure(context);
99                      }
100                     catch (ClassNotFoundException e)
101                     {
102                         LOG.warn("Unable to process jetty-web.xml", e);
103                     }
104                 }
105                 finally
106                 {
107                     if (context.getServerClasses()==null)
108                         context.setServerClasses(old_server_classes);
109                 }
110             }
111         }
112     }
113 
114     /**
115      * Configures some well-known properties before the XmlConfiguration reads
116      * the configuration.
117      * @param jetty_config The configuration object.
118      */
119     private void setupXmlConfiguration(WebAppContext context, XmlConfiguration jetty_config, Resource web_inf)
120     {
121         setupXmlConfiguration(jetty_config,web_inf);
122     }
123     
124     /**
125      * Configures some well-known properties before the XmlConfiguration reads
126      * the configuration.
127      * @param jetty_config The configuration object.
128      */
129     private void setupXmlConfiguration(XmlConfiguration jetty_config, Resource web_inf)
130     {
131     	Map<String,String> props = jetty_config.getProperties();
132     	if (props == null)
133     	{
134     		props = new HashMap<String, String>();
135     		jetty_config.setProperties(props);
136     	}
137     	
138     	// TODO - should this be an id rather than a property?
139     	props.put(PROPERTY_THIS_WEB_INF_URL, String.valueOf(web_inf.getURL()));
140     }
141 }