KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > naming > java > ReadOnlyContext


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

17
18 package org.apache.geronimo.naming.java;
19
20 import java.util.Collections JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.io.Serializable JavaDoc;
26
27 import javax.naming.Binding JavaDoc;
28 import javax.naming.CompositeName JavaDoc;
29 import javax.naming.Context JavaDoc;
30 import javax.naming.LinkRef JavaDoc;
31 import javax.naming.Name JavaDoc;
32 import javax.naming.NameClassPair JavaDoc;
33 import javax.naming.NameNotFoundException JavaDoc;
34 import javax.naming.NameParser JavaDoc;
35 import javax.naming.NamingEnumeration JavaDoc;
36 import javax.naming.NamingException JavaDoc;
37 import javax.naming.NotContextException JavaDoc;
38 import javax.naming.OperationNotSupportedException JavaDoc;
39 import javax.naming.Reference JavaDoc;
40 import javax.naming.InitialContext JavaDoc;
41 import javax.naming.spi.NamingManager JavaDoc;
42
43 import org.apache.geronimo.naming.reference.SimpleReference;
44 import org.apache.geronimo.naming.reference.KernelAwareReference;
45 import org.apache.geronimo.naming.reference.ClassLoaderAwareReference;
46 import org.apache.geronimo.kernel.Kernel;
47
48 /**
49  * A read-only Context in the java: namespace.
50  * <p>
51  * This version assumes it and all its subcontext are read-only and any attempt
52  * to modify (e.g. through bind) will result in an OperationNotSupportedException.
53  * Each Context in the tree builds a cache of the entries in all sub-contexts
54  * to optimise the performance of lookup.
55  * </p>
56  * <p>This implementation is intended to optimise the performance of lookup(String)
57  * to about the level of a HashMap get. It has been observed that the scheme
58  * resolution phase performed by the JVM takes considerably longer, so for
59  * optimum performance lookups should be coded like:</p>
60  * <code>
61  * Context componentContext = (Context)new InitialContext().lookup("java:comp");
62  * String envEntry = (String) componentContext.lookup("env/myEntry");
63  * String envEntry2 = (String) componentContext.lookup("env/myEntry2");
64  * </code>
65  *
66  * @version $Rev: 126558 $ $Date: 2005-01-26 15:13:09 -0800 (Wed, 26 Jan 2005) $
67  */

