View Javadoc

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