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