KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > mock > jndi > SimpleNamingContext


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.mock.jndi;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Hashtable JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.naming.Binding JavaDoc;
25 import javax.naming.Context JavaDoc;
26 import javax.naming.Name JavaDoc;
27 import javax.naming.NameClassPair JavaDoc;
28 import javax.naming.NameNotFoundException JavaDoc;
29 import javax.naming.NameParser JavaDoc;
30 import javax.naming.NamingEnumeration JavaDoc;
31 import javax.naming.NamingException JavaDoc;
32 import javax.naming.OperationNotSupportedException JavaDoc;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36
37 import org.springframework.util.StringUtils;
38
39 /**
40  * Simple implementation of a JNDI naming context.
41  * Only supports binding plain Objects to String names.
42  * Mainly for test environments, but also usable for standalone applications.
43  *
44  * <p>This class is not intended for direct usage by applications, although it
45  * can be used for example to override JndiTemplate's <code>createInitialContext</code>
46  * method in unit tests. Typically, SimpleNamingContextBuilder will be used to
47  * set up a JVM-level JNDI environment.
48  *
49  * @author Rod Johnson
50  * @author Juergen Hoeller
51  * @see org.springframework.mock.jndi.SimpleNamingContextBuilder
52  * @see org.springframework.jndi.JndiTemplate#createInitialContext
53  */

