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