KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > jndi > ReadOnlyContext


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

18
19 package org.apache.activemq.jndi;
20
21 import javax.naming.*;
22 import javax.naming.spi.NamingManager JavaDoc;
23 import java.io.Serializable JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29
30 /**
31  * A read-only Context
32  * <p/>
33  * This version assumes it and all its subcontext are read-only and any attempt
34  * to modify (e.g. through bind) will result in an OperationNotSupportedException.
35  * Each Context in the tree builds a cache of the entries in all sub-contexts
36  * to optimise the performance of lookup.
37  * </p>
38  * <p>This implementation is intended to optimise the performance of lookup(String)
39  * to about the level of a HashMap get. It has been observed that the scheme
40  * resolution phase performed by the JVM takes considerably longer, so for
41  * optimum performance lookups should be coded like:</p>
42  * <code>
43  * Context componentContext = (Context)new InitialContext().lookup("java:comp");
44  * String envEntry = (String) componentContext.lookup("env/myEntry");
45  * String envEntry2 = (String) componentContext.lookup("env/myEntry2");
46  * </code>
47  *
48  * @version $Revision: 1.2 $ $Date: 2005/08/27 03:52:39 $
49  */

50 public class ReadOnlyContext implements Context, Serializable JavaDoc {
51     private static final long serialVersionUID = -5754338187296859149L;
52     protected static final NameParser nameParser = new NameParserImpl();
53
54     protected final Hashtable JavaDoc environment; // environment for this context
55
protected final Map JavaDoc bindings; // bindings at my level
56
protected final Map JavaDoc treeBindings; // all bindings under me
57

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

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