KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lateralnz > panther > naming > LocalContext


1 /* ====================================================================
2  * The LateralNZ Software License, Version 1.0
3  *
4  * Copyright (c) 2003 LateralNZ. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by
21  * LateralNZ (http://www.lateralnz.org/) and other third parties."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "LateralNZ" must not be used to endorse or promote
26  * products derived from this software without prior written
27  * permission. For written permission, please
28  * contact oss@lateralnz.org.
29  *
30  * 5. Products derived from this software may not be called "Panther",
31  * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
32  * "LATERALNZ" appear in their name, without prior written
33  * permission of LateralNZ.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of LateralNZ. For more
51  * information on Lateral, please see http://www.lateralnz.com/ or
52  * http://www.lateralnz.org
53  *
54  */

55 package org.lateralnz.panther.naming;
56
57 import java.util.Hashtable JavaDoc;
58 import java.util.HashMap JavaDoc;
59 import javax.naming.*;
60 import javax.naming.spi.*;
61
62 import org.apache.log4j.Logger;
63
64 import org.lateralnz.common.util.Constants;
65 import org.lateralnz.common.util.StringUtils;
66 import org.lateralnz.panther.util.EJBRef;
67
68 /**
69  *
70  * @author jbriggs
71  */

72 public class LocalContext implements Context, Constants, Referenceable {
73   public static final String JavaDoc NAMING_CONTEXT_NAME = "panther.naming.contextname";
74   
75   private static final String JavaDoc SPLIT_REGEX = "/";
76   
77   private static final Logger log = Logger.getLogger(LocalContext.class.getName());
78
79   private static HashMap JavaDoc contexts = new HashMap JavaDoc();
80   private static HashMap JavaDoc localobjects = new HashMap JavaDoc();
81   private static HashMap JavaDoc localenv = new HashMap JavaDoc();
82   private static LocalContextNameParser nameparser = new LocalContextNameParser();
83   
84   protected HashMap JavaDoc objects;
85   private Hashtable JavaDoc environ;
86   private String JavaDoc contextName;
87     
88   class Subcontext {
89     String JavaDoc name;
90     LocalContext ctx;
91     
92     public Subcontext(String JavaDoc name, LocalContext ctx) {
93       this.name = name;
94       this.ctx = ctx;
95     }
96   }
97   
98   protected LocalContext(HashMap JavaDoc objects, Hashtable JavaDoc environ) {
99     this.objects = objects;
100     this.environ = environ;
101   }
102   
103   public LocalContext(Hashtable JavaDoc ht) throws NamingException {
104     String JavaDoc key;
105
106     if (ht != null) {
107       key = ht.toString();
108       contextName = (String JavaDoc)ht.get(NAMING_CONTEXT_NAME);
109       if (StringUtils.isEmpty(contextName)) {
110         contextName = System.getProperty(NAMING_CONTEXT_NAME);
111       }
112     }
113     else {
114       key = EMPTY;
115       contextName = EMPTY;
116     }
117     
118     contexts.put(getKey(), this);
119     
120     System.out.println("local context " + contextName + " constructed with properties " + ht + " keyed on " + key);
121     if (log.isInfoEnabled()) {
122       log.info("local context " + contextName + " constructed with properties " + ht + " keyed on " + key);
123     }
124     
125     if (!localobjects.containsKey(key)) {
126       synchronized (localobjects) {
127         if (!localobjects.containsKey(key)) {
128           HashMap JavaDoc objects = new HashMap JavaDoc();
129           Hashtable JavaDoc env = new Hashtable JavaDoc();
130           localobjects.put(key, objects);
131           localenv.put(key, env);
132         }
133       }
134     }
135     objects = (HashMap JavaDoc)localobjects.get(key);
136     environ = (Hashtable JavaDoc)localenv.get(key);
137   }
138   
139   private String JavaDoc getKey() {
140     return contextName + UNDERSCORE + Integer.toString(System.identityHashCode(this));
141   }
142   
143   protected LocalContext create(HashMap JavaDoc objects, Hashtable JavaDoc environ) {
144     return new LocalContext(objects, environ);
145   }
146   
147   protected Subcontext getSubcontext(String JavaDoc name, boolean create) throws NamingException {
148     if (name.startsWith("java:")) {
149       name = name.substring(5);
150     }
151     String JavaDoc[] split = name.split(SPLIT_REGEX);
152     LocalContext ctx = this;
153     for (int i = 0; i < split.length-1; i++) {
154       Object JavaDoc tmp = ctx.lookup(split[i]);
155       if (tmp == null) {
156         if (!create) {
157           throw new NameNotFoundException("subcontext " + split[i] + " does not exist");
158         }
159         tmp = create(new HashMap JavaDoc(), new Hashtable JavaDoc());
160         ctx.bind(split[i], tmp);
161       }
162       else if (!(tmp instanceof LocalContext)) {
163         throw new NotContextException(split[i] + " is not a subcontext");
164       }
165       
166       ctx = (LocalContext)tmp;
167     }
168     return new Subcontext(split[split.length-1], ctx);
169   }
170
171   public static Context getContext(String JavaDoc name) {
172     return (Context)contexts.get(name);
173   }
174   
175   public Object JavaDoc addToEnvironment(String JavaDoc name, Object JavaDoc obj) throws NamingException {
176     return environ.put(name, obj);
177   }
178   
179   public void bind(Name name, Object JavaDoc obj) throws NamingException {
180     bind(name.toString(), obj);
181   }
182   
183   public void bind(String JavaDoc name, Object JavaDoc obj) throws NamingException {
184     if (objects.containsKey(name)) {
185       throw new NameAlreadyBoundException(name);
186     }
187     else {
188       rebind(name, obj);
189     }
190   }
191   
192   public void close() throws NamingException {
193     objects = null;
194     environ = null;
195     contexts.remove(getKey());
196   }
197   
198   public String JavaDoc composeName(String JavaDoc str, String JavaDoc prefix) throws NamingException {
199     if (!prefix.endsWith(FORWARD_SLASH)) {
200       prefix = prefix + FORWARD_SLASH;
201     }
202     return prefix + str;
203   }
204   
205   public Name composeName(Name name, Name prefix) throws NamingException {
206     return nameparser.parse(composeName(name.toString(), prefix.toString()));
207   }
208   
209   public Context createSubcontext(Name name) throws NamingException {
210     return createSubcontext(name.toString());
211   }
212   
213   public Context createSubcontext(String JavaDoc name) throws NamingException {
214     Subcontext sctx = getSubcontext(name, false);
215     if (objects.containsKey(sctx.name)) {
216       throw new NameAlreadyBoundException();
217     }
218     LocalContext newctx = create(new HashMap JavaDoc(), new Hashtable JavaDoc());
219     sctx.ctx.objects.put(sctx.name, new Binding(sctx.name, newctx.getClass().getName(), newctx));
220
221     return newctx;
222   }
223   
224   public void destroySubcontext(Name name) throws NamingException {
225     destroySubcontext(name.toString());
226   }
227   
228   public void destroySubcontext(String JavaDoc name) throws NamingException {
229     if (!objects.containsKey(name)) {
230       throw new NameNotFoundException(name);
231     }
232     Binding binding = (Binding)objects.get(name);
233     Object JavaDoc obj = null;
234     if (binding != null) {
235       obj = binding.getObject();
236     }
237     
238     if (obj != null && obj instanceof Context) {
239       objects.remove(name);
240     }
241     else {
242       throw new NotContextException(name);
243     }
244   }
245   
246   public Hashtable JavaDoc getEnvironment() throws NamingException {
247     return environ;
248   }
249   
250   public String JavaDoc getNameInNamespace() throws NamingException {
251     throw new OperationNotSupportedException();
252   }
253   
254   public NameParser getNameParser(Name name) throws NamingException {
255     return nameparser;
256   }
257   
258   public NameParser getNameParser(String JavaDoc str) throws NamingException {
259     return nameparser;
260   }
261   
262   public Reference getReference() {
263     Reference ref = new Reference(getClass().getName(), LocalContextFactory.class.getName(), null);
264     ref.add(new StringRefAddr("name", getKey()));
265     return ref;
266   }
267   
268   public NamingEnumeration list(String JavaDoc name) throws NamingException {
269     Object JavaDoc obj;
270     if (StringUtils.isEmpty(name)) {
271       obj = this;
272     }
273     else {
274       obj = lookup(name);
275     }
276     
277     if (obj != null && obj instanceof LocalContext) {
278       return new LocalContextNamingEnumeration((LocalContext)obj);
279     }
280     else {
281       return new LocalContextNamingEnumeration();
282     }
283   }
284   
285   public NamingEnumeration list(Name name) throws NamingException {
286     return list(name.toString());
287   }
288   
289   public NamingEnumeration listBindings(String JavaDoc name) throws NamingException {
290     return list(name);
291   }
292   
293   public javax.naming.NamingEnumeration JavaDoc listBindings(Name name) throws NamingException {
294     return listBindings(name.toString());
295   }
296   
297   public Object JavaDoc lookup(String JavaDoc name) throws NamingException {
298     if (StringUtils.isEmpty(name)) {
299       return create(objects, (Hashtable JavaDoc)environ.clone());
300     }
301     
302     Subcontext sctx = getSubcontext(name, false);
303     if (!sctx.ctx.objects.containsKey(sctx.name)) {
304       String JavaDoc ejbname = EJBRef.getEJBName();
305       if (!StringUtils.isEmpty(ejbname) && !name.startsWith(ejbname + UNDERSCORE)) {
306         return lookup(ejbname + UNDERSCORE + name);
307       }
308       else {
309         throw new NameNotFoundException(name);
310       }
311     }
312     else {
313       Binding binding = (Binding)sctx.ctx.objects.get(sctx.name);
314       if (binding != null) {
315         Object JavaDoc obj = binding.getObject();
316         if (obj instanceof Reference) {
317           //return ((Reference)obj).
318
try {
319             Reference ref = (Reference)obj;
320             Class JavaDoc c = Class.forName(ref.getFactoryClassName());
321             ObjectFactory of = (ObjectFactory)c.newInstance();
322             return of.getObjectInstance(obj, nameparser.parse(name), sctx.ctx, sctx.ctx.getEnvironment());
323           }
324           catch (Exception JavaDoc e) {
325             throw new NamingException(e.getMessage());
326           }
327         }
328         else {
329           return obj;
330         }
331       }
332       else {
333         return null;
334       }
335     }
336   }
337   
338   public Object JavaDoc lookup(Name name) throws NamingException {
339     return lookup(name.toString());
340   }
341   
342   public Object JavaDoc lookupLink(Name name) throws NamingException {
343     return lookup(name);
344   }
345   
346   public Object JavaDoc lookupLink(String JavaDoc name) throws NamingException {
347     return lookup(name);
348   }
349   
350   public void rebind(String JavaDoc name, Object JavaDoc obj) throws NamingException {
351     Subcontext sctx = getSubcontext(name, false);
352     
353     Binding binding = new Binding(sctx.name, obj.getClass().getName(), obj);
354     sctx.ctx.objects.put(sctx.name, binding);
355   }
356   
357   public void rebind(Name name, Object JavaDoc obj) throws NamingException {
358     rebind(name.toString(), obj);
359   }
360   
361   public Object JavaDoc removeFromEnvironment(String JavaDoc name) throws NamingException {
362     return environ.remove(name);
363   }
364   
365   public void rename(String JavaDoc name1, String JavaDoc name2) throws NamingException {
366     Subcontext sctx1 = getSubcontext(name1, false);
367     Subcontext sctx2 = getSubcontext(name2, false);
368     Object JavaDoc obj = sctx1.ctx.objects.remove(sctx1.name);
369
370     sctx2.ctx.objects.put(sctx2.name, obj);
371   }
372   
373   public void rename(Name name1, Name name2) throws NamingException {
374     rename(name1.toString(), name2.toString());
375   }
376   
377   public void unbind(String JavaDoc name) throws NamingException {
378     Subcontext sctx = getSubcontext(name, false);
379     sctx.ctx.objects.remove(name);
380   }
381   
382   public void unbind(Name name) throws NamingException {
383     unbind(name.toString());
384   }
385   
386   
387   public static void main(String JavaDoc[] args) throws Exception JavaDoc {
388     LocalContext ctx = (LocalContext)(new LocalContextFactory().getInitialContext(null));
389     
390     Context ctx2 = ctx.createSubcontext("java:comp");
391     ctx2.createSubcontext("env");
392     
393     NamingEnumeration en = ctx.list("");
394     while (en.hasMore()) {
395       System.out.println("root.... " + en.next());
396     }
397     
398     en = ctx.list("java:comp");
399     while (en.hasMore()) {
400       System.out.println("java:comp.... " + en.next());
401     }
402   }
403 }
Popular Tags