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