1   // ========================================================================
2   // $Id: EnvEntry.java 2952 2008-06-10 04:00:02Z gregw $
3   // Copyright 2006 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.naming;
17  
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import javax.naming.Binding;
23  import javax.naming.Context;
24  import javax.naming.InitialContext;
25  import javax.naming.NameNotFoundException;
26  import javax.naming.NamingEnumeration;
27  import javax.naming.NamingException;
28  
29  import org.mortbay.log.Log;
30  import org.mortbay.naming.NamingUtil;
31  
32  
33  /**
34   * EnvEntry
35   *
36   *
37   */
38  public class EnvEntry extends NamingEntry
39  {
40      private boolean overrideWebXml;
41      
42      
43      /**
44       * Bind a name and value to java:comp/env, taking into account
45       * any overriding EnvEntrys in the environment, either local or global.
46       * 
47       * @param name the name from web.xml env-entry
48       * @param value the value from web.xml env-entry
49       * @throws NamingException
50       */
51      public static void bindToENC (String name, Object value)
52      throws NamingException
53      {       
54          if (name==null||name.trim().equals(""))
55              throw new NamingException("No name for EnvEntry");
56  
57          //Is there an EnvEntry with the same name? If so, check its overrideWebXml setting. If true,
58          //it's value should be used instead of the one supplied in web.xml - as the name matches, then in
59          //fact the value is already bound, so there's nothing to do . If false, use the value
60          //from web.xml. In the case where the EnvEntry has been scoped only to the webapp (ie it was
61          //in jetty-env.xml).
62          EnvEntry envEntry = (EnvEntry)NamingEntryUtil.lookupNamingEntry(name);
63          if (envEntry!=null && envEntry.isOverrideWebXml())
64              envEntry.bindToENC(name);
65          else
66          {
67              //No EnvEntry, or it wasnt set to override, so just bind the value from web.xml
68              InitialContext ic = new InitialContext();
69              Context envCtx = (Context)ic.lookup("java:comp/env");
70              NamingUtil.bind(envCtx, name, value);
71          }     
72      }
73      
74      public static List lookupGlobalEnvEntries ()
75      throws NamingException
76      {
77          ArrayList list = new ArrayList();
78          lookupEnvEntries(list, new InitialContext());
79          return list;
80      }
81      
82      private static List lookupEnvEntries (List list, Context context)
83      throws NamingException
84      {
85          try
86          {
87              NamingEnumeration nenum = context.listBindings("");
88              while (nenum.hasMoreElements())
89              {
90                  Binding binding = (Binding)nenum.next();
91                  if (binding.getObject() instanceof Context)
92                      lookupEnvEntries (list, (Context)binding.getObject());
93                  else if (EnvEntry.class.isInstance(binding.getObject()))
94                    list.add(binding.getObject());
95              }
96          }
97          catch (NameNotFoundException e)
98          {
99              Log.debug("No EnvEntries in context="+context);
100         }
101 
102         return list;
103     }
104 
105     
106     
107     public EnvEntry (String jndiName, Object objToBind)
108     throws NamingException
109     {
110         this(jndiName, objToBind, false);
111     }
112     
113     public EnvEntry (String jndiName, Object objToBind, boolean overrideWebXml)
114     throws NamingException
115     {
116         super(jndiName, objToBind);
117         this.overrideWebXml = overrideWebXml;
118     }
119     
120     
121     public boolean isOverrideWebXml ()
122     {
123         return this.overrideWebXml;
124     }
125 }