View Javadoc

1   package org.eclipse.jetty.deploy.bindings;
2   //========================================================================
3   //Copyright (c) Webtide LLC
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.apache.org/licenses/LICENSE-2.0.txt
14  //
15  //You may elect to redistribute this code under either of these licenses. 
16  //========================================================================
17  
18  import java.net.URL;
19  
20  import org.eclipse.jetty.deploy.App;
21  import org.eclipse.jetty.deploy.AppLifeCycle;
22  import org.eclipse.jetty.deploy.graph.Node;
23  import org.eclipse.jetty.server.handler.ContextHandler;
24  import org.eclipse.jetty.util.log.Log;
25  import org.eclipse.jetty.util.log.Logger;
26  import org.eclipse.jetty.util.resource.FileResource;
27  import org.eclipse.jetty.util.resource.Resource;
28  import org.eclipse.jetty.webapp.WebAppContext;
29  import org.eclipse.jetty.xml.XmlConfiguration;
30  
31  /**
32   * Provides a way of globally setting various aspects of webapp contexts.
33   * 
34   * Adding this binding will allow the user to arbitrarily apply a file of 
35   * jetty-web.xml like settings to a webapp context.
36   * 
37   * Example usage would be:
38   * - adding a server or system class setting to all webapp contexts
39   * - adding an override descriptor 
40   * 
41   * Note: Currently properties from startup will not be available for 
42   * reference.
43   *
44   */
45  public class GlobalWebappConfigBinding implements AppLifeCycle.Binding
46  {
47      private static final Logger LOG = Log.getLogger(GlobalWebappConfigBinding.class);
48  
49  
50      private String _jettyXml;
51  
52      public String getJettyXml()
53      {
54          return _jettyXml;
55      }
56  
57      public void setJettyXml(String jettyXml)
58      {
59          this._jettyXml = jettyXml;
60      }
61  
62      public String[] getBindingTargets()
63      {
64          return new String[]  { "deploying" };
65      }
66  
67      public void processBinding(Node node, App app) throws Exception
68      {
69          ContextHandler handler = app.getContextHandler();
70          if (handler == null)
71          {
72              throw new NullPointerException("No Handler created for App: " + app);
73          }
74  
75          if (handler instanceof WebAppContext)
76          {
77              WebAppContext context = (WebAppContext)handler;
78  
79              if (LOG.isDebugEnabled())
80              {
81                  LOG.debug("Binding: Configuring webapp context with global settings from: " + _jettyXml);
82              }
83  
84              if ( _jettyXml == null )
85              {
86                  LOG.warn("Binding: global context binding is enabled but no jetty-web.xml file has been registered");
87              }
88              
89              Resource globalContextSettings = Resource.newResource(_jettyXml);
90  
91              if (globalContextSettings.exists())
92              {
93                  XmlConfiguration jettyXmlConfig = new XmlConfiguration(globalContextSettings.getInputStream());
94  
95                  jettyXmlConfig.configure(context);
96              }
97              else
98              {
99                  LOG.info("Binding: Unable to locate global webapp context settings: " + _jettyXml);
100             }
101         }
102     }
103 
104 }