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         ClassLoader old_loader = Thread.currentThread().getContextClassLoader();
189         Thread.currentThread().setContextClassLoader(context.getClassLoader());
190         ContextFactory.associateClassLoader(context.getClassLoader());
191 
192         try
193         {
194             //unbind any NamingEntries that were configured in this webapp's name space
195             
196             NamingContext scopeContext = (NamingContext)NamingEntryUtil.getContextForScope(context);
197             scopeContext.getParent().destroySubcontext(scopeContext.getName());
198         }
199         catch (NameNotFoundException e)
200         {
201             LOG.ignore(e);
202             LOG.debug("No naming entries configured in environment for webapp "+context);
203         }
204         finally
205         {
206             ContextFactory.disassociateClassLoader();
207             Thread.currentThread().setContextClassLoader(old_loader);
208         }
209     }
210 
211     /**
212      * Bind all EnvEntries that have been declared, so that the processing of the
213      * web.xml file can potentially override them.
214      *
215      * We first bind EnvEntries declared in Server scope, then WebAppContext scope.
216      * @throws NamingException
217      */
218     public void bindEnvEntries (WebAppContext context)
219     throws NamingException
220     {
221         LOG.debug("Binding env entries from the jvm scope");
222         InitialContext ic = new InitialContext();
223         Context envCtx = (Context)ic.lookup("java:comp/env");
224         Object scope = null;
225         List<Object> list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
226         Iterator<Object> itor = list.iterator();
227         while (itor.hasNext())
228         {
229             EnvEntry ee = (EnvEntry)itor.next();
230             ee.bindToENC(ee.getJndiName());
231             Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
232             NamingUtil.bind(envCtx, namingEntryName.toString(), ee);//also save the EnvEntry in the context so we can check it later
233         }
234 
235         LOG.debug("Binding env entries from the server scope");
236 
237         scope = context.getServer();
238         list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
239         itor = list.iterator();
240         while (itor.hasNext())
241         {
242             EnvEntry ee = (EnvEntry)itor.next();
243             ee.bindToENC(ee.getJndiName());
244             Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
245             NamingUtil.bind(envCtx, namingEntryName.toString(), ee);//also save the EnvEntry in the context so we can check it later
246         }
247 
248         LOG.debug("Binding env entries from the context scope");
249         scope = context;
250         list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
251         itor = list.iterator();
252         while (itor.hasNext())
253         {
254             EnvEntry ee = (EnvEntry)itor.next();
255             ee.bindToENC(ee.getJndiName());
256             Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
257             NamingUtil.bind(envCtx, namingEntryName.toString(), ee);//also save the EnvEntry in the context so we can check it later
258         }
259     }
260 
261     protected void createEnvContext (WebAppContext wac)
262     throws NamingException
263     {
264         ClassLoader old_loader = Thread.currentThread().getContextClassLoader();
265         Thread.currentThread().setContextClassLoader(wac.getClassLoader());
266         ContextFactory.associateClassLoader(wac.getClassLoader());
267         try
268         {
269             Context context = new InitialContext();
270             Context compCtx =  (Context)context.lookup ("java:comp");
271             compCtx.createSubcontext("env");
272         }
273         finally
274         {
275             ContextFactory.disassociateClassLoader();
276             Thread.currentThread().setContextClassLoader(old_loader);
277         }
278     }
279 
280     private static class Bound
281     {
282         final NamingContext _context;
283         final String _name;
284         Bound(NamingContext context, String name)
285         {
286             _context=context;
287             _name=name;
288         }
289     }
290 }