View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.jndi;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import javax.naming.Binding;
25  import javax.naming.Context;
26  import javax.naming.Name;
27  import javax.naming.NameNotFoundException;
28  import javax.naming.NameParser;
29  import javax.naming.NamingEnumeration;
30  import javax.naming.NamingException;
31  
32  import org.eclipse.jetty.util.log.Logger;
33  
34  
35  /**
36   * Naming Utility Methods
37   */
38  public class NamingUtil
39  {
40      public final static Logger __log=org.eclipse.jetty.util.log.Log.getLogger("jndi");
41  
42      /* ------------------------------------------------------------ */
43      /**
44       * Bind an object to a context ensuring all sub-contexts
45       * are created if necessary
46       *
47       * @param ctx the context into which to bind
48       * @param nameStr the name relative to context to bind
49       * @param obj the object to be bound
50       * @return the bound context
51       * @exception NamingException if an error occurs
52       */
53      public static Context bind (Context ctx, String nameStr, Object obj)
54          throws NamingException
55      {
56          Name name = ctx.getNameParser("").parse(nameStr);
57  
58          //no name, nothing to do
59          if (name.size() == 0)
60              return null;
61  
62          Context subCtx = ctx;
63  
64          //last component of the name will be the name to bind
65          for (int i=0; i < name.size() - 1; i++)
66          {
67              try
68              {
69                  subCtx = (Context)subCtx.lookup (name.get(i));
70                  if(__log.isDebugEnabled())
71                      __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())
77                      __log.debug("Subcontext "+name.get(i)+" created");
78              }
79          }
80  
81          subCtx.rebind (name.get(name.size() - 1), obj);
82          if(__log.isDebugEnabled())
83              __log.debug("Bound object to "+name.get(name.size() - 1));
84          return subCtx;
85      }
86  
87      public static void unbind (Context ctx)
88      throws NamingException
89      {
90          //unbind everything in the context and all of its subdirectories
91          NamingEnumeration ne = ctx.listBindings(ctx.getNameInNamespace());
92  
93          while (ne.hasMoreElements())
94          {
95              Binding b = (Binding)ne.nextElement();
96              if (b.getObject() instanceof Context)
97              {
98                  unbind((Context)b.getObject());
99              }
100             else
101                 ctx.unbind(b.getName());
102         }
103     }
104 
105     /**
106      * Do a deep listing of the bindings for a context.
107      * @param ctx the context containing the name for which to list the bindings
108      * @param name the name in the context to list
109      * @return map: key is fully qualified name, value is the bound object
110      * @throws NamingException if unable to flatten bindings
111      */
112     public static Map flattenBindings (Context ctx, String name)
113     throws NamingException
114     {
115         HashMap map = new HashMap();
116 
117         //the context representation of name arg
118         Context c = (Context)ctx.lookup (name);
119         NameParser parser = c.getNameParser("");
120         NamingEnumeration enm = ctx.listBindings(name);
121         while (enm.hasMore())
122         {
123             Binding b = (Binding)enm.next();
124 
125             if (b.getObject() instanceof Context)
126             {
127                 map.putAll(flattenBindings (c, b.getName()));
128             }
129             else
130             {
131                 Name compoundName = parser.parse (c.getNameInNamespace());
132                 compoundName.add(b.getName());
133                 map.put (compoundName.toString(), b.getObject());
134             }
135 
136         }
137 
138         return map;
139     }
140 
141 }