View Javadoc

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