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
25 import javax.naming.Binding;
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.Name;
29 import javax.naming.NameNotFoundException;
30 import javax.naming.NameParser;
31 import javax.naming.NamingEnumeration;
32 import javax.naming.NamingException;
33
34 import org.eclipse.jetty.jndi.NamingUtil;
35 import org.eclipse.jetty.util.log.Logger;
36
37
38 public class NamingEntryUtil
39 {
40 private static Logger __log = NamingUtil.__log;
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public static boolean bindToENC (Object scope, String asName, String mappedName)
55 throws NamingException
56 {
57 if (asName==null||asName.trim().equals(""))
58 throw new NamingException ("No name for NamingEntry");
59
60 if (mappedName==null || "".equals(mappedName))
61 mappedName=asName;
62
63 NamingEntry entry = lookupNamingEntry (scope, mappedName);
64 if (entry == null)
65 return false;
66
67 entry.bindToENC(asName);
68 return true;
69 }
70
71
72
73
74
75
76
77
78
79
80
81
82
83 public static NamingEntry lookupNamingEntry (Object scope, String jndiName)
84 throws NamingException
85 {
86 NamingEntry entry = null;
87 try
88 {
89 Name scopeName = getNameForScope(scope);
90 InitialContext ic = new InitialContext();
91 NameParser parser = ic.getNameParser("");
92 Name namingEntryName = makeNamingEntryName(parser, jndiName);
93 scopeName.addAll(namingEntryName);
94 entry = (NamingEntry)ic.lookup(scopeName);
95 }
96 catch (NameNotFoundException ee)
97 {
98 }
99
100 return entry;
101 }
102
103 public static Object lookup(Object scope, String jndiName) throws NamingException
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 public static List<Object> 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<Object> list = new ArrayList<Object>();
129 lookupNamingEntries(list, namingEntriesContext, clazz);
130 return list;
131 }
132 catch (NameNotFoundException e)
133 {
134 return Collections.emptyList();
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 InitialContext ic = new InitialContext();
188 NameParser parser = ic.getNameParser("");
189 Name name = parser.parse("");
190 if (scope != null)
191 {
192 name.add(canonicalizeScope(scope));
193 }
194 return (Context)ic.lookup(name);
195 }
196
197 public static Context getContextForNamingEntries (Object scope)
198 throws NamingException
199 {
200 Context scopeContext = getContextForScope(scope);
201 return (Context)scopeContext.lookup(NamingEntry.__contextName);
202 }
203
204
205
206
207
208
209
210
211
212
213 private static List<Object> lookupNamingEntries (List<Object> list, Context context, Class<?> clazz)
214 throws NamingException
215 {
216 try
217 {
218 NamingEnumeration<Binding> nenum = context.listBindings("");
219 while (nenum.hasMoreElements())
220 {
221 Binding binding = nenum.next();
222 if (binding.getObject() instanceof Context)
223 lookupNamingEntries (list, (Context)binding.getObject(), clazz);
224 else if (clazz.isInstance(binding.getObject()))
225 list.add(binding.getObject());
226 }
227 }
228 catch (NameNotFoundException e)
229 {
230 __log.debug("No entries of type "+clazz.getName()+" in context="+context);
231 }
232
233 return list;
234 }
235
236 private static String canonicalizeScope(Object scope)
237 {
238 if (scope==null)
239 return "";
240
241 String str = scope.getClass().getName()+"@"+Long.toHexString(scope.hashCode());
242 str=str.replace('/', '_').replace(' ', '_');
243 return str;
244 }
245 }