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.util.log.Log;
32
33
34 public class NamingEntryUtil
35 {
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public static boolean bindToENC (Object scope, String asName, String mappedName)
50 throws NamingException
51 {
52 if (asName==null||asName.trim().equals(""))
53 throw new NamingException ("No name for NamingEntry");
54
55 if (mappedName==null || "".equals(mappedName))
56 mappedName=asName;
57
58 NamingEntry entry = lookupNamingEntry (scope, mappedName);
59 if (entry == null)
60 return false;
61
62 entry.bindToENC(asName);
63 return true;
64 }
65
66
67
68
69
70
71
72
73
74
75
76
77
78 public static NamingEntry lookupNamingEntry (Object scope, String jndiName)
79 throws NamingException
80 {
81 NamingEntry entry = null;
82 try
83 {
84 Name scopeName = getNameForScope(scope);
85 InitialContext ic = new InitialContext();
86 NameParser parser = ic.getNameParser("");
87 Name namingEntryName = makeNamingEntryName(parser, jndiName);
88 scopeName.addAll(namingEntryName);
89 entry = (NamingEntry)ic.lookup(scopeName);
90 }
91 catch (NameNotFoundException ee)
92 {
93 }
94
95 return entry;
96 }
97
98
99 public static Object lookup (Object scope, String jndiName)
100 throws NamingException
101 {
102 Object o = null;
103
104 Name scopeName = getNameForScope(scope);
105 InitialContext ic = new InitialContext();
106 NameParser parser = ic.getNameParser("");
107 scopeName.addAll(parser.parse(jndiName));
108 return ic.lookup(scopeName);
109 }
110
111
112
113
114
115
116
117
118
119
120
121 public static List lookupNamingEntries (Object scope, Class clazz)
122 throws NamingException
123 {
124 try
125 {
126 Context scopeContext = getContextForScope(scope);
127 Context namingEntriesContext = (Context)scopeContext.lookup(NamingEntry.__contextName);
128 ArrayList list = new ArrayList();
129 lookupNamingEntries(list, namingEntriesContext, clazz);
130 return list;
131 }
132 catch (NameNotFoundException e)
133 {
134 return Collections.EMPTY_LIST;
135 }
136 }
137
138
139 public static Name makeNamingEntryName (NameParser parser, NamingEntry namingEntry)
140 throws NamingException
141 {
142 return makeNamingEntryName(parser, (namingEntry==null?null:namingEntry.getJndiName()));
143 }
144
145 public static Name makeNamingEntryName (NameParser parser, String jndiName)
146 throws NamingException
147 {
148 if (jndiName==null)
149 return null;
150
151 if (parser==null)
152 {
153 InitialContext ic = new InitialContext();
154 parser = ic.getNameParser("");
155 }
156
157 Name name = parser.parse("");
158 name.add(NamingEntry.__contextName);
159 name.addAll(parser.parse(jndiName));
160 return name;
161 }
162
163
164 public static Name getNameForScope (Object scope)
165 {
166 try
167 {
168 InitialContext ic = new InitialContext();
169 NameParser parser = ic.getNameParser("");
170 Name name = parser.parse("");
171 if (scope != null)
172 {
173 name.add(canonicalizeScope(scope));
174 }
175 return name;
176 }
177 catch (NamingException e)
178 {
179 Log.warn(e);
180 return null;
181 }
182 }
183
184 public static Context getContextForScope(Object scope)
185 throws NamingException
186 {
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()+"@"+scope.hashCode();
243 str=str.replace('/', '_').replace(' ', '_');
244 return str;
245 }
246 }