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.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 jetty6-web.xml, jetty-web.xml or web-jetty.xml
29   *
30   * 
31   *
32   */
33  public class JettyWebXmlConfiguration extends AbstractConfiguration
34  {
35      /** The value of this property points to the WEB-INF directory of
36       * the web-app currently installed.
37       * it is passed as a property to the jetty-web.xml file */
38      public static final String PROPERTY_THIS_WEB_INF_URL = "this.web-inf.url";
39  
40  
41      public static final String XML_CONFIGURATION = "org.eclipse.jetty.webapp.JettyWebXmlConfiguration";
42      public static final String JETTY_WEB_XML = "jetty-web.xml";
43      
44      /** 
45       * Configure
46       * Apply web-jetty.xml configuration
47       * @see Configuration#configure(WebAppContext)
48       */
49      @Override
50      public void configure (WebAppContext context) throws Exception
51      {
52          //cannot configure if the _context is already started
53          if (context.isStarted())
54          {
55              if (Log.isDebugEnabled()){Log.debug("Cannot configure webapp after it is started");}
56              return;
57          }
58          
59          if(Log.isDebugEnabled())
60              Log.debug("Configuring web-jetty.xml");
61          
62          Resource web_inf = context.getWebInf();
63          // handle any WEB-INF descriptors
64          if(web_inf!=null&&web_inf.isDirectory())
65          {
66              // do jetty.xml file
67              Resource jetty=web_inf.addPath("jetty8-web.xml");
68              if(!jetty.exists())
69                  jetty=web_inf.addPath(JETTY_WEB_XML);
70              if(!jetty.exists())
71                  jetty=web_inf.addPath("web-jetty.xml");
72  
73              if(jetty.exists())
74              {
75                  // No server classes while configuring 
76                  String[] old_server_classes = context.getServerClasses();
77                  try
78                  {
79                      context.setServerClasses(null);
80                      if(Log.isDebugEnabled())
81                          Log.debug("Configure: "+jetty);
82                      XmlConfiguration jetty_config = (XmlConfiguration)context.getAttribute(XML_CONFIGURATION);
83                      if (jetty_config==null)
84                          jetty_config=new XmlConfiguration(jetty.getURL());
85                      else
86                          context.removeAttribute(XML_CONFIGURATION);
87                      setupXmlConfiguration(context,jetty_config, web_inf);
88                      jetty_config.configure(context);
89                  }
90                  finally
91                  {
92                      if (context.getServerClasses()==null)
93                          context.setServerClasses(old_server_classes);
94                  }
95              }
96          }
97      }
98  
99      /**
100      * Configures some well-known properties before the XmlConfiguration reads
101      * the configuration.
102      * @param jetty_config The configuration object.
103      */
104     private void setupXmlConfiguration(WebAppContext context, XmlConfiguration jetty_config, Resource web_inf)
105     {
106         setupXmlConfiguration(jetty_config,web_inf);
107     }
108     
109     /**
110      * Configures some well-known properties before the XmlConfiguration reads
111      * the configuration.
112      * @param jetty_config The configuration object.
113      */
114     private void setupXmlConfiguration(XmlConfiguration jetty_config, Resource web_inf)
115     {
116     	Map<String,String> props = jetty_config.getProperties();
117     	if (props == null)
118     	{
119     		props = new HashMap<String, String>();
120     		jetty_config.setProperties(props);
121     	}
122     	
123     	// TODO - should this be an id rather than a property?
124     	props.put(PROPERTY_THIS_WEB_INF_URL, String.valueOf(web_inf.getURL()));
125     }
126     
127 }