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.Log;
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
42 /* ------------------------------------------------------------ */
43 /**
44 * Bind an object to a context ensuring all subcontexts
45 * are created if necessary
46 *
47 * @param ctx the context into which to bind
48 * @param name the name relative to context to bind
49 * @param obj the object to be bound
50 * @exception NamingException if an error occurs
51 */
52 public static Context bind (Context ctx, String nameStr, Object obj)
53 throws NamingException
54 {
55 Name name = ctx.getNameParser("").parse(nameStr);
56
57 //no name, nothing to do
58 if (name.size() == 0)
59 return null;
60
61 Context subCtx = ctx;
62
63 //last component of the name will be the name to bind
64 for (int i=0; i < name.size() - 1; i++)
65 {
66 try
67 {
68 subCtx = (Context)subCtx.lookup (name.get(i));
69 if(Log.isDebugEnabled())Log.debug("Subcontext "+name.get(i)+" already exists");
70 }
71 catch (NameNotFoundException e)
72 {
73 subCtx = subCtx.createSubcontext(name.get(i));
74 if(Log.isDebugEnabled())Log.debug("Subcontext "+name.get(i)+" created");
75 }
76 }
77
78 subCtx.rebind (name.get(name.size() - 1), obj);
79 if(Log.isDebugEnabled())Log.debug("Bound object to "+name.get(name.size() - 1));
80 return subCtx;
81
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 }