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