1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.plus.jndi;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import javax.naming.Binding;
25 import javax.naming.Context;
26 import javax.naming.InitialContext;
27 import javax.naming.Name;
28 import javax.naming.NameNotFoundException;
29 import javax.naming.NameParser;
30 import javax.naming.NamingEnumeration;
31 import javax.naming.NamingException;
32
33 import org.eclipse.jetty.jndi.NamingUtil;
34 import org.eclipse.jetty.util.log.Logger;
35
36
37 public class NamingEntryUtil
38 {
39 private static Logger __log = NamingUtil.__log;
40
41
42
43
44
45
46
47
48
49
50
51
52 public static boolean bindToENC (Object scope, String asName, String mappedName)
53 throws NamingException
54 {
55 if (asName==null||asName.trim().equals(""))
56 throw new NamingException ("No name for NamingEntry");
57
58 if (mappedName==null || "".equals(mappedName))
59 mappedName=asName;
60
61 NamingEntry entry = lookupNamingEntry (scope, mappedName);
62 if (entry == null)
63 return false;
64
65 entry.bindToENC(asName);
66 return true;
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80
81 public static NamingEntry lookupNamingEntry (Object scope, String jndiName)
82 throws NamingException
83 {
84 NamingEntry entry = null;
85 try
86 {
87 Name scopeName = getNameForScope(scope);
88 InitialContext ic = new InitialContext();
89 NameParser parser = ic.getNameParser("");
90 Name namingEntryName = makeNamingEntryName(parser, jndiName);
91 scopeName.addAll(namingEntryName);
92 entry = (NamingEntry)ic.lookup(scopeName);
93 }
94 catch (NameNotFoundException ee)
95 {
96 }
97
98 return entry;
99 }
100
101 public static Object lookup(Object scope, String jndiName) throws NamingException
102 {
103 Name scopeName = getNameForScope(scope);
104 InitialContext ic = new InitialContext();
105 NameParser parser = ic.getNameParser("");
106 scopeName.addAll(parser.parse(jndiName));
107 return ic.lookup(scopeName);
108 }
109
110
111
112
113
114
115
116
117
118
119 public static List<Object> lookupNamingEntries (Object scope, Class<?> clazz)
120 throws NamingException
121 {
122 try
123 {
124 Context scopeContext = getContextForScope(scope);
125 Context namingEntriesContext = (Context)scopeContext.lookup(NamingEntry.__contextName);
126 ArrayList<Object> list = new ArrayList<Object>();
127 lookupNamingEntries(list, namingEntriesContext, clazz);
128 return list;
129 }
130 catch (NameNotFoundException e)
131 {
132 return Collections.emptyList();
133 }
134 }
135
136
137 public static Name makeNamingEntryName (NameParser parser, NamingEntry namingEntry)
138 throws NamingException
139 {
140 return makeNamingEntryName(parser, (namingEntry==null?null:namingEntry.getJndiName()));
141 }
142
143 public static Name makeNamingEntryName (NameParser parser, String jndiName)
144 throws NamingException
145 {
146 if (jndiName==null)
147 return null;
148
149 if (parser==null)
150 {
151 InitialContext ic = new InitialContext();
152 parser = ic.getNameParser("");
153 }
154
155 Name name = parser.parse("");
156 name.add(NamingEntry.__contextName);
157 name.addAll(parser.parse(jndiName));
158 return name;
159 }
160
161
162 public static Name getNameForScope (Object scope)
163 {
164 try
165 {
166 InitialContext ic = new InitialContext();
167 NameParser parser = ic.getNameParser("");
168 Name name = parser.parse("");
169 if (scope != null)
170 {
171 name.add(canonicalizeScope(scope));
172 }
173 return name;
174 }
175 catch (NamingException e)
176 {
177 __log.warn(e);
178 return null;
179 }
180 }
181
182 public static Context getContextForScope(Object scope)
183 throws NamingException
184 {
185 InitialContext ic = new InitialContext();
186 NameParser parser = ic.getNameParser("");
187 Name name = parser.parse("");
188 if (scope != null)
189 {
190 name.add(canonicalizeScope(scope));
191 }
192 return (Context)ic.lookup(name);
193 }
194
195 public static Context getContextForNamingEntries (Object scope)
196 throws NamingException
197 {
198 Context scopeContext = getContextForScope(scope);
199 return (Context)scopeContext.lookup(NamingEntry.__contextName);
200 }
201
202
203
204
205
206
207
208
209
210
211 private static List<Object> lookupNamingEntries (List<Object> list, Context context, Class<?> clazz)
212 throws NamingException
213 {
214 try
215 {
216 NamingEnumeration<Binding> nenum = context.listBindings("");
217 while (nenum.hasMoreElements())
218 {
219 Binding binding = nenum.next();
220 if (binding.getObject() instanceof Context)
221 lookupNamingEntries (list, (Context)binding.getObject(), clazz);
222 else if (clazz.isInstance(binding.getObject()))
223 list.add(binding.getObject());
224 }
225 }
226 catch (NameNotFoundException e)
227 {
228 __log.debug("No entries of type "+clazz.getName()+" in context="+context);
229 }
230
231 return list;
232 }
233
234 private static String canonicalizeScope(Object scope)
235 {
236 if (scope==null)
237 return "";
238
239 String str = scope.getClass().getName()+"@"+Long.toHexString(scope.hashCode());
240 str=str.replace('/', '_').replace(' ', '_');
241 return str;
242 }
243 }