54 public class SimpleNamingContext implements Context JavaDoc {
55
56     private final Log logger = LogFactory.getLog(getClass());
57
58     private final String JavaDoc root;
59
60     private final Hashtable JavaDoc boundObjects;
61
62     private final Hashtable JavaDoc environment = new Hashtable JavaDoc();
63
64
65     /**
66      * Create a new naming context.
67      */

68     public SimpleNamingContext() {
69         this("");
70     }
71
72     /**
73      * Create a new naming context with the given naming root.
74      */

75     public SimpleNamingContext(String JavaDoc root) {
76         this.root = root;
77         this.boundObjects = new Hashtable JavaDoc();
78     }
79
80     /**
81      * Create a new naming context with the given naming root,
82      * the given name/object map, and the JNDI environment entries.
83      */

84     public SimpleNamingContext(String JavaDoc root, Hashtable JavaDoc boundObjects, Hashtable JavaDoc environment) {
85         this.root = root;
86         this.boundObjects = boundObjects;
87         if (environment != null) {
88             this.environment.putAll(environment);
89         }
90     }
91
92
93     // Actual implementations of Context methods follow
94

95     public NamingEnumeration JavaDoc list(String JavaDoc root) throws NamingException JavaDoc {
96         if (logger.isDebugEnabled()) {
97             logger.debug("Listing name/class pairs under [" + root + "]");
98         }
99         return new NameClassPairEnumeration(this, root);
100     }
101
102     public NamingEnumeration JavaDoc listBindings(String JavaDoc root) throws NamingException JavaDoc {
103         if (logger.isDebugEnabled()) {
104             logger.debug("Listing bindings under [" + root + "]");
105         }
106         return new BindingEnumeration(this, root);
107     }
108
109     /**
110      * Look up the object with the given name.
111      * Note: Not intended for direct use by applications.
112      * Will be used by any standard InitialContext JNDI lookups.
113      * @throws javax.naming.NameNotFoundException if the object could not be found
114      */

115     public Object JavaDoc lookup(String JavaDoc pname) throws NameNotFoundException JavaDoc {
116         String JavaDoc name = this.root + pname;
117         if (logger.isDebugEnabled()) {
118             logger.debug("Static JNDI lookup: [" + name + "]");
119         }
120         if ("".equals(name)) {
121             return new SimpleNamingContext(this.root, this.boundObjects, this.environment);
122         }
123         Object JavaDoc found = this.boundObjects.get(name);
124         if (found == null) {
125             if (!name.endsWith("/")) {
126                 name = name + "/";
127             }
128             for (Iterator JavaDoc it = this.boundObjects.keySet().iterator(); it.hasNext();) {
129                 String JavaDoc boundName = (String JavaDoc) it.next();
130                 if (boundName.startsWith(name)) {
131                     return new SimpleNamingContext(name, this.boundObjects, this.environment);
132                 }
133             }
134             throw new NameNotFoundException JavaDoc(
135                     "Name [" + this.root + pname + "] not bound; " + this.boundObjects.size() + " bindings: [" +
136                     StringUtils.collectionToDelimitedString(this.boundObjects.keySet(), ",") + "]");
137         }
138         return found;
139     }
140
141     public Object JavaDoc lookupLink(String JavaDoc name) throws NameNotFoundException JavaDoc {
142         return lookup(name);
143     }
144
145     /**
146      * Bind the given object to the given name.
147      * Note: Not intended for direct use by applications
148      * if setting up a JVM-level JNDI environment.
149      * Use SimpleNamingContextBuilder to set up JNDI bindings then.
150      * @see org.springframework.mock.jndi.SimpleNamingContextBuilder#bind
151      */

152     public void bind(String JavaDoc name, Object JavaDoc obj) {
153         if (logger.isInfoEnabled()) {
154             logger.info("Static JNDI binding: [" + this.root + name + "] = [" + obj + "]");
155         }
156         this.boundObjects.put(this.root + name, obj);
157     }
158
159     public void unbind(String JavaDoc name) {
160         if (logger.isInfoEnabled()) {
161             logger.info("Static JNDI remove: [" + this.root + name + "]");
162         }
163         this.boundObjects.remove(this.root + name);
164     }
165
166     public void rebind(String JavaDoc name, Object JavaDoc obj) {
167         bind(name, obj);
168     }
169
170     public void rename(String JavaDoc oldName, String JavaDoc newName) throws NameNotFoundException JavaDoc {
171         Object JavaDoc obj = lookup(oldName);
172         unbind(oldName);
173         bind(newName, obj);
174     }
175
176     public Context JavaDoc createSubcontext(String JavaDoc name) {
177         Context JavaDoc subcontext = new SimpleNamingContext(this.root + name, this.boundObjects, this.environment);
178         bind(name, subcontext);
179         return subcontext;
180     }
181
182     public void destroySubcontext(String JavaDoc name) {
183         unbind(name);
184     }
185
186     public String JavaDoc composeName(String JavaDoc name, String JavaDoc prefix) {
187         return prefix + name;
188     }
189
190     public Hashtable JavaDoc getEnvironment() {
191         return environment;
192     }
193
194     public Object JavaDoc addToEnvironment(String JavaDoc propName, Object JavaDoc propVal) {
195         return environment.put(propName, propVal);
196     }
197
198     public Object JavaDoc removeFromEnvironment(String JavaDoc propName) {
199         return environment.remove(propName);
200     }
201
202     public void close() {
203     }
204
205
206     // Unsupported methods follow: no support for javax.naming.Name
207

208     public NamingEnumeration JavaDoc list(Name JavaDoc name) throws NamingException JavaDoc {
209         throw new OperationNotSupportedException JavaDoc("SimpleNamingContext does not support [javax.naming.Name]");
210     }
211
212     public NamingEnumeration JavaDoc listBindings(Name JavaDoc name) throws NamingException JavaDoc {
213         throw new OperationNotSupportedException JavaDoc("SimpleNamingContext does not support [javax.naming.Name]");
214     }
215
216     public Object JavaDoc lookup(Name JavaDoc name) throws NamingException JavaDoc {
217         throw new OperationNotSupportedException JavaDoc("SimpleNamingContext does not support [javax.naming.Name]");
218     }
219
220     public Object JavaDoc lookupLink(Name JavaDoc name) throws NamingException JavaDoc {
221         throw new OperationNotSupportedException JavaDoc("SimpleNamingContext does not support [javax.naming.Name]");
222     }
223
224     public void bind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
225         throw new OperationNotSupportedException JavaDoc("SimpleNamingContext does not support [javax.naming.Name]");
226     }
227
228     public void unbind(Name JavaDoc name) throws NamingException JavaDoc {
229         throw new OperationNotSupportedException JavaDoc("SimpleNamingContext does not support [javax.naming.Name]");
230     }
231
232     public void rebind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
233         throw new OperationNotSupportedException JavaDoc("SimpleNamingContext does not support [javax.naming.Name]");
234     }
235
236     public void rename(Name JavaDoc oldName, Name JavaDoc newName) throws NamingException JavaDoc {
237         throw new OperationNotSupportedException JavaDoc("SimpleNamingContext does not support [javax.naming.Name]");
238     }
239
240     public Context JavaDoc createSubcontext(Name JavaDoc name) throws NamingException JavaDoc {
241         throw new OperationNotSupportedException JavaDoc("SimpleNamingContext does not support [javax.naming.Name]");
242     }
243
244     public void destroySubcontext(Name JavaDoc name) throws NamingException JavaDoc {
245         throw new OperationNotSupportedException JavaDoc("SimpleNamingContext does not support [javax.naming.Name]");
246     }
247
248     public String JavaDoc getNameInNamespace() throws NamingException JavaDoc {
249         throw new OperationNotSupportedException JavaDoc("SimpleNamingContext does not support [javax.naming.Name]");
250     }
251
252     public NameParser JavaDoc getNameParser(Name JavaDoc name) throws NamingException JavaDoc {
253         throw new OperationNotSupportedException JavaDoc("SimpleNamingContext does not support [javax.naming.Name]");
254     }
255
256     public NameParser JavaDoc getNameParser(String JavaDoc name) throws NamingException JavaDoc {
257         throw new OperationNotSupportedException JavaDoc("SimpleNamingContext does not support [javax.naming.Name]");
258     }
259
260     public Name JavaDoc composeName(Name JavaDoc name, Name JavaDoc prefix) throws NamingException JavaDoc {
261         throw new OperationNotSupportedException JavaDoc("SimpleNamingContext does not support [javax.naming.Name]");
262     }
263
264
265     private static abstract class AbstractNamingEnumeration implements NamingEnumeration JavaDoc {
266
267         private Iterator JavaDoc iterator;
268
269         private AbstractNamingEnumeration(SimpleNamingContext context, String JavaDoc proot) throws NamingException JavaDoc {
270             if (!"".equals(proot) && !proot.endsWith("/")) {
271                 proot = proot + "/";
272             }
273             String JavaDoc root = context.root + proot;
274             Map JavaDoc contents = new HashMap JavaDoc();
275             Iterator JavaDoc it = context.boundObjects.keySet().iterator();
276             while (it.hasNext()) {
277                 String JavaDoc boundName = (String JavaDoc) it.next();
278                 if (boundName.startsWith(root)) {
279                     int startIndex = root.length();
280                     int endIndex = boundName.indexOf('/', startIndex);
281                     String JavaDoc strippedName =
282                             (endIndex != -1 ? boundName.substring(startIndex, endIndex) : boundName.substring(startIndex));
283                     if (!contents.containsKey(strippedName)) {
284                         try {
285                             contents.put(strippedName, createObject(strippedName, context.lookup(proot + strippedName)));
286                         }
287                         catch (NameNotFoundException JavaDoc ex) {
288                             // cannot happen
289
}
290                     }
291                 }
292             }
293             if (contents.size() == 0) {
294                 throw new NamingException JavaDoc("Invalid root: [" + context.root + proot + "]");
295             }
296             this.iterator = contents.values().iterator();
297         }
298
299         protected abstract Object JavaDoc createObject(String JavaDoc strippedName, Object JavaDoc obj);
300
301         public boolean hasMore() {
302             return iterator.hasNext();
303         }
304
305         public Object JavaDoc next() {
306             return iterator.next();
307         }
308
309         public boolean hasMoreElements() {
310             return iterator.hasNext();
311         }
312
313         public Object JavaDoc nextElement() {
314             return iterator.next();
315         }
316
317         public void close() {
318         }
319     }
320
321
322     private static class NameClassPairEnumeration extends AbstractNamingEnumeration {
323
324         private NameClassPairEnumeration(SimpleNamingContext context, String JavaDoc root) throws NamingException JavaDoc {
325             super(context, root);
326         }
327
328         protected Object JavaDoc createObject(String JavaDoc strippedName, Object JavaDoc obj) {
329             return new NameClassPair JavaDoc(strippedName, obj.getClass().getName());
330         }
331     }
332
333
334     private static class BindingEnumeration extends AbstractNamingEnumeration {
335
336         private BindingEnumeration(SimpleNamingContext context, String JavaDoc root) throws NamingException JavaDoc {
337             super(context, root);
338         }
339
340         protected Object JavaDoc createObject(String JavaDoc strippedName, Object JavaDoc obj) {
341             return new Binding JavaDoc(strippedName, obj);
342         }
343     }
344
345 }
346
Popular Tags