View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 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.plus.webapp;
20  
21  import java.net.URL;
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  import javax.naming.Binding;
28  import javax.naming.Context;
29  import javax.naming.InitialContext;
30  import javax.naming.Name;
31  import javax.naming.NameNotFoundException;
32  import javax.naming.NamingException;
33  
34  import org.eclipse.jetty.jndi.ContextFactory;
35  import org.eclipse.jetty.jndi.NamingContext;
36  import org.eclipse.jetty.jndi.NamingUtil;
37  import org.eclipse.jetty.jndi.local.localContextRoot;
38  import org.eclipse.jetty.plus.jndi.EnvEntry;
39  import org.eclipse.jetty.plus.jndi.NamingEntryUtil;
40  import org.eclipse.jetty.util.log.Log;
41  import org.eclipse.jetty.util.log.Logger;
42  import org.eclipse.jetty.webapp.AbstractConfiguration;
43  import org.eclipse.jetty.webapp.WebAppContext;
44  import org.eclipse.jetty.xml.XmlConfiguration;
45  
46  
47  /**
48   * EnvConfiguration
49   *
50   *
51   */
52  public class EnvConfiguration extends AbstractConfiguration
53  {
54      private static final Logger LOG = Log.getLogger(EnvConfiguration.class);
55  
56      private static final String JETTY_ENV_BINDINGS = "org.eclipse.jetty.jndi.EnvConfiguration";
57      private URL jettyEnvXmlUrl;
58  
59      public void setJettyEnvXml (URL url)
60      {
61          this.jettyEnvXmlUrl = url;
62      }
63  
64      /**
65       * @see Configuration#configure(WebAppContext)
66       * @throws Exception
67       */
68      @Override
69      public void preConfigure (WebAppContext context) throws Exception
70      {
71          //create a java:comp/env
72          createEnvContext(context);
73      }
74  
75      /**
76       * @throws Exception
77       */
78      @Override
79      public void configure (WebAppContext context) throws Exception
80      {
81          if (LOG.isDebugEnabled())
82              LOG.debug("Created java:comp/env for webapp "+context.getContextPath());
83  
84          //check to see if an explicit file has been set, if not,
85          //look in WEB-INF/jetty-env.xml
86          if (jettyEnvXmlUrl == null)
87          {
88              //look for a file called WEB-INF/jetty-env.xml
89              //and process it if it exists
90              org.eclipse.jetty.util.resource.Resource web_inf = context.getWebInf();
91              if(web_inf!=null && web_inf.isDirectory())
92              {
93                  org.eclipse.jetty.util.resource.Resource jettyEnv = web_inf.addPath("jetty-env.xml");
94                  if(jettyEnv.exists())
95                  {
96                      jettyEnvXmlUrl = jettyEnv.getURL();
97                  }
98              }
99          }
100 
101         if (jettyEnvXmlUrl != null)
102         {
103             synchronized (localContextRoot.getRoot())
104             {
105                 // create list and listener to remember the bindings we make.
106                 final List<Bound> bindings = new ArrayList<Bound>();
107                 NamingContext.Listener listener = new NamingContext.Listener()
108                 {
109                     public void unbind(NamingContext ctx, Binding binding)
110                     {
111                     }
112 
113                     public Binding bind(NamingContext ctx, Binding binding)
114                     {
115                         bindings.add(new Bound(ctx,binding.getName()));
116                         return binding;
117                     }
118                 };
119 
120                 try
121                 {
122                     localContextRoot.getRoot().addListener(listener);
123                     XmlConfiguration configuration = new XmlConfiguration(jettyEnvXmlUrl);
124                     configuration.configure(context);
125                 }
126                 finally
127                 {
128                     localContextRoot.getRoot().removeListener(listener);
129                     context.setAttribute(JETTY_ENV_BINDINGS,bindings);
130                 }
131             }
132         }
133 
134         //add java:comp/env entries for any EnvEntries that have been defined so far
135         bindEnvEntries(context);
136     }
137 
138 
139     /**
140      * Remove jndi setup from start
141      * @see Configuration#deconfigure(WebAppContext)
142      * @throws Exception
143      */
144     @Override
145     public void deconfigure (WebAppContext context) throws Exception
146     {
147         //get rid of any bindings for comp/env for webapp
148         ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
149         Thread.currentThread().setContextClassLoader(context.getClassLoader());
150         ContextFactory.associateClassLoader(context.getClassLoader());
151         try
152         {
153             Context ic = new InitialContext();
154             Context compCtx =  (Context)ic.lookup ("java:comp");
155             compCtx.destroySubcontext("env");
156 
157             //unbind any NamingEntries that were configured in this webapp's name space
158             @SuppressWarnings("unchecked")
159             List<Bound> bindings = (List<Bound>)context.getAttribute(JETTY_ENV_BINDINGS);
160             context.setAttribute(JETTY_ENV_BINDINGS,null);
161             if (bindings!=null)
162             {
163                 Collections.reverse(bindings);
164                 for (Bound b:bindings)
165                     b._context.destroySubcontext(b._name);
166             }
167         }
168         catch (NameNotFoundException e)
169         {
170             LOG.warn(e);
171         }
172         finally
173         {
174             ContextFactory.disassociateClassLoader();
175             Thread.currentThread().setContextClassLoader(oldLoader);
176         }
177     }
178 
179 
180     /**
181      * Remove all jndi setup
182      * @see Configuration#deconfigure(WebAppContext)
183      * @throws Exception
184      */
185     @Override
186     public void destroy (WebAppContext context) throws Exception
187     {
188         try
189         {
190             //unbind any NamingEntries that were configured in this webapp's name space
191             NamingContext scopeContext = (NamingContext)NamingEntryUtil.getContextForScope(context);
192             scopeContext.getParent().destroySubcontext(scopeContext.getName());
193         }
194         catch (NameNotFoundException e)
195         {
196             LOG.ignore(e);
197             LOG.debug("No naming entries configured in environment for webapp "+context);
198         }
199     }
200 
201     /**
202      * Bind all EnvEntries that have been declared, so that the processing of the
203      * web.xml file can potentially override them.
204      *
205      * We first bind EnvEntries declared in Server scope, then WebAppContext scope.
206      * @throws NamingException
207      */
208     public void bindEnvEntries (WebAppContext context)
209     throws NamingException
210     {
211         LOG.debug("Binding env entries from the jvm scope");
212         InitialContext ic = new InitialContext();
213         Context envCtx = (Context)ic.lookup("java:comp/env");
214         Object scope = null;
215         List<Object> list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
216         Iterator<Object> itor = list.iterator();
217         while (itor.hasNext())
218         {
219             EnvEntry ee = (EnvEntry)itor.next();
220             ee.bindToENC(ee.getJndiName());
221             Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
222             NamingUtil.bind(envCtx, namingEntryName.toString(), ee);//also save the EnvEntry in the context so we can check it later
223         }
224 
225         LOG.debug("Binding env entries from the server scope");
226 
227         scope = context.getServer();
228         list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
229         itor = list.iterator();
230         while (itor.hasNext())
231         {
232             EnvEntry ee = (EnvEntry)itor.next();
233             ee.bindToENC(ee.getJndiName());
234             Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
235             NamingUtil.bind(envCtx, namingEntryName.toString(), ee);//also save the EnvEntry in the context so we can check it later
236         }
237 
238         LOG.debug("Binding env entries from the context scope");
239         scope = context;
240         list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
241         itor = list.iterator();
242         while (itor.hasNext())
243         {
244             EnvEntry ee = (EnvEntry)itor.next();
245             ee.bindToENC(ee.getJndiName());
246             Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
247             NamingUtil.bind(envCtx, namingEntryName.toString(), ee);//also save the EnvEntry in the context so we can check it later
248         }
249     }
250 
251     protected void createEnvContext (WebAppContext wac)
252     throws NamingException
253     {
254         ClassLoader old_loader = Thread.currentThread().getContextClassLoader();
255         Thread.currentThread().setContextClassLoader(wac.getClassLoader());
256         ContextFactory.associateClassLoader(wac.getClassLoader());
257         try
258         {
259             Context context = new InitialContext();
260             Context compCtx =  (Context)context.lookup ("java:comp");
261             compCtx.createSubcontext("env");
262         }
263         finally
264         {
265             ContextFactory.disassociateClassLoader();
266             Thread.currentThread().setContextClassLoader(old_loader);
267         }
268     }
269 
270     private static class Bound
271     {
272         final NamingContext _context;
273         final String _name;
274         Bound(NamingContext context, String name)
275         {
276             _context=context;
277             _name=name;
278         }
279     }
280 }