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.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.NamingContext;
35  import org.eclipse.jetty.jndi.NamingUtil;
36  import org.eclipse.jetty.jndi.local.localContextRoot;
37  import org.eclipse.jetty.plus.jndi.EnvEntry;
38  import org.eclipse.jetty.plus.jndi.NamingEntryUtil;
39  import org.eclipse.jetty.util.log.Log;
40  import org.eclipse.jetty.util.log.Logger;
41  import org.eclipse.jetty.webapp.AbstractConfiguration;
42  import org.eclipse.jetty.webapp.Configuration;
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         try
151         {
152             Context ic = new InitialContext();
153             Context compCtx =  (Context)ic.lookup ("java:comp");
154             compCtx.destroySubcontext("env");
155 
156             //unbind any NamingEntries that were configured in this webapp's name space
157             @SuppressWarnings("unchecked")
158             List<Bound> bindings = (List<Bound>)context.getAttribute(JETTY_ENV_BINDINGS);
159             context.setAttribute(JETTY_ENV_BINDINGS,null);
160             if (bindings!=null)
161             {
162                 Collections.reverse(bindings);
163                 for (Bound b:bindings)
164                     b._context.destroySubcontext(b._name);
165             }
166         }
167         catch (NameNotFoundException e)
168         {
169             LOG.warn(e);
170         }
171         finally
172         {
173             Thread.currentThread().setContextClassLoader(oldLoader);
174         }
175     }
176 
177     
178     /** 
179      * Remove all jndi setup
180      * @see Configuration#deconfigure(WebAppContext)
181      * @throws Exception
182      */
183     @Override
184     public void destroy (WebAppContext context) throws Exception
185     {
186         try
187         {            
188             //unbind any NamingEntries that were configured in this webapp's name space
189             NamingContext scopeContext = (NamingContext)NamingEntryUtil.getContextForScope(context);
190             scopeContext.getParent().destroySubcontext(scopeContext.getName());
191         }
192         catch (NameNotFoundException e)
193         {
194             LOG.ignore(e);
195             LOG.debug("No naming entries configured in environment for webapp "+context);
196         }
197     }
198     
199     /**
200      * Bind all EnvEntries that have been declared, so that the processing of the
201      * web.xml file can potentially override them.
202      * 
203      * We first bind EnvEntries declared in Server scope, then WebAppContext scope.
204      * @throws NamingException
205      */
206     public void bindEnvEntries (WebAppContext context)
207     throws NamingException
208     {
209         LOG.debug("Binding env entries from the jvm scope");
210         InitialContext ic = new InitialContext();
211         Context envCtx = (Context)ic.lookup("java:comp/env");
212         Object scope = null;
213         List<Object> list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
214         Iterator<Object> itor = list.iterator();
215         while (itor.hasNext())
216         {
217             EnvEntry ee = (EnvEntry)itor.next();
218             ee.bindToENC(ee.getJndiName());
219             Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
220             NamingUtil.bind(envCtx, namingEntryName.toString(), ee);//also save the EnvEntry in the context so we can check it later          
221         }
222         
223         LOG.debug("Binding env entries from the server scope");
224         
225         scope = context.getServer();
226         list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
227         itor = list.iterator();
228         while (itor.hasNext())
229         {
230             EnvEntry ee = (EnvEntry)itor.next();
231             ee.bindToENC(ee.getJndiName());
232             Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
233             NamingUtil.bind(envCtx, namingEntryName.toString(), ee);//also save the EnvEntry in the context so we can check it later          
234         }
235         
236         LOG.debug("Binding env entries from the context scope");
237         scope = context;
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     
249     protected void createEnvContext (WebAppContext wac)
250     throws NamingException
251     {
252         ClassLoader old_loader = Thread.currentThread().getContextClassLoader();
253         Thread.currentThread().setContextClassLoader(wac.getClassLoader());
254         try
255         {
256             Context context = new InitialContext();
257             Context compCtx =  (Context)context.lookup ("java:comp");
258             compCtx.createSubcontext("env");
259         }
260         finally 
261         {
262            Thread.currentThread().setContextClassLoader(old_loader);
263        }
264     }
265     
266     private static class Bound
267     {
268         final NamingContext _context;
269         final String _name;
270         Bound(NamingContext context, String name)
271         {
272             _context=context;
273             _name=name;
274         }
275     }
276 }