1   // ========================================================================
2   // $Id: NamingUtil.java 2800 2008-04-23 11:06:22Z janb $
3   // Copyright 1999-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.naming;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import javax.naming.Binding;
22  import javax.naming.Context;
23  import javax.naming.Name;
24  import javax.naming.NameNotFoundException;
25  import javax.naming.NameParser;
26  import javax.naming.NamingEnumeration;
27  import javax.naming.NamingException;
28  
29  import org.mortbay.log.Log;
30  
31  
32  /**
33   * Util.java
34   *
35   *
36   * Created: Tue Jul  1 18:26:17 2003
37   *
38   * @author <a href="mailto:janb@mortbay.com">Jan Bartel</a>
39   * @version 1.0
40   */
41  public class NamingUtil 
42  {
43  
44      /* ------------------------------------------------------------ */
45      /**
46       * Bind an object to a context ensuring all subcontexts 
47       * are created if necessary
48       *
49       * @param ctx the context into which to bind
50       * @param name the name relative to context to bind
51       * @param obj the object to be bound
52       * @exception NamingException if an error occurs
53       */
54      public static void bind (Context ctx, String nameStr, Object obj)
55          throws NamingException
56      {
57          Name name = ctx.getNameParser("").parse(nameStr);
58  
59          //no name, nothing to do 
60          if (name.size() == 0)
61              return;
62  
63          Context subCtx = ctx;
64          
65          //last component of the name will be the name to bind
66          for (int i=0; i < name.size() - 1; i++)
67          {
68              try
69              {
70                  subCtx = (Context)subCtx.lookup (name.get(i));
71                  if(Log.isDebugEnabled())Log.debug("Subcontext "+name.get(i)+" already exists");
72              }
73              catch (NameNotFoundException e)
74              {
75                  subCtx = subCtx.createSubcontext(name.get(i));
76                  if(Log.isDebugEnabled())Log.debug("Subcontext "+name.get(i)+" created");
77              }
78          }
79  
80          subCtx.rebind (name.get(name.size() - 1), obj);
81          if(Log.isDebugEnabled())Log.debug("Bound object to "+name.get(name.size() - 1));
82      } 
83      
84      
85      public static void unbind (Context ctx)
86      throws NamingException
87      {
88          //unbind everything in the context and all of its subdirectories
89          NamingEnumeration ne = ctx.listBindings(ctx.getNameInNamespace());
90          
91          while (ne.hasMoreElements())
92          {
93              Binding b = (Binding)ne.nextElement();
94              if (b.getObject() instanceof Context)
95              {
96                  unbind((Context)b.getObject());
97              }
98              else
99                  ctx.unbind(b.getName());
100         }
101     }
102     
103     /**
104      * Do a deep listing of the bindings for a context.
105      * @param ctx the context containing the name for which to list the bindings
106      * @param name the name in the context to list
107      * @return map: key is fully qualified name, value is the bound object 
108      * @throws NamingException
109      */
110     public static Map flattenBindings (Context ctx, String name)
111     throws NamingException
112     {
113         HashMap map = new HashMap();
114 
115         //the context representation of name arg
116         Context c = (Context)ctx.lookup (name);
117         NameParser parser = c.getNameParser("");
118         NamingEnumeration enm = ctx.listBindings(name);
119         while (enm.hasMore())
120         {
121             Binding b = (Binding)enm.next();
122 
123             if (b.getObject() instanceof Context)
124             {
125                 map.putAll(flattenBindings (c, b.getName()));
126             }
127             else
128             {
129                 Name compoundName = parser.parse (c.getNameInNamespace());
130                 compoundName.add(b.getName());
131                 map.put (compoundName.toString(), b.getObject());
132             }
133             
134         }
135         
136         return map;
137     }
138     
139 }