View Javadoc

1   // ========================================================================
2   // $Id: EnvConfiguration.java 3680 2008-09-21 10:37:13Z janb $
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.Name;
25  import javax.naming.NameNotFoundException;
26  import javax.naming.NamingEnumeration;
27  import javax.naming.NamingException;
28  
29  import org.mortbay.jetty.plus.naming.EnvEntry;
30  import org.mortbay.jetty.plus.naming.NamingEntry;
31  import org.mortbay.jetty.plus.naming.NamingEntryUtil;
32  import org.mortbay.jetty.plus.naming.Resource;
33  import org.mortbay.jetty.plus.naming.Transaction;
34  import org.mortbay.jetty.webapp.Configuration;
35  import org.mortbay.jetty.webapp.WebAppContext;
36  import org.mortbay.log.Log;
37  import org.mortbay.naming.NamingUtil;
38  
39  import org.mortbay.xml.XmlConfiguration;
40  
41  /**
42   * EnvConfiguration
43   *
44   *
45   */
46  public class EnvConfiguration implements Configuration
47  {
48      private WebAppContext webAppContext;
49      private Context compCtx;    
50      private Context envCtx;
51      private URL jettyEnvXmlUrl;
52  
53      protected void createEnvContext ()
54      throws NamingException
55      {
56          Context context = new InitialContext();
57          compCtx =  (Context)context.lookup ("java:comp");
58          envCtx = compCtx.createSubcontext("env");
59          if (Log.isDebugEnabled())
60              Log.debug("Created java:comp/env for webapp "+getWebAppContext().getContextPath());
61      }
62      
63      
64      /** 
65       * @see org.mortbay.jetty.webapp.Configuration#setWebAppContext(org.mortbay.jetty.webapp.WebAppContext)
66       * @param context
67       */
68      public void setWebAppContext(WebAppContext context)
69      {
70          this.webAppContext = context;
71      }
72  
73      public void setJettyEnvXml (URL url)
74      {
75          this.jettyEnvXmlUrl = url;
76      }
77      
78      /** 
79       * @see org.mortbay.jetty.webapp.Configuration#getWebAppContext()
80       */
81      public WebAppContext getWebAppContext()
82      {
83          return webAppContext;
84      }
85  
86      /** 
87       * @see org.mortbay.jetty.webapp.Configuration#configureClassLoader()
88       * @throws Exception
89       */
90      public void configureClassLoader() throws Exception
91      {
92      }
93  
94      /** 
95       * @see org.mortbay.jetty.webapp.Configuration#configureDefaults()
96       * @throws Exception
97       */
98      public void configureDefaults() throws Exception
99      {        
100         //create a java:comp/env
101         createEnvContext();
102     }
103 
104     /** 
105      * @see org.mortbay.jetty.webapp.Configuration#configureWebApp()
106      * @throws Exception
107      */
108     public void configureWebApp() throws Exception
109     {
110         //check to see if an explicit file has been set, if not,
111         //look in WEB-INF/jetty-env.xml
112         if (jettyEnvXmlUrl == null)
113         {
114             
115             //look for a file called WEB-INF/jetty-env.xml
116             //and process it if it exists
117             org.mortbay.resource.Resource web_inf = getWebAppContext().getWebInf();
118             if(web_inf!=null && web_inf.isDirectory())
119             {
120                 org.mortbay.resource.Resource jettyEnv = web_inf.addPath("jetty-env.xml");
121                 if(jettyEnv.exists())
122                 {
123                     jettyEnvXmlUrl = jettyEnv.getURL();
124                 }
125             }
126         }
127         if (jettyEnvXmlUrl != null)
128         {
129             XmlConfiguration configuration = new XmlConfiguration(jettyEnvXmlUrl);
130             configuration.configure(getWebAppContext());
131         }
132         
133         //add java:comp/env entries for any EnvEntries that have been defined so far
134         bindEnvEntries();
135     }
136 
137     /** 
138      * Remove all jndi setup
139      * @see org.mortbay.jetty.webapp.Configuration#deconfigureWebApp()
140      * @throws Exception
141      */
142     public void deconfigureWebApp() throws Exception
143     {
144         //get rid of any bindings for comp/env for webapp
145         ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
146         Thread.currentThread().setContextClassLoader(webAppContext.getClassLoader());
147         compCtx.destroySubcontext("env");
148         
149         //unbind any NamingEntries that were configured in this webapp's name space
150         try
151         {
152             Context scopeContext = NamingEntryUtil.getContextForScope(getWebAppContext());
153             scopeContext.destroySubcontext(NamingEntry.__contextName);
154         }
155         catch (NameNotFoundException e)
156         {
157             Log.ignore(e);
158             Log.debug("No naming entries configured in environment for webapp "+getWebAppContext());
159         }
160         Thread.currentThread().setContextClassLoader(oldLoader);
161     }
162     
163     /**
164      * Bind all EnvEntries that have been declared, so that the processing of the
165      * web.xml file can potentially override them.
166      * 
167      * We first bind EnvEntries declared in Server scope, then WebAppContext scope.
168      * @throws NamingException
169      */
170     public void bindEnvEntries ()
171     throws NamingException
172     {
173         Log.debug("Binding env entries from the jvm scope");
174         Object scope = null;
175         List list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
176         Iterator itor = list.iterator();
177         while (itor.hasNext())
178         {
179             EnvEntry ee = (EnvEntry)itor.next();
180             ee.bindToENC(ee.getJndiName());
181             Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
182             NamingUtil.bind(envCtx, namingEntryName.toString(), ee);//also save the EnvEntry in the context so we can check it later          
183         }
184         
185         Log.debug("Binding env entries from the server scope");
186         
187         scope = getWebAppContext().getServer();
188         list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
189         itor = list.iterator();
190         while (itor.hasNext())
191         {
192             EnvEntry ee = (EnvEntry)itor.next();
193             ee.bindToENC(ee.getJndiName());
194             Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
195             NamingUtil.bind(envCtx, namingEntryName.toString(), ee);//also save the EnvEntry in the context so we can check it later          
196         }
197         
198         Log.debug("Binding env entries from the context scope");
199         scope = getWebAppContext();
200         list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
201         itor = list.iterator();
202         while (itor.hasNext())
203         {
204             EnvEntry ee = (EnvEntry)itor.next();
205             ee.bindToENC(ee.getJndiName());
206             Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
207             NamingUtil.bind(envCtx, namingEntryName.toString(), ee);//also save the EnvEntry in the context so we can check it later
208         }
209     }  
210 }