KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > naming > TransientContext


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.naming;
24
25 import java.util.*;
26 import java.io.*;
27 import javax.naming.*;
28 import javax.rmi.PortableRemoteObject JavaDoc;
29 import java.rmi.*;
30 import com.sun.enterprise.util.ORBManager;
31 import org.omg.CosNaming.NamingContext JavaDoc;
32 import org.omg.CosNaming.NameComponent JavaDoc;
33 import org.omg.CosNaming.NamingContextHelper JavaDoc;
34 import javax.rmi.CORBA.Tie JavaDoc;
35
36 //START OF IASRI 4660742
37
import java.util.logging.*;
38 import com.sun.logging.*;
39 //END OF IASRI 4660742
40

41
42 /** Class to implement multiple level of subcontexts in SerialContext. To use this
43  * class a new object of class InitialContext (env) should be instantiated.
44  * The env i.e the Environment is initialised with SerialInitContextFactory
45  * An example for using this is in /test/subcontext
46  */

47 public class TransientContext implements Context JavaDoc, Serializable {
48
49     // START OF IASRI 4660742
50
static Logger _logger=LogDomains.getLogger(LogDomains.JNDI_LOGGER);
51     // END OF IASRI 4660742
52

53     public static final boolean debug = false;
54     Hashtable myEnv;
55     private Hashtable bindings = new Hashtable ();
56     static NameParser myParser = new SerialNameParser();
57     
58     public TransientContext() {
59     }
60     
61     /**
62      * Create a subcontext with the specified name.
63      * @return the created subcontext.
64      * @exception NamingException if there is a naming exception.
65      * @exception RMIException if there is an RMI exception.
66      */

67     public Context JavaDoc createSubcontext(String JavaDoc name) throws NamingException {
68     return drillDownAndCreateSubcontext (name);
69     }
70     
71     /**
72      * Create a subcontext with the specified name.
73      * @return the created subcontext.
74      * @exception NamingException if there is a naming exception.
75      * @exception RMIException if there is an RMI exception.
76      */

77     public Context JavaDoc createSubcontext(Name name) throws NamingException {
78     return createSubcontext(name.toString());
79     }
80
81     /**
82      * Destroy the subcontext with the specified name.
83      * @exception NamingException if there is a naming exception.
84      * @exception RMIException if there is an RMI exception.
85      */

86     public void destroySubcontext(String JavaDoc name) throws NamingException {
87     drillDownAndDestroySubcontext (name);
88     }
89
90     /**
91      * Destroy the subcontext with the specified name.
92      * @exception NamingException if there is a naming exception.
93      * @exception RMIException if there is an RMI exception.
94      */

95     public void destroySubcontext(Name name) throws NamingException {
96     destroySubcontext(name.toString());
97     }
98     
99     /** Handles making nested subcontexts
100      * i.e. if you want abcd/efg/hij. It will go subcontext efg in abcd
101      * (if not present already - it will create it) and then
102      * make subcontext hij
103      * @return the created subcontext.
104      * @exception NamingException if there is a Naming exception
105      */

106     public Context JavaDoc drillDownAndCreateSubcontext (String JavaDoc name)
107     throws NamingException {
108     Name n = new CompositeName (name);
109     if (n.size () < 1){
110         throw new InvalidNameException ("Cannot create empty subcontext");
111     }
112     if (n.size() == 1){ // bottom
113
if (bindings.containsKey (name)){
114         throw new NameAlreadyBoundException ("Subcontext " +
115                              name + "already present");
116         }
117        
118         TransientContext ctx = null;
119         ctx = new TransientContext ();
120         bindings.put (name, ctx);
121         return ctx;
122     } else {
123         String JavaDoc suffix = n.getSuffix(1).toString();
124         Context JavaDoc retCtx, ctx; // the created context
125
try {
126         ctx = resolveContext(n.get(0));
127         } catch (NameNotFoundException e){
128         ctx = new TransientContext ();
129         }
130         retCtx = ctx.createSubcontext (suffix);
131         bindings.put (n.get(0), ctx);
132         return retCtx;
133     }
134     }
135
136     /** Handles deleting nested subcontexts
137      * i.e. if you want delete abcd/efg/hij. It will go subcontext efg in abcd
138      * it will delete it) and then delete subcontext hij
139      * @exception NamingException if there is a naming exception
140      */

141     public void drillDownAndDestroySubcontext (String JavaDoc name)
142     throws NamingException {
143     Name n = new CompositeName (name);
144     if (n.size () < 1){
145         throw new InvalidNameException ("Cannot destoy empty subcontext");
146     }
147     if (n.size() == 1){ // bottom
148
if (bindings.containsKey (name)){
149         bindings.remove (name);
150         }
151         else{
152         throw new NameNotFoundException ("Subcontext: " + name +
153                         " not found");
154         }
155     } else {
156         String JavaDoc suffix = n.getSuffix(1).toString();
157         Context JavaDoc ctx; // the context to drill down from
158
ctx = resolveContext(n.get(0));
159         ctx.destroySubcontext (suffix);
160     }
161     }
162    
163     /**
164      * Lookup the specified name.
165      * @return the object or context bound to the name.
166      * @exception NamingException if there is a naming exception.
167      * @exception RemoteException if there is an RMI exception.
168      */

169      public Object JavaDoc lookup(String JavaDoc name) throws NamingException {
170     Name n = new CompositeName(name);
171     if (n.size() < 1) {
172         throw new InvalidNameException("Cannot bind empty name");
173     }
174     if(n.size() == 1) { // bottom
175
return doLookup(n.toString());
176     } else {
177         String JavaDoc suffix = n.getSuffix(1).toString();
178         TransientContext ctx = resolveContext(n.get(0));
179         return ctx.lookup(suffix);
180     }
181     }
182
183     /**
184      * Lookup the specified name.
185      * @return the object or context bound to the name.
186      * @exception NamingException if there is a naming exception.
187      * @exception RemoteException if there is an RMI exception.
188      */

189     public Object JavaDoc lookup(Name name) throws NamingException {
190     return lookup(name.toString());
191     }
192
193     /**
194      * Lookup the specified name in the current objects hashtable.
195      * @return the object or context bound to the name.
196      * @exception NamingException if there is a naming exception.
197      * @exception RemoteException if there is an RMI exception.
198      */

199     private Object JavaDoc doLookup(String JavaDoc name) throws NamingException
200     {
201     Object JavaDoc answer = bindings.get(name);
202     if (answer == null) {
203             throw new NameNotFoundException(name + " not found");
204         }
205         return answer;
206     }
207     
208     /**
209      * Bind the object to the specified name.
210      * @exception NamingException if there is a naming exception.
211      * @exception RemoteException if there is an RMI exception.
212      */

213     public void bind(String JavaDoc name, Object JavaDoc obj) throws NamingException{
214     Name n = new CompositeName(name);
215     if (n.size() < 1) {
216         throw new InvalidNameException("Cannot bind empty name");
217     }
218     if(n.size() == 1) { // bottom
219
doBindOrRebind(n.toString(), obj, false);
220     } else {
221         String JavaDoc suffix = n.getSuffix(1).toString();
222         Context JavaDoc ctx;
223         try {
224         ctx = resolveContext(n.get(0));
225         } catch (NameNotFoundException e){
226         ctx = createSubcontext (n.get (0));
227         }
228         ctx.bind(suffix, obj);
229     }
230     }
231
232     /**
233      * Bind the object to the specified name.
234      * @exception NamingException if there is a naming exception.
235      * @exception RemoteException if there is an RMI exception.
236      */

237     public void bind(Name name, Object JavaDoc obj)
238     throws NamingException {
239     bind(name.toString(), obj);
240     }
241    
242     /**
243      * Finds out if the subcontext specified is present in the current context
244      * @exception NamingException if there is a naming exception
245      * @exception RemoteException if there is a RMI exception
246      */

247     /* Finds if the name searched for is a type of context or anyother type of
248      * object.
249      */

250     private TransientContext resolveContext(String JavaDoc s) throws NamingException {
251     //TransientContext ctx = (TransientContext) bindings.get(s);
252
TransientContext ctx;
253     Object JavaDoc obj = bindings.get (s);
254     if(obj == null) {
255         throw new NameNotFoundException();
256     }
257     if (obj instanceof TransientContext){
258         ctx = (TransientContext) obj;
259     }
260     else {
261         throw new NameAlreadyBoundException (s);
262     }
263     return ctx;
264     }
265     
266     /**
267      * Binds or rebinds the object specified by name
268      * @exception NamingException if there is a naming exception
269      * @exception RemoteException if there is a RMI exception
270      */

271     private void doBindOrRebind(String JavaDoc name, Object JavaDoc obj, boolean rebind)
272     throws NamingException
273     {
274         if (name.equals("")) {
275             throw new InvalidNameException("Cannot bind empty name");
276         }
277     if(!rebind) {
278             if (bindings.get(name) != null) {
279                 throw new NameAlreadyBoundException(
280                             "Use rebind to override");
281             }
282     }
283         bindings.put(name, obj);
284     }
285
286    
287     /**
288      * Rebinds the object specified by name
289      * @exception NamingException if there is a naming exception
290      * @exception RemoteException if there is a RMI exception
291      */

292     public void rebind(String JavaDoc name, Object JavaDoc obj)
293         throws NamingException {
294     Name n = new CompositeName(name);
295     if (n.size() < 1) {
296         throw new InvalidNameException("Cannot bind empty name");
297     }
298     if(n.size() == 1) { // bottom
299
doBindOrRebind(n.toString(), obj, true);
300     } else {
301         String JavaDoc suffix = n.getSuffix(1).toString();
302         Context JavaDoc ctx=null;
303         try {
304         ctx = resolveContext(n.get(0));
305         ctx.rebind(suffix, obj);
306         } catch (NameNotFoundException e){
307         ctx = createSubcontext (n.get(0));
308         ctx.rebind(suffix, obj);
309         }
310     }
311     }
312
313     /**
314      * Binds or rebinds the object specified by name
315      * @exception NamingException if there is a naming exception
316      * @exception RemoteException if there is a RMI exception
317      */

318     public void rebind(Name name, Object JavaDoc obj)
319     throws NamingException {
320     rebind(name.toString(), obj);
321     }
322
323     /**
324      * Unbinds the object specified by name. Traverses down the context tree
325      * and unbinds the object if required.
326      * @exception NamingException if there is a naming exception
327      * @exception RemoteException if there is a RMI exception
328      */

329     private void doUnbind(String JavaDoc name) throws NamingException {
330         if (name.equals("")) {
331             throw new InvalidNameException("Cannot unbind empty name");
332         }
333         if (bindings.get(name) == null) {
334         throw new NameNotFoundException(
335                         "Cannot find name to unbind");
336         }
337     bindings.remove(name);
338     }
339
340     /**
341      * Unbinds the object specified by name. Calls itself recursively to
342      * traverse down the context tree and unbind the object.
343      * @exception NamingException if there is a naming exception
344      * @exception RemoteException if there is a RMI exception
345      */

346     public void unbind(String JavaDoc name) throws NamingException {
347     Name n = new CompositeName(name);
348     if (n.size() < 1) {
349         throw new InvalidNameException("Cannot unbind empty name");
350     }
351     if(n.size() == 1) { // bottom
352
doUnbind(n.toString());
353     } else {
354         String JavaDoc suffix = n.getSuffix(1).toString();
355         TransientContext ctx = resolveContext(n.get(0));
356         ctx.unbind(suffix);
357     }
358     }
359
360     /**
361      * Unbinds the object specified by name
362      * @exception NamingException if there is a naming exception
363      * @exception RemoteException if there is a RMI exception
364      */

365     public void unbind(Name name)
366     throws NamingException {
367     unbind(name.toString());
368     }
369
370     /**
371      * Rename the object specified by oldname to newname
372      * @exception NamingException if there is a naming exception
373      * @exception RemoteException if there is a RMI exception
374      */

375     public void rename(Name oldname, Name newname) throws NamingException {
376     rename(oldname.toString(), newname.toString());
377     }
378
379     /**
380      * Rename the object specified by oldname to newname
381      * @exception NamingException if there is a naming exception
382      * @exception RemoteException if there is a RMI exception
383      */

384     public void rename(String JavaDoc oldname, String JavaDoc newname)
385         throws NamingException {
386         if (oldname.equals("") || newname.equals("")) {
387             throw new InvalidNameException("Cannot rename empty name");
388         }
389
390         // Check if new name exists
391
if (bindings.get(newname) != null) {
392             throw new NameAlreadyBoundException(newname +
393                                                 " is already bound");
394         }
395
396         // Check if old name is bound
397
Object JavaDoc oldBinding = bindings.remove(oldname);
398         if (oldBinding == null) {
399             throw new NameNotFoundException(oldname + " not bound");
400         }
401
402         bindings.put(newname, oldBinding);
403     }
404
405     /**
406      * list the objects stored by the current context
407      * @exception NamingException if there is a naming exception
408      * @exception RemoteException if there is a RMI exception
409      */

410     public Hashtable list() {
411     return bindings;
412     }
413
414     /**
415      * List the objects specified by name.
416      * @exception NamingException if there is a naming exception
417      * @exception RemoteException if there is a RMI exception
418      */

419     public Hashtable listContext(String JavaDoc name) throws NamingException {
420     if(debug) {
421         print(bindings);
422     }
423     if(name.equals(""))
424         return bindings;
425     
426     Object JavaDoc target = lookup(name);
427     if(target instanceof TransientContext) {
428         return ((TransientContext)target).listContext("");
429     }
430     throw new NotContextException(name + " cannot be listed");
431     }
432
433     /**
434      * List the objects specified by name.
435      * @exception NamingException if there is a naming exception
436      * @exception RemoteException if there is a RMI exception
437      */

438     public NamingEnumeration list(Name name) throws NamingException {
439         return list(name.toString());
440     }
441
442     /**
443      * List the objects specified by name.
444      * @exception NamingException if there is a naming exception
445      * @exception RemoteException if there is a RMI exception
446      */

447     public NamingEnumeration list(String JavaDoc name) throws NamingException {
448     if(debug) {
449         print(bindings);
450     }
451     if(name.equals(""))
452         return new RepNames(bindings);
453     
454     Object JavaDoc target = lookup(name);
455     if(target instanceof Context JavaDoc) {
456         return ((Context JavaDoc)target).list("");
457     }
458     throw new NotContextException(name + " cannot be listed");
459     }
460
461     /**
462      * List the bindings of objects present in name.
463      * @exception NamingException if there is a naming exception
464      * @exception RemoteException if there is a RMI exception
465      */

466     public NamingEnumeration listBindings(String JavaDoc name) throws NamingException {
467     if(name.equals(""))
468         return new RepBindings(bindings);
469
470     Object JavaDoc target = lookup(name);
471     if(target instanceof Context JavaDoc) {
472         return ((Context JavaDoc)target).listBindings("");
473     }
474     throw new NotContextException(name + " cannot be listed");
475     }
476
477     /**
478      * List the binding of objects specified by name.
479      * @exception NamingException if there is a naming exception
480      * @exception RemoteException if there is a RMI exception
481      */

482     public NamingEnumeration listBindings(Name name) throws NamingException {
483     return listBindings(name.toString());
484     }
485
486     /**
487      * Lookup the name.
488      * @exception NamingException if there is a naming exception
489      * @exception RemoteException if there is a RMI exception
490      */

491     public Object JavaDoc lookupLink(String JavaDoc name) throws NamingException {
492         // This flat context does not treat links specially
493
return lookup(name);
494     }
495
496     /**
497      * Lookup name.
498      * @exception NamingException if there is a naming exception
499      * @exception RemoteException if there is a RMI exception
500      */

501     public Object JavaDoc lookupLink(Name name) throws NamingException {
502         // Flat namespace; no federation; just call string version
503
return lookupLink(name.toString());
504     }
505     
506     /**
507      * List the NameParser specified by name.
508      * @exception NamingException if there is a naming exception
509      * @exception RemoteException if there is a RMI exception
510      */

511     public NameParser getNameParser(String JavaDoc name) throws NamingException {
512         return myParser;
513     }
514
515     /**
516      * List the NameParser specified by name.
517      * @exception NamingException if there is a naming exception
518      * @exception RemoteException if there is a RMI exception
519      */

520     public NameParser getNameParser(Name name) throws NamingException {
521         // Flat namespace; no federation; just call string version
522
return getNameParser(name.toString());
523     }
524
525     /**
526      * Compose a new name specified by name and prefix.
527      * @exception NamingException if there is a naming exception
528      * @exception RemoteException if there is a RMI exception
529      * @return null
530      */

531     public String JavaDoc composeName(String JavaDoc name, String JavaDoc prefix)
532     throws NamingException {
533         return null;
534     }
535
536     /**
537      * Compose a new name specified by name and prefix.
538      * @exception NamingException if there is a naming exception
539      * @exception RemoteException if there is a RMI exception
540      * @return Name result of the concatenation
541      */

542     public Name composeName(Name name, Name prefix)
543     throws NamingException {
544         Name result = (Name)(prefix.clone());
545         result.addAll(name);
546         return result;
547     }
548
549     /**
550      * Add the property name and value to the environment.
551      * @exception NamingException if there is a naming exception
552      * @exception RemoteException if there is a RMI exception
553      */

554     public Object JavaDoc addToEnvironment(String JavaDoc propName, Object JavaDoc propVal)
555     throws NamingException {
556         if (myEnv == null) {
557             myEnv = new Hashtable(5, 0.75f);
558         }
559         return myEnv.put(propName, propVal);
560     }
561
562     /**
563      * Remove property from the environment.
564      * @exception NamingException if there is a naming exception
565      * @exception RemoteException if there is a RMI exception
566      */

567     public Object JavaDoc removeFromEnvironment(String JavaDoc propName)
568     throws NamingException {
569         if (myEnv == null) {
570             return null;
571         }
572         return myEnv.remove(propName);
573     }
574
575     /**
576      * List the current environment.
577      * @exception NamingException if there is a naming exception
578      * @exception RemoteException if there is a RMI exception
579      */

580     public Hashtable getEnvironment() throws NamingException {
581         if (myEnv == null) {
582             // Must return non-null
583
myEnv = new Hashtable(3, 0.75f);
584         }
585         return myEnv;
586     }
587
588     /**
589      * Invalidate the current environment.
590      * @exception NamingException if there is a naming exception
591      * @exception RemoteException if there is a RMI exception
592      */

593     public void close() throws NamingException {
594         myEnv = null;
595     }
596     
597     /**
598      * Operation not supported.
599      */

600     public String JavaDoc getNameInNamespace() throws NamingException {
601         throw new OperationNotSupportedException("getNameInNamespace() " +
602                          "not implemented");
603     }
604
605     /**
606      * Print the current hashtable.
607      */

608     private static void print(Hashtable ht) {
609         for (Enumeration en = ht.keys(); en.hasMoreElements(); ) {
610             Object JavaDoc key = en.nextElement();
611             Object JavaDoc value = ht.get(key);
612               /** IASRI 4660742
613             System.out.println("[" + key + ":" + key.getClass().getName() +
614                                ", " + value + ":" + value.getClass().getName()
615                                + "]");
616               **/

617               // START OF IASRI 4660742
618
if(_logger.isLoggable(Level.FINE)) {
619                  _logger.log(Level.FINE,"[" + key + ":" +
620                         key.getClass().getName() +
621                         ", " + value + ":" + value.getClass().getName() + "]");
622             }
623               // END OF IASRI 4660742
624
}
625     }
626
627     // Class for enumerating name/class pairs
628
class RepNames implements NamingEnumeration {
629         Hashtable bindings;
630         Enumeration names;
631
632         RepNames (Hashtable bindings) {
633             this.bindings = bindings;
634             this.names = bindings.keys();
635         }
636
637         public boolean hasMoreElements() {
638             return names.hasMoreElements();
639         }
640
641         public boolean hasMore() throws NamingException {
642             return hasMoreElements();
643         }
644
645         public Object JavaDoc nextElement() {
646             if(names.hasMoreElements())
647         {
648             String JavaDoc name = (String JavaDoc)names.nextElement();
649             String JavaDoc className = bindings.get(name).getClass().getName();
650             return new NameClassPair(name, className);
651         }
652             else
653                 return null;
654         }
655
656         public Object JavaDoc next() throws NamingException {
657             return nextElement();
658         }
659
660         // New API for JNDI 1.2
661
public void close() throws NamingException {
662             throw new OperationNotSupportedException("close() not implemented");
663         }
664     }
665
666     // Class for enumerating bindings
667
class RepBindings implements NamingEnumeration {
668         Enumeration names;
669         Hashtable bindings;
670
671         RepBindings (Hashtable bindings) {
672             this.bindings = bindings;
673             this.names = bindings.keys();
674         }
675
676         public boolean hasMoreElements() {
677             return names.hasMoreElements();
678         }
679
680         public boolean hasMore() throws NamingException {
681             return hasMoreElements();
682         }
683
684         public Object JavaDoc nextElement() {
685             if(hasMoreElements())
686         {
687             String JavaDoc name = (String JavaDoc)names.nextElement();
688             return new Binding(name, bindings.get(name));
689         }
690             else
691                 return null;
692         }
693         public Object JavaDoc next() throws NamingException {
694             return nextElement();
695         }
696
697         // New API for JNDI 1.2
698
public void close() throws NamingException {
699             throw new
700         OperationNotSupportedException("close() not implemented");
701         }
702     }
703 }
704
705
706
707
708
709
710
711
712
713
714
Popular Tags