1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
37
38 public class NamingUtil
39 {
40 public final static Logger __log=org.eclipse.jetty.util.log.Log.getLogger("jndi");
41
42
43
44
45
46
47
48
49
50
51
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
59 if (name.size() == 0)
60 return null;
61
62 Context subCtx = ctx;
63
64
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
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
107
108
109
110
111
112 public static Map flattenBindings (Context ctx, String name)
113 throws NamingException
114 {
115 HashMap map = new HashMap();
116
117
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 }