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 public static Object lookup(Object scope, String jndiName) throws NamingException
100 {
101 Name scopeName = getNameForScope(scope);
102 InitialContext ic = new InitialContext();
103 NameParser parser = ic.getNameParser("");
104 scopeName.addAll(parser.parse(jndiName));
105 return ic.lookup(scopeName);
106 }
107
108
109
110
111
112
113
114
115
116
117 public static List<Object> lookupNamingEntries (Object scope, Class<?> clazz)
118 throws NamingException
119 {
120 try
121 {
122 Context scopeContext = getContextForScope(scope);
123 Context namingEntriesContext = (Context)scopeContext.lookup(NamingEntry.__contextName);
124 ArrayList<Object> list = new ArrayList<Object>();
125 lookupNamingEntries(list, namingEntriesContext, clazz);
126 return list;
127 }
128 catch (NameNotFoundException e)
129 {
130 return Collections.emptyList();
131 }
132 }
133
134
135 public static Name makeNamingEntryName (NameParser parser, NamingEntry namingEntry)
136 throws NamingException
137 {
138 return makeNamingEntryName(parser, (namingEntry==null?null:namingEntry.getJndiName()));
139 }
140
141 public static Name makeNamingEntryName (NameParser parser, String jndiName)
142 throws NamingException
143 {
144 if (jndiName==null)
145 return null;
146
147 if (parser==null)
148 {
149 InitialContext ic = new InitialContext();
150 parser = ic.getNameParser("");
151 }
152
153 Name name = parser.parse("");
154 name.add(NamingEntry.__contextName);
155 name.addAll(parser.parse(jndiName));
156 return name;
157 }
158
159
160 public static Name getNameForScope (Object scope)
161 {
162 try
163 {
164 InitialContext ic = new InitialContext();
165 NameParser parser = ic.getNameParser("");
166 Name name = parser.parse("");
167 if (scope != null)
168 {
169 name.add(canonicalizeScope(scope));
170 }
171 return name;
172 }
173 catch (NamingException e)
174 {
175 __log.warn(e);
176 return null;
177 }
178 }
179
180 public static Context getContextForScope(Object scope)
181 throws NamingException
182 {
183 InitialContext ic = new InitialContext();
184 NameParser parser = ic.getNameParser("");
185 Name name = parser.parse("");
186 if (scope != null)
187 {
188 name.add(canonicalizeScope(scope));
189 }
190 return (Context)ic.lookup(name);
191 }
192
193 public static Context getContextForNamingEntries (Object scope)
194 throws NamingException
195 {
196 Context scopeContext = getContextForScope(scope);
197 return (Context)scopeContext.lookup(NamingEntry.__contextName);
198 }
199
200
201
202
203
204
205
206
207
208
209 private static List<Object> lookupNamingEntries (List<Object> list, Context context, Class<?> clazz)
210 throws NamingException
211 {
212 try
213 {
214 NamingEnumeration<Binding> nenum = context.listBindings("");
215 while (nenum.hasMoreElements())
216 {
217 Binding binding = nenum.next();
218 if (binding.getObject() instanceof Context)
219 lookupNamingEntries (list, (Context)binding.getObject(), clazz);
220 else if (clazz.isInstance(binding.getObject()))
221 list.add(binding.getObject());
222 }
223 }
224 catch (NameNotFoundException e)
225 {
226 __log.debug("No entries of type "+clazz.getName()+" in context="+context);
227 }
228
229 return list;
230 }
231
232 private static String canonicalizeScope(Object scope)
233 {
234 if (scope==null)
235 return "";
236
237 String str = scope.getClass().getName()+"@"+Long.toHexString(scope.hashCode());
238 str=str.replace('/', '_').replace(' ', '_');
239 return str;
240 }
241 }