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 jettyX-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      
42      /** 
43       * Configure
44       * Apply web-jetty.xml configuration
45       * @see Configuration#configure(WebAppContext)
46       */
47      @Override
48      public void configure (WebAppContext context) throws Exception
49      {
50          //cannot configure if the _context is already started
51          if (context.isStarted())
52          {
53              if (Log.isDebugEnabled()){Log.debug("Cannot configure webapp after it is started");}
54              return;
55          }
56          
57          if(Log.isDebugEnabled())
58              Log.debug("Configuring web-jetty.xml");
59          
60          Resource web_inf = context.getWebInf();
61          // handle any WEB-INF descriptors
62          if(web_inf!=null&&web_inf.isDirectory())
63          {
64              // do jetty.xml file
65              Resource jetty=web_inf.addPath("jetty7-web.xml");
66              if(!jetty.exists())
67                  jetty=web_inf.addPath("jetty-web.xml");
68              if(!jetty.exists())
69                  jetty=web_inf.addPath("web-jetty.xml");
70  
71              if(jetty.exists())
72              {
73                  // No server classes while configuring 
74                  String[] old_server_classes = context.getServerClasses();
75                  try
76                  {
77                      context.setServerClasses(null);
78                      if(Log.isDebugEnabled())
79                          Log.debug("Configure: "+jetty);
80                      XmlConfiguration jetty_config=new XmlConfiguration(jetty.getURL());
81                      setupXmlConfiguration(context,jetty_config, web_inf);
82                      jetty_config.configure(context);
83                  }
84                  finally
85                  {
86                      if (context.getServerClasses()==null)
87                          context.setServerClasses(old_server_classes);
88                  }
89              }
90          }
91      }
92  
93      /**
94       * Configures some well-known properties before the XmlConfiguration reads
95       * the configuration.
96       * @param jetty_config The configuration object.
97       */
98      private void setupXmlConfiguration(WebAppContext context, XmlConfiguration jetty_config, Resource web_inf)
99      {
100         setupXmlConfiguration(jetty_config,web_inf);
101     }
102     
103     /**
104      * Configures some well-known properties before the XmlConfiguration reads
105      * the configuration.
106      * @param jetty_config The configuration object.
107      */
108     private void setupXmlConfiguration(XmlConfiguration jetty_config, Resource web_inf)
109     {
110     	Map<String,String> props = jetty_config.getProperties();
111     	props.put(PROPERTY_THIS_WEB_INF_URL, String.valueOf(web_inf.getURL()));
112     }
113     
114 }