View Javadoc

1   // ========================================================================
2   // Copyright (c) 1999-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.plus.webapp;
15  
16  import java.net.URL;
17  import java.util.Iterator;
18  import java.util.List;
19  
20  import javax.naming.Context;
21  import javax.naming.InitialContext;
22  import javax.naming.Name;
23  import javax.naming.NameNotFoundException;
24  import javax.naming.NamingException;
25  
26  import org.eclipse.jetty.jndi.NamingUtil;
27  import org.eclipse.jetty.plus.jndi.EnvEntry;
28  import org.eclipse.jetty.plus.jndi.NamingEntry;
29  import org.eclipse.jetty.plus.jndi.NamingEntryUtil;
30  import org.eclipse.jetty.util.log.Log;
31  import org.eclipse.jetty.webapp.Configuration;
32  import org.eclipse.jetty.webapp.WebAppContext;
33  import org.eclipse.jetty.xml.XmlConfiguration;
34  
35  
36  /**
37   * EnvConfiguration
38   *
39   *
40   */
41  public class EnvConfiguration implements Configuration
42  {
43      private URL jettyEnvXmlUrl;
44  
45    
46   
47  
48      public void setJettyEnvXml (URL url)
49      {
50          this.jettyEnvXmlUrl = url;
51      }
52      
53   
54  
55      /** 
56       * @see org.eclipse.jetty.webapp.Configuration#configureDefaults()
57       * @throws Exception
58       */
59      public void preConfigure (WebAppContext context) throws Exception
60      {        
61          //create a java:comp/env
62          createEnvContext(context);
63      }
64  
65      /** 
66       * @throws Exception
67       */
68      public void configure (WebAppContext context) throws Exception
69      {  
70          if (Log.isDebugEnabled())
71              Log.debug("Created java:comp/env for webapp "+context.getContextPath());
72          
73          //check to see if an explicit file has been set, if not,
74          //look in WEB-INF/jetty-env.xml
75          if (jettyEnvXmlUrl == null)
76          {
77              
78              //look for a file called WEB-INF/jetty-env.xml
79              //and process it if it exists
80              org.eclipse.jetty.util.resource.Resource web_inf = context.getWebInf();
81              if(web_inf!=null && web_inf.isDirectory())
82              {
83                  org.eclipse.jetty.util.resource.Resource jettyEnv = web_inf.addPath("jetty-env.xml");
84                  if(jettyEnv.exists())
85                  {
86                      jettyEnvXmlUrl = jettyEnv.getURL();
87                  }
88              }
89          }
90          
91          //apply the jetty-env.xml file
92          if (jettyEnvXmlUrl != null)
93          {
94              XmlConfiguration configuration = new XmlConfiguration(jettyEnvXmlUrl);
95              configuration.configure(context);
96          }
97       
98          //add java:comp/env entries for any EnvEntries that have been defined so far
99          bindEnvEntries(context);
100     }
101 
102     public void postConfigure(WebAppContext context) throws Exception
103     {
104         // TODO Auto-generated method stub
105         
106     }
107     
108     
109     /** 
110      * Remove all jndi setup
111      * @see org.eclipse.jetty.webapp.Configuration#deconfigureWebApp()
112      * @throws Exception
113      */
114     public void deconfigure (WebAppContext context) throws Exception
115     {
116         //get rid of any bindings for comp/env for webapp
117         ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
118         Thread.currentThread().setContextClassLoader(context.getClassLoader());
119         try
120         {
121             Context ic = new InitialContext();
122             Context compCtx =  (Context)ic.lookup ("java:comp");
123             compCtx.destroySubcontext("env");
124         }
125         catch (NameNotFoundException e)
126         {
127             Log.warn(e);
128         }
129 
130         //unbind any NamingEntries that were configured in this webapp's name space
131         try
132         {
133             Context scopeContext = NamingEntryUtil.getContextForScope(context);
134             scopeContext.destroySubcontext(NamingEntry.__contextName);
135         }
136         catch (NameNotFoundException e)
137         {
138             Log.ignore(e);
139             Log.debug("No naming entries configured in environment for webapp "+context);
140         }
141         Thread.currentThread().setContextClassLoader(oldLoader);
142     }
143     
144     /**
145      * Bind all EnvEntries that have been declared, so that the processing of the
146      * web.xml file can potentially override them.
147      * 
148      * We first bind EnvEntries declared in Server scope, then WebAppContext scope.
149      * @throws NamingException
150      */
151     public void bindEnvEntries (WebAppContext context)
152     throws NamingException
153     {
154         Log.debug("Binding env entries from the jvm scope");
155         InitialContext ic = new InitialContext();
156         Context envCtx = (Context)ic.lookup("java:comp/env");
157         Object scope = null;
158         List list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
159         Iterator itor = list.iterator();
160         while (itor.hasNext())
161         {
162             EnvEntry ee = (EnvEntry)itor.next();
163             ee.bindToENC(ee.getJndiName());
164             Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
165             NamingUtil.bind(envCtx, namingEntryName.toString(), ee);//also save the EnvEntry in the context so we can check it later          
166         }
167         
168         Log.debug("Binding env entries from the server scope");
169         
170         scope = context.getServer();
171         list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
172         itor = list.iterator();
173         while (itor.hasNext())
174         {
175             EnvEntry ee = (EnvEntry)itor.next();
176             ee.bindToENC(ee.getJndiName());
177             Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
178             NamingUtil.bind(envCtx, namingEntryName.toString(), ee);//also save the EnvEntry in the context so we can check it later          
179         }
180         
181         Log.debug("Binding env entries from the context scope");
182         scope = context;
183         list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
184         itor = list.iterator();
185         while (itor.hasNext())
186         {
187             EnvEntry ee = (EnvEntry)itor.next();
188             ee.bindToENC(ee.getJndiName());
189             Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
190             NamingUtil.bind(envCtx, namingEntryName.toString(), ee);//also save the EnvEntry in the context so we can check it later
191         }
192     }  
193     
194     protected void createEnvContext (WebAppContext wac)
195     throws NamingException
196     {
197         ClassLoader old_loader = Thread.currentThread().getContextClassLoader();
198         Thread.currentThread().setContextClassLoader(wac.getClassLoader());
199         try
200         {
201             Context context = new InitialContext();
202             Context compCtx =  (Context)context.lookup ("java:comp");
203             compCtx.createSubcontext("env");
204         }
205         finally 
206         {
207            Thread.currentThread().setContextClassLoader(old_loader);
208        }
209     }
210 }