68 public class ReadOnlyContext implements Context JavaDoc, Serializable JavaDoc {
69     protected final Hashtable JavaDoc env; // environment for this context
70
protected final Map JavaDoc bindings; // bindings at my level
71
protected final Map JavaDoc treeBindings; // all bindings under me
72

73     private boolean frozen = false;
74
75     public ReadOnlyContext() {
76         env = new Hashtable JavaDoc();
77         bindings = new HashMap JavaDoc();
78         treeBindings = new HashMap JavaDoc();
79     }
80
81     ReadOnlyContext(Hashtable JavaDoc env) {
82         if (env == null) {
83             this.env = new Hashtable JavaDoc();
84         } else {
85             this.env = new Hashtable JavaDoc(env);
86         }
87         this.bindings = Collections.EMPTY_MAP;
88         this.treeBindings = Collections.EMPTY_MAP;
89     }
90
91     protected ReadOnlyContext(ReadOnlyContext clone, Hashtable JavaDoc env) {
92         this.bindings = clone.bindings;
93         this.treeBindings = clone.treeBindings;
94         this.env = new Hashtable JavaDoc(env);
95     }
96
97     public void freeze() {
98         frozen = true;
99     }
100
101     boolean isFrozen() {
102         return frozen;
103     }
104
105     /**
106      * internalBind is intended for use only during setup or possibly by suitably synchronized superclasses.
107      * It binds every possible lookup into a map in each context. To do this, each context
108      * strips off one name segment and if necessary creates a new context for it. Then it asks that context
109      * to bind the remaining name. It returns a map containing all the bindings from the next context, plus
110      * the context it just created (if it in fact created it). (the names are suitably extended by the segment
111      * originally lopped off).
112      * @param name
113      * @param value
114      * @return
115      * @throws javax.naming.NamingException
116      */

117     protected Map JavaDoc internalBind(String JavaDoc name, Object JavaDoc value) throws NamingException JavaDoc {
118         assert name != null && name.length() > 0;
119         assert !frozen;
120
121         Map JavaDoc newBindings = new HashMap JavaDoc();
122         int pos = name.indexOf('/');
123         if (pos == -1) {
124             if (treeBindings.put(name, value) != null) {
125                 throw new NamingException JavaDoc("Something already bound at " + name);
126             }
127             bindings.put(name, value);
128             newBindings.put(name, value);
129         } else {
130             String JavaDoc segment = name.substring(0, pos);
131             assert segment != null;
132             assert !segment.equals("");
133             Object JavaDoc o = treeBindings.get(segment);
134             if (o == null) {
135                 o = newContext();
136                 treeBindings.put(segment, o);
137                 bindings.put(segment, o);
138                 newBindings.put(segment, o);
139             } else if (!(o instanceof ReadOnlyContext)) {
140                 throw new NamingException JavaDoc("Something already bound where a subcontext should go");
141             }
142             ReadOnlyContext readOnlyContext = (ReadOnlyContext)o;
143             String JavaDoc remainder = name.substring(pos + 1);
144             Map JavaDoc subBindings = readOnlyContext.internalBind(remainder, value);
145             for (Iterator JavaDoc iterator = subBindings.entrySet().iterator(); iterator.hasNext();) {
146                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
147                 String JavaDoc subName = segment + "/" + entry.getKey();
148                 Object JavaDoc bound = entry.getValue();
149                 treeBindings.put(subName, bound);
150                 newBindings.put(subName, bound);
151             }
152         }
153         return newBindings;
154     }
155
156     protected ReadOnlyContext newContext() {
157         return new ReadOnlyContext();
158     }
159
160     public void setKernel(Kernel kernel) {
161         for (Iterator JavaDoc iterator = treeBindings.values().iterator(); iterator.hasNext();) {
162             Object JavaDoc o = iterator.next();
163             if (o instanceof KernelAwareReference) {
164                 ((KernelAwareReference) o).setKernel(kernel);
165             }
166         }
167     }
168
169     public void setClassLoader(ClassLoader JavaDoc classLoader) {
170         for (Iterator JavaDoc iterator = treeBindings.values().iterator(); iterator.hasNext();) {
171             Object JavaDoc o = iterator.next();
172             if (o instanceof ClassLoaderAwareReference) {
173                 ((ClassLoaderAwareReference) o).setClassLoader(classLoader);
174             }
175         }
176     }
177
178     public Object JavaDoc addToEnvironment(String JavaDoc propName, Object JavaDoc propVal) throws NamingException JavaDoc {
179         return env.put(propName, propVal);
180     }
181
182     public Hashtable JavaDoc getEnvironment() throws NamingException JavaDoc {
183         return (Hashtable JavaDoc) env.clone();
184     }
185
186     public Object JavaDoc removeFromEnvironment(String JavaDoc propName) throws NamingException JavaDoc {
187         return env.remove(propName);
188     }
189
190     public Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc {
191         if (name.length() == 0) {
192             return this;
193         }
194         Object JavaDoc result = treeBindings.get(name);
195         if (result == null) {
196             if (name.indexOf(':') > 0) {
197                 Context JavaDoc ctx = new InitialContext JavaDoc();
198                 return ctx.lookup(name);
199             } else {
200                 // Split out the first name of the path
201
// and look for it in the bindings map.
202
CompositeName JavaDoc path = new CompositeName JavaDoc(name);
203
204                 if (path.size() == 0) {
205                     return this;
206                 } else {
207                     Object JavaDoc obj = bindings.get(path.get(0));
208                     if (obj == null){
209                         throw new NameNotFoundException JavaDoc(name);
210                     } else if (obj instanceof Context JavaDoc && path.size() > 1){
211                         Context JavaDoc subContext = (Context JavaDoc) obj;
212                         obj = subContext.lookup(path.getSuffix(1));
213                     }
214                     return obj;
215                 }
216             }
217         }
218         if (result instanceof SimpleReference) {
219             try {
220                 result = ((SimpleReference) result).getContent();
221             } catch (Exception JavaDoc e) {
222                 throw (NamingException JavaDoc)new NamingException JavaDoc("could not look up : " + name).initCause(e);
223             }
224         }
225         if (result instanceof LinkRef JavaDoc) {
226             LinkRef JavaDoc ref = (LinkRef JavaDoc) result;
227             result = lookup(ref.getLinkName());
228         }
229         if (result instanceof Reference JavaDoc) {
230             try {
231                 result = NamingManager.getObjectInstance(result, null, null, this.env);
232             } catch (NamingException JavaDoc e) {
233                 throw e;
234             } catch (Exception JavaDoc e) {
235                 throw (NamingException JavaDoc)new NamingException JavaDoc("could not look up : " + name).initCause(e);
236             }
237         }
238         return result;
239     }
240
241     public Object JavaDoc lookup(Name JavaDoc name) throws NamingException JavaDoc {
242         return lookup(name.toString());
243     }
244
245     public Object JavaDoc lookupLink(String JavaDoc name) throws NamingException JavaDoc {
246         return lookup(name);
247     }
248
249     public Name JavaDoc composeName(Name JavaDoc name, Name JavaDoc prefix) throws NamingException JavaDoc {
250         Name JavaDoc result = (Name JavaDoc) prefix.clone();
251         result.addAll(name);
252         return result;
253     }
254
255     public String JavaDoc composeName(String JavaDoc name, String JavaDoc prefix) throws NamingException JavaDoc {
256         CompositeName JavaDoc result = new CompositeName JavaDoc(prefix);
257         result.addAll(new CompositeName JavaDoc(name));
258         return result.toString();
259     }
260
261     public NamingEnumeration JavaDoc list(String JavaDoc name) throws NamingException JavaDoc {
262         Object JavaDoc o = lookup(name);
263         if (o == this) {
264             return new ListEnumeration();
265         } else if (o instanceof Context JavaDoc) {
266             return ((Context JavaDoc) o).list("");
267         } else {
268             throw new NotContextException JavaDoc();
269         }
270     }
271
272     public NamingEnumeration JavaDoc listBindings(String JavaDoc name) throws NamingException JavaDoc {
273         Object JavaDoc o = lookup(name);
274         if (o == this) {
275             return new ListBindingEnumeration();
276         } else if (o instanceof Context JavaDoc) {
277             return ((Context JavaDoc) o).listBindings("");
278         } else {
279             throw new NotContextException JavaDoc();
280         }
281     }
282
283     public Object JavaDoc lookupLink(Name JavaDoc name) throws NamingException JavaDoc {
284         return lookupLink(name.toString());
285     }
286
287     public NamingEnumeration JavaDoc list(Name JavaDoc name) throws NamingException JavaDoc {
288         return list(name.toString());
289     }
290
291     public NamingEnumeration JavaDoc listBindings(Name JavaDoc name) throws NamingException JavaDoc {
292         return listBindings(name.toString());
293     }
294
295     public void bind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
296         throw new OperationNotSupportedException JavaDoc();
297     }
298
299     public void bind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
300         throw new OperationNotSupportedException JavaDoc();
301     }
302
303     public void close() throws NamingException JavaDoc {
304         throw new OperationNotSupportedException JavaDoc();
305     }
306
307     public Context JavaDoc createSubcontext(Name JavaDoc name) throws NamingException JavaDoc {
308         throw new OperationNotSupportedException JavaDoc();
309     }
310
311     public Context JavaDoc createSubcontext(String JavaDoc name) throws NamingException JavaDoc {
312         throw new OperationNotSupportedException JavaDoc();
313     }
314
315     public void destroySubcontext(Name JavaDoc name) throws NamingException JavaDoc {
316         throw new OperationNotSupportedException JavaDoc();
317     }
318
319     public void destroySubcontext(String JavaDoc name) throws NamingException JavaDoc {
320         throw new OperationNotSupportedException JavaDoc();
321     }
322
323     public String JavaDoc getNameInNamespace() throws NamingException JavaDoc {
324         throw new OperationNotSupportedException JavaDoc();
325     }
326
327     public NameParser JavaDoc getNameParser(Name JavaDoc name) throws NamingException JavaDoc {
328         throw new OperationNotSupportedException JavaDoc();
329     }
330
331     public NameParser JavaDoc getNameParser(String JavaDoc name) throws NamingException JavaDoc {
332         throw new OperationNotSupportedException JavaDoc();
333     }
334
335     public void rebind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
336         throw new OperationNotSupportedException JavaDoc();
337     }
338
339     public void rebind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
340         throw new OperationNotSupportedException JavaDoc();
341     }
342
343     public void rename(Name JavaDoc oldName, Name JavaDoc newName) throws NamingException JavaDoc {
344         throw new OperationNotSupportedException JavaDoc();
345     }
346
347     public void rename(String JavaDoc oldName, String JavaDoc newName) throws NamingException JavaDoc {
348         throw new OperationNotSupportedException JavaDoc();
349     }
350
351     public void unbind(Name JavaDoc name) throws NamingException JavaDoc {
352         throw new OperationNotSupportedException JavaDoc();
353     }
354
355     public void unbind(String JavaDoc name) throws NamingException JavaDoc {
356         throw new OperationNotSupportedException JavaDoc();
357     }
358
359     private abstract class LocalNamingEnumeration implements NamingEnumeration JavaDoc {
360         private Iterator JavaDoc i = bindings.entrySet().iterator();
361
362         public boolean hasMore() throws NamingException JavaDoc {
363             return i.hasNext();
364         }
365
366         public boolean hasMoreElements() {
367             return i.hasNext();
368         }
369
370         protected Map.Entry JavaDoc getNext() {
371             return (Map.Entry JavaDoc) i.next();
372         }
373
374         public void close() throws NamingException JavaDoc {
375         }
376     }
377
378     private class ListEnumeration extends LocalNamingEnumeration {
379         public Object JavaDoc next() throws NamingException JavaDoc {
380             return nextElement();
381         }
382
383         public Object JavaDoc nextElement() {
384             Map.Entry JavaDoc entry = getNext();
385             return new NameClassPair JavaDoc((String JavaDoc) entry.getKey(), entry.getValue().getClass().getName());
386         }
387     }
388
389     private class ListBindingEnumeration extends LocalNamingEnumeration {
390         public Object JavaDoc next() throws NamingException JavaDoc {
391             return nextElement();
392         }
393
394         public Object JavaDoc nextElement() {
395             Map.Entry JavaDoc entry = getNext();
396             return new Binding JavaDoc((String JavaDoc) entry.getKey(), entry.getValue());
397         }
398     }
399 }
400
Popular Tags