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