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.jndi;
15
16 import java.util.HashMap;
17 import java.util.Map;
18
19 import javax.naming.Binding;
20 import javax.naming.Context;
21 import javax.naming.Name;
22 import javax.naming.NameNotFoundException;
23 import javax.naming.NameParser;
24 import javax.naming.NamingEnumeration;
25 import javax.naming.NamingException;
26
27 import org.eclipse.jetty.util.log.Logger;
28
29
30 /**
31 * Util.java
32 *
33 *
34 * Created: Tue Jul 1 18:26:17 2003
35 *
36 *
37 * @version 1.0
38 */
39 public class NamingUtil
40 {
41 public final static Logger __log=org.eclipse.jetty.util.log.Log.getLogger("jndi");
42
43 /* ------------------------------------------------------------ */
44 /**
45 * Bind an object to a context ensuring all sub-contexts
46 * are created if necessary
47 *
48 * @param ctx the context into which to bind
49 * @param nameStr the name relative to context to bind
50 * @param obj the object to be bound
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
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 }