View Javadoc

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