1   // ========================================================================
2   // $Id: EnvConfiguration.java 2952 2008-06-10 04:00:02Z gregw $
3   // Copyright 1999-2004 Mort Bay Consulting Pty. Ltd.
4   // ------------------------------------------------------------------------
5   // Licensed under the Apache License, Version 2.0 (the "License");
6   // you may not use this file except in compliance with the License.
7   // You may obtain a copy of the License at 
8   // http://www.apache.org/licenses/LICENSE-2.0
9   // Unless required by applicable law or agreed to in writing, software
10  // distributed under the License is distributed on an "AS IS" BASIS,
11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  // See the License for the specific language governing permissions and
13  // limitations under the License.
14  // ========================================================================
15  
16  package org.mortbay.jetty.plus.webapp;
17  
18  import java.net.URL;
19  import java.util.Iterator;
20  import java.util.List;
21  
22  import javax.naming.Context;
23  import javax.naming.InitialContext;
24  import javax.naming.NamingEnumeration;
25  import javax.naming.NamingException;
26  
27  import org.mortbay.jetty.plus.naming.EnvEntry;
28  import org.mortbay.jetty.plus.naming.NamingEntry;
29  import org.mortbay.jetty.plus.naming.NamingEntryUtil;
30  import org.mortbay.jetty.plus.naming.Resource;
31  import org.mortbay.jetty.plus.naming.Transaction;
32  import org.mortbay.jetty.webapp.Configuration;
33  import org.mortbay.jetty.webapp.WebAppContext;
34  import org.mortbay.log.Log;
35  
36  import org.mortbay.xml.XmlConfiguration;
37  
38  /**
39   * EnvConfiguration
40   *
41   *
42   */
43  public class EnvConfiguration implements Configuration
44  {
45      private WebAppContext webAppContext;
46      private Context compCtx;
47      private URL jettyEnvXmlUrl;
48  
49      protected void createEnvContext ()
50      throws NamingException
51      {
52          Context context = new InitialContext();
53          compCtx =  (Context)context.lookup ("java:comp");
54          Context envCtx = compCtx.createSubcontext("env");
55          if (Log.isDebugEnabled())
56              Log.debug("Created java:comp/env for webapp "+getWebAppContext().getContextPath());
57      }
58      
59      
60      /** 
61       * @see org.mortbay.jetty.webapp.Configuration#setWebAppContext(org.mortbay.jetty.webapp.WebAppContext)
62       * @param context
63       */
64      public void setWebAppContext(WebAppContext context)
65      {
66          this.webAppContext = context;
67      }
68  
69      public void setJettyEnvXml (URL url)
70      {
71          this.jettyEnvXmlUrl = url;
72      }
73      
74      /** 
75       * @see org.mortbay.jetty.webapp.Configuration#getWebAppContext()
76       */
77      public WebAppContext getWebAppContext()
78      {
79          return webAppContext;
80      }
81  
82      /** 
83       * @see org.mortbay.jetty.webapp.Configuration#configureClassLoader()
84       * @throws Exception
85       */
86      public void configureClassLoader() throws Exception
87      {
88      }
89  
90      /** 
91       * @see org.mortbay.jetty.webapp.Configuration#configureDefaults()
92       * @throws Exception
93       */
94      public void configureDefaults() throws Exception
95      {
96      }
97  
98      /** 
99       * @see org.mortbay.jetty.webapp.Configuration#configureWebApp()
100      * @throws Exception
101      */
102     public void configureWebApp() throws Exception
103     {
104         //create a java:comp/env
105         createEnvContext();
106         
107         //add java:comp/env entries for any globally defined EnvEntries
108         bindGlobalEnvEntries();
109         
110         //set up java:comp/env as the Context in which to bind directly
111         //the entries in jetty-env.xml
112         NamingEntry.setScope(NamingEntry.SCOPE_WEBAPP);
113         
114         //check to see if an explicit file has been set, if not,
115         //look in WEB-INF/jetty-env.xml
116         if (jettyEnvXmlUrl == null)
117         {
118             
119             //look for a file called WEB-INF/jetty-env.xml
120             //and process it if it exists
121             org.mortbay.resource.Resource web_inf = getWebAppContext().getWebInf();
122             if(web_inf!=null && web_inf.isDirectory())
123             {
124                 org.mortbay.resource.Resource jettyEnv = web_inf.addPath("jetty-env.xml");
125                 if(jettyEnv.exists())
126                 {
127                     jettyEnvXmlUrl = jettyEnv.getURL();
128                 }
129             }
130         }
131         if (jettyEnvXmlUrl != null)
132         {
133             XmlConfiguration configuration = new XmlConfiguration(jettyEnvXmlUrl);
134             configuration.configure(getWebAppContext());
135         }
136 
137     }
138 
139     /** 
140      * Remove all jndi setup
141      * @see org.mortbay.jetty.webapp.Configuration#deconfigureWebApp()
142      * @throws Exception
143      */
144     public void deconfigureWebApp() throws Exception
145     {
146         //get rid of any bindings only defined for the webapp
147         ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
148         Thread.currentThread().setContextClassLoader(webAppContext.getClassLoader());
149         NamingEntry.setScope(NamingEntry.SCOPE_WEBAPP);
150         unbindLocalNamingEntries();
151         compCtx.destroySubcontext("env");
152         NamingEntry.setScope(NamingEntry.SCOPE_CONTAINER);
153         Thread.currentThread().setContextClassLoader(oldLoader);
154     }
155     
156    
157     /**
158      * Bind all EnvEntries that have been globally declared.
159      * @throws NamingException
160      */
161     public void bindGlobalEnvEntries ()
162     throws NamingException
163     {
164         Log.debug("Finding global env entries");
165         
166         List  list = EnvEntry.lookupGlobalEnvEntries();
167         Iterator itor = list.iterator();
168         
169         while (itor.hasNext())
170         {
171             EnvEntry ee = (EnvEntry)itor.next();
172             ee.bindToENC(ee.getJndiName());
173         }
174     }
175     
176   
177     
178 
179     
180     public void unbindLocalNamingEntries ()
181     throws NamingException
182     {
183         //TODO is this necessary??? Why not just cut loose the context at java:comp/env??
184    
185         /*
186         List  list = NamingEntryUtil.lookupNamingEntries(NamingEntry.SCOPE_WEBAPP, EnvEntry.class);
187         list.addAll(NamingEntryUtil.lookupNamingEntries(NamingEntry.SCOPE_WEBAPP, Resource.class));
188         list.addAll(NamingEntryUtil.lookupNamingEntries(NamingEntry.SCOPE_WEBAPP, Transaction.class));
189         
190         Iterator itor = list.iterator();
191         
192         Log.debug("Finding all naming entries for webapp local naming context: size="+list.size());
193         while (itor.hasNext())
194         {
195             NamingEntry ne = (NamingEntry)itor.next();
196             Log.debug("Unbinding naming entry "+ne.getJndiName());
197             ne.unbindENC();
198             ne.release();
199         }
200         */
201         
202     }
203     
204 }