KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > jndi > ContextImpl


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.jndi;
8
9 import java.io.IOException JavaDoc;
10 import java.io.Serializable JavaDoc;
11 import java.lang.reflect.Proxy JavaDoc;
12 import java.rmi.MarshalledObject JavaDoc;
13 import java.util.Hashtable JavaDoc;
14 import java.util.List JavaDoc;
15 import javax.naming.Binding JavaDoc;
16 import javax.naming.CompoundName JavaDoc;
17 import javax.naming.Context JavaDoc;
18 import javax.naming.ContextNotEmptyException JavaDoc;
19 import javax.naming.InitialContext JavaDoc;
20 import javax.naming.InvalidNameException JavaDoc;
21 import javax.naming.LinkRef JavaDoc;
22 import javax.naming.Name JavaDoc;
23 import javax.naming.NameAlreadyBoundException JavaDoc;
24 import javax.naming.NameClassPair JavaDoc;
25 import javax.naming.NameNotFoundException JavaDoc;
26 import javax.naming.NameParser JavaDoc;
27 import javax.naming.NamingEnumeration JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29 import javax.naming.NotContextException JavaDoc;
30 import javax.naming.OperationNotSupportedException JavaDoc;
31 import javax.naming.Reference JavaDoc;
32 import javax.naming.Referenceable JavaDoc;
33 import javax.naming.directory.Attributes JavaDoc;
34 import javax.naming.directory.DirContext JavaDoc;
35 import javax.naming.directory.InvalidAttributesException JavaDoc;
36 import javax.naming.spi.NamingManager JavaDoc;
37
38 import org.jfox.ioc.connector.ConnectorHelper;
39 import org.jfox.ioc.connector.ConnectorRemote;
40 import org.jfox.ioc.connector.ObjectId;
41 import org.jfox.ioc.util.ObjectUUID;
42 import org.jfox.jndi.enc.EnterpriseContextFactory;
43
44 /**
45  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
46  */

47
48 public class ContextImpl implements Context JavaDoc, Serializable JavaDoc {
49     /**
50      * environment of this Context
51      */

52     private Hashtable JavaDoc env = new Hashtable JavaDoc();
53
54     /**
55      * 该 context 名字空间,如:/ , /test
56      */

57     private Name JavaDoc prefix;
58
59     private NameParserImpl parser = NameParserImpl.getInstance();
60
61     // JNDIContainer
62
private JNDIContainer container;
63
64     public ContextImpl(Name JavaDoc prefix, Hashtable JavaDoc env) throws NamingException JavaDoc {
65         init(prefix, env);
66     }
67
68     public ContextImpl(String JavaDoc prefix, Hashtable JavaDoc env) throws NamingException JavaDoc {
69         Name JavaDoc _prefix = null;
70         if(prefix == null || prefix.trim().length() == 0) {
71             _prefix = parser.parse("/");
72         }
73         else {
74             _prefix = parser.parse(prefix.trim());
75         }
76
77         init(_prefix, env);
78
79     }
80
81     private void init(Name JavaDoc prefix, Hashtable JavaDoc env) throws NamingException JavaDoc {
82         if(env == null) {
83             env = JNDIProperties.getDefaultEnvironment();
84         }
85         this.env = (Hashtable JavaDoc) env.clone();
86
87         String JavaDoc privoderURL = (String JavaDoc) env.get(Context.PROVIDER_URL);
88         ConnectorRemote remote = ConnectorHelper.lookupConnector(privoderURL);
89         container = (JNDIContainer) Proxy.newProxyInstance(this.getClass().getClassLoader(),
90                                                            new Class JavaDoc[]{JNDIContainer.class},
91                                                            new JNDIConnectorInvoker(new ObjectId(ObjectUUID.randomUUID()), remote));
92
93         if(prefix == null || prefix.toString().trim().length() == 0) {
94             this.prefix = parser.parse("/");
95         }
96         else {
97             this.prefix = prefix;
98         }
99
100         // bound root Context as "/";
101
if(this.prefix.toString().equals("/") && !container.isBound(prefix)) {
102             this.bind(prefix, this);
103         }
104
105     }
106
107
108     /**
109      * Retrieves the named object.
110      * If <tt>name</tt> is empty, returns a new instance of this context
111      * (which represents the same naming context as this context, but its
112      * environment may be modified independently and it may be accessed
113      * concurrently).
114      *
115      * @param name the name of the object to look up
116      * @return the object bound to <tt>name</tt>
117      * @throws NamingException if a naming exception is encountered
118      * @see #lookup(String)
119      * @see #lookupLink(Name)
120      */

121     public Object JavaDoc lookup(Name JavaDoc name) throws NamingException JavaDoc {
122         if(name.isEmpty()) { // empty name will return a new context
123
return new ContextImpl(this.getNameInNamespace(), this.getEnvironment());
124         }
125
126         // check enc
127
if(EnterpriseContextFactory.isEnterpriseContext(name)) {
128             Context JavaDoc ctx = (Context JavaDoc) lookup(EnterpriseContextFactory.ENC_JNDI_NAME);
129             return ctx.lookup(EnterpriseContextFactory.getEnterpriseContextSuffix(name));
130         }
131
132         try {
133             Name JavaDoc absoluteName = getAbsoluteName(name);
134             Object JavaDoc obj = container.lookup(absoluteName);
135             Object JavaDoc ret = obj;
136             if(obj instanceof MarshalledObject JavaDoc) {
137                 ret = ((MarshalledObject JavaDoc) obj).get();
138             }
139             else if(obj instanceof Reference JavaDoc) { // common reference
140
// Dereference object
141
if(obj instanceof LinkRef JavaDoc) { // link ref
142
String JavaDoc ref = ((LinkRef JavaDoc) obj).getLinkName();
143                     return new InitialContext JavaDoc(env).lookup(ref);
144                 }
145                 else { // other Reference, must use ObjectFactory.getObjectInstance
146
ret = NamingManager.getObjectInstance(obj, absoluteName, this, env);
147                 }
148             }
149
150             return ret;
151         }
152         catch(NamingException JavaDoc e) {
153             throw e;
154         }
155         catch(Exception JavaDoc ex) {
156             ex.printStackTrace();
157             throw new NamingException JavaDoc(ex.toString());
158         }
159
160     }
161
162     /**
163      * Retrieves the named object.
164      * See {@link #lookup(Name)} for details.
165      *
166      * @param name the name of the object to look up
167      * @return the object bound to <tt>name</tt>
168      * @throws NamingException if a naming exception is encountered
169      */

170     public Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc {
171         return lookup(parser.parse(name));
172     }
173
174     /**
175      * Binds a name to an object.
176      * All intermediate contexts and the target context (that named by all
177      * but terminal atomic component of the name) must already exist.
178      *
179      * @param name the name to bind; may not be empty
180      * @param obj the object to bind; possibly null
181      * @throws NameAlreadyBoundException if name is already bound
182      * @throws InvalidAttributesException if object did not supply all mandatory attributes
183      * @throws NamingException if a naming exception is encountered
184      * @see #bind(String, Object)
185      * @see #rebind(Name, Object)
186      * @see DirContext#bind(Name, Object,
187             * Attributes)
188      */

189     public void bind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
190         if(name.isEmpty()) {
191             throw new InvalidNameException JavaDoc(name.toString());
192         }
193
194         String JavaDoc classname = null;
195         if(obj instanceof Referenceable JavaDoc) { // get the reference first
196
obj = ((Referenceable JavaDoc) obj).getReference();
197         }
198         if(obj instanceof Reference JavaDoc) {
199             classname = ((Reference JavaDoc) obj).getClassName();
200         }
201         else {
202             classname = obj.getClass().getName();
203 // if(!(obj instanceof Remote)){ // not a remote class , let it be
204

205             try {
206                 // if the object is a remote object, MarshalledObject is the stub
207
obj = new MarshalledObject JavaDoc(obj);
208             }
209             catch(IOException JavaDoc e) {
210                 throw new NamingException JavaDoc(e.toString());
211             }
212
213 // }
214
}
215         container.bind(this, getAbsoluteName(name), obj, classname);
216
217     }
218
219     /**
220      * Binds a name to an object.
221      * See {@link #bind(Name, Object)} for details.
222      *
223      * @param name the name to bind; may not be empty
224      * @param obj the object to bind; possibly null
225      * @throws NameAlreadyBoundException if name is already bound
226      * @throws InvalidAttributesException if object did not supply all mandatory attributes
227      * @throws NamingException if a naming exception is encountered
228      */

229     public void bind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
230         bind(parser.parse(name), obj);
231     }
232
233     /**
234      * Binds a name to an object, overwriting any existing binding.
235      * All intermediate contexts and the target context (that named by all
236      * but terminal atomic component of the name) must already exist.
237      * <p/>
238      * <p> If the object is a <tt>DirContext</tt>, any existing attributes
239      * associated with the name are replaced with those of the object.
240      * Otherwise, any existing attributes associated with the name remain
241      * unchanged.
242      *
243      * @param name the name to bind; may not be empty
244      * @param obj the object to bind; possibly null
245      * @throws InvalidAttributesException if object did not supply all mandatory attributes
246      * @throws NamingException if a naming exception is encountered
247      * @see #rebind(String, Object)
248      * @see #bind(Name, Object)
249      * @see DirContext#rebind(Name, Object,
250             * Attributes)
251      * @see DirContext
252      */

253     public void rebind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
254         if(name.isEmpty() || name.toString().trim().equals("/")) { // Empty names are not allowed
255
throw new InvalidNameException JavaDoc(name.toString());
256         }
257
258         String JavaDoc classname = null;
259         if(obj instanceof Referenceable JavaDoc) { // get the reference first
260
obj = ((Referenceable JavaDoc) obj).getReference();
261         }
262         if(obj instanceof Reference JavaDoc) {
263             classname = ((Reference JavaDoc) obj).getClassName();
264         }
265         else {
266             classname = obj.getClass().getName();
267             try {
268                 if(!(obj instanceof MarshalledObject JavaDoc)) {
269                     obj = new MarshalledObject JavaDoc(obj);
270                 }
271             }
272             catch(IOException JavaDoc e) {
273                 throw new NamingException JavaDoc(e.toString());
274             }
275         }
276
277         try {
278 // System.out.println("server.rebind: " + getAbsoluteName(name));
279
container.rebind(this, getAbsoluteName(name), obj, classname);
280         }
281         catch(Exception JavaDoc e) {
282             e.printStackTrace();
283             throw new NamingException JavaDoc(e.toString());
284         }
285
286     }
287
288     /**
289      * Binds a name to an object, overwriting any existing binding.
290      * See {@link #rebind(Name, Object)} for details.
291      *
292      * @param name the name to bind; may not be empty
293      * @param obj the object to bind; possibly null
294      * @throws InvalidAttributesException if object did not supply all mandatory attributes
295      * @throws NamingException if a naming exception is encountered
296      */

297     public void rebind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
298         rebind(parser.parse(name), obj);
299     }
300
301     /**
302      * Unbinds the named object.
303      * Removes the terminal atomic name in <code>name</code>
304      * from the target context--that named by all but the terminal
305      * atomic part of <code>name</code>.
306      * <p/>
307      * <p> This method is idempotent.
308      * It succeeds even if the terminal atomic name
309      * is not bound in the target context, but throws
310      * <tt>NameNotFoundException</tt>
311      * if any of the intermediate contexts do not exist.
312      * <p/>
313      * <p> Any attributes associated with the name are removed.
314      * Intermediate contexts are not changed.
315      *
316      * @param name the name to unbind; may not be empty
317      * @throws NameNotFoundException if an intermediate context does not exist
318      * @throws NamingException if a naming exception is encountered
319      * @see #unbind(String)
320      */

321     public void unbind(Name JavaDoc name) throws NamingException JavaDoc {
322         if(name.isEmpty() || name.toString().trim().equals("/")) {
323             throw new InvalidNameException JavaDoc(name.toString());
324         }
325         try {
326             Name JavaDoc absoluteName = getAbsoluteName(name);
327             container.unbind(this, absoluteName);
328         }
329         catch(Exception JavaDoc ex) {
330             throw new NamingException JavaDoc(ex.toString());
331         }
332
333     }
334
335     /**
336      * Unbinds the named object.
337      * See {@link #unbind(Name)} for details.
338      *
339      * @param name the name to unbind; may not be empty
340      * @throws NameNotFoundException if an intermediate context does not exist
341      * @throws NamingException if a naming exception is encountered
342      */

343     public void unbind(String JavaDoc name) throws NamingException JavaDoc {
344         unbind(parser.parse(name));
345     }
346
347     /**
348      * Binds a new name to the object bound to an old name, and unbinds
349      * the old name. Both names are relative to this context.
350      * Any attributes associated with the old name become associated
351      * with the new name.
352      * Intermediate contexts of the old name are not changed.
353      *
354      * @param oldName the name of the existing binding; may not be empty
355      * @param newName the name of the new binding; may not be empty
356      * @throws NameAlreadyBoundException if <tt>newName</tt> is already bound
357      * @throws NamingException if a naming exception is encountered
358      * @see #rename(String, String)
359      * @see #bind(Name, Object)
360      * @see #rebind(Name, Object)
361      */

362     public void rename(Name JavaDoc oldName, Name JavaDoc newName) throws NamingException JavaDoc {
363         if(oldName.isEmpty() || oldName.toString().trim().equals("/") ||
364            newName.isEmpty() || newName.toString().trim().equals("/")) {
365             throw new InvalidNameException JavaDoc("oldName: " + oldName.toString() + ", newName: " + newName);
366         }
367         try {
368             Object JavaDoc obj = container.lookup(oldName);
369             bind(newName, obj);
370         }
371         catch(Exception JavaDoc e) {
372             throw new NamingException JavaDoc(e.toString());
373         }
374
375         unbind(oldName);
376     }
377
378     /**
379      * Binds a new name to the object bound to an old name, and unbinds
380      * the old name.
381      * See {@link #rename(Name, Name)} for details.
382      *
383      * @param oldName the name of the existing binding; may not be empty
384      * @param newName the name of the new binding; may not be empty
385      * @throws NameAlreadyBoundException if <tt>newName</tt> is already bound
386      * @throws NamingException if a naming exception is encountered
387      */

388     public void rename(String JavaDoc oldName, String JavaDoc newName) throws NamingException JavaDoc {
389         rename(parser.parse(oldName), parser.parse(newName));
390     }
391
392     /**
393      * Enumerates the names bound in the named context, along with the
394      * class names of objects bound to them.
395      * The contents of any subcontexts are not included.
396      * <p/>
397      * <p> If a binding is added to or removed from this context,
398      * its effect on an enumeration previously returned is undefined.
399      *
400      * @param name the name of the context to list
401      * @return an enumeration of the names and class names of the
402      * bindings in this context. Each element of the
403      * enumeration is of type <tt>NameClassPair</tt>.
404      * @throws NamingException if a naming exception is encountered
405      * @see #list(String)
406      * @see #listBindings(Name)
407      * @see NameClassPair
408      */

409     public NamingEnumeration JavaDoc list(Name JavaDoc name) throws NamingException JavaDoc {
410         try {
411             List JavaDoc list = container.list(this, getAbsoluteName(name));
412             return new NamingEnumerationImpl(list);
413         }
414         catch(Exception JavaDoc ex) {
415             throw new NamingException JavaDoc(ex.toString());
416         }
417     }
418
419     /**
420      * Enumerates the names bound in the named context, along with the
421      * class names of objects bound to them.
422      * See {@link #list(Name)} for details.
423      *
424      * @param name the name of the context to list
425      * @return an enumeration of the names and class names of the
426      * bindings in this context. Each element of the
427      * enumeration is of type <tt>NameClassPair</tt>.
428      * @throws NamingException if a naming exception is encountered
429      */

430     public NamingEnumeration JavaDoc list(String JavaDoc name) throws NamingException JavaDoc {
431         return list(parser.parse(name));
432     }
433
434     /**
435      * Enumerates the names bound in the named context, along with the
436      * objects bound to them.
437      * The contents of any subcontexts are not included.
438      * <p/>
439      * <p> If a binding is added to or removed from this context,
440      * its effect on an enumeration previously returned is undefined.
441      *
442      * @param name the name of the context to list
443      * @return an enumeration of the bindings in this context.
444      * Each element of the enumeration is of type
445      * <tt>Binding</tt>.
446      * @throws NamingException if a naming exception is encountered
447      * @see #listBindings(String)
448      * @see #list(Name)
449      * @see Binding
450      */

451     public NamingEnumeration JavaDoc listBindings(Name JavaDoc name) throws NamingException JavaDoc {
452         try {
453             List JavaDoc list = container.listBindings(this, getAbsoluteName(name));
454             return new NamingEnumerationImpl(list);
455         }
456         catch(Exception JavaDoc ex) {
457             throw new NamingException JavaDoc(ex.toString());
458         }
459
460     }
461
462     /**
463      * Enumerates the names bound in the named context, along with the
464      * objects bound to them.
465      * See {@link #listBindings(Name)} for details.
466      *
467      * @param name the name of the context to list
468      * @return an enumeration of the bindings in this context.
469      * Each element of the enumeration is of type
470      * <tt>Binding</tt>.
471      * @throws NamingException if a naming exception is encountered
472      */

473     public NamingEnumeration JavaDoc listBindings(String JavaDoc name) throws NamingException JavaDoc {
474         return listBindings(parser.parse(name));
475     }
476
477     /**
478      * Destroys the named context and removes it from the namespace.
479      * Any attributes associated with the name are also removed.
480      * Intermediate contexts are not destroyed.
481      * <p/>
482      * <p> This method is idempotent.
483      * It succeeds even if the terminal atomic name
484      * is not bound in the target context, but throws
485      * <tt>NameNotFoundException</tt>
486      * if any of the intermediate contexts do not exist.
487      * <p/>
488      * <p> In a federated naming system, a context from one naming system
489      * may be bound to a name in another. One can subsequently
490      * look up and perform operations on the foreign context using a
491      * composite name. However, an attempt destroy the context using
492      * this composite name will fail with
493      * <tt>NotContextException</tt>, because the foreign context is not
494      * a "subcontext" of the context in which it is bound.
495      * Instead, use <tt>unbind()</tt> to remove the
496      * binding of the foreign context. Destroying the foreign context
497      * requires that the <tt>destroySubcontext()</tt> be performed
498      * on a context from the foreign context's "native" naming system.
499      *
500      * @param name the name of the context to be destroyed; may not be empty
501      * @throws NameNotFoundException if an intermediate context does not exist
502      * @throws NotContextException if the name is bound but does not name a
503      * context, or does not name a context of the appropriate type
504      * @throws ContextNotEmptyException if the named context is not empty
505      * @throws NamingException if a naming exception is encountered
506      * @see #destroySubcontext(String)
507      */

508     public void destroySubcontext(Name JavaDoc name) throws NamingException JavaDoc {
509         if(name.isEmpty() || name.toString().trim().equals("/")) { // Empty names are not allowed
510
throw new InvalidNameException JavaDoc(name.toString());
511         }
512         try {
513             container.destroySubcontext(this, getAbsoluteName(name));
514         }
515         catch(Exception JavaDoc e) {
516             throw new NamingException JavaDoc(e.toString());
517         }
518
519     }
520
521     /**
522      * Destroys the named context and removes it from the namespace.
523      * See {@link #destroySubcontext(Name)} for details.
524      *
525      * @param name the name of the context to be destroyed; may not be empty
526      * @throws NameNotFoundException if an intermediate context does not exist
527      * @throws NotContextException if the name is bound but does not name a
528      * context, or does not name a context of the appropriate type
529      * @throws ContextNotEmptyException if the named context is not empty
530      * @throws NamingException if a naming exception is encountered
531      */

532     public void destroySubcontext(String JavaDoc name) throws NamingException JavaDoc {
533         destroySubcontext(parser.parse(name));
534     }
535
536     /**
537      * Creates and binds a new context.
538      * Creates a new context with the given name and binds it in
539      * the target context (that named by all but terminal atomic
540      * component of the name). All intermediate contexts and the
541      * target context must already exist.
542      *
543      * @param name the name of the context to create; may not be empty
544      * @return the newly created context
545      * @throws NameAlreadyBoundException if name is already bound
546      * @throws InvalidAttributesException if creation of the subcontext requires specification of
547      * mandatory attributes
548      * @throws NamingException if a naming exception is encountered
549      * @see #createSubcontext(String)
550      * @see DirContext#createSubcontext
551      */

552     public Context JavaDoc createSubcontext(Name JavaDoc name) throws NamingException JavaDoc {
553         if(name.isEmpty() || name.toString().trim().equals("/")) {
554             throw new InvalidNameException JavaDoc(name.toString());
555         }
556         try {
557             name = getAbsoluteName(name);
558             return container.createSubcontext(this, name);
559         }
560         catch(Exception JavaDoc ex) {
561             throw new NamingException JavaDoc(ex.toString());
562         }
563
564     }
565
566     /**
567      * Creates and binds a new context.
568      * See {@link #createSubcontext(Name)} for details.
569      *
570      * @param name the name of the context to create; may not be empty
571      * @return the newly created context
572      * @throws NameAlreadyBoundException if name is already bound
573      * @throws InvalidAttributesException if creation of the subcontext requires specification of
574      * mandatory attributes
575      * @throws NamingException if a naming exception is encountered
576      */

577     public Context JavaDoc createSubcontext(String JavaDoc name) throws NamingException JavaDoc {
578         return createSubcontext(parser.parse(name));
579     }
580
581     /**
582      * Retrieves the named object, following links except
583      * for the terminal atomic component of the name.
584      * If the object bound to <tt>name</tt> is not a link,
585      * returns the object itself.
586      *
587      * @param name the name of the object to look up
588      * @return the object bound to <tt>name</tt>, not following the
589      * terminal link (if any).
590      * @throws NamingException if a naming exception is encountered
591      * @see #lookupLink(String)
592      */

593     public Object JavaDoc lookupLink(Name JavaDoc name) throws NamingException JavaDoc {
594         return lookup(name);
595     }
596
597     /**
598      * Retrieves the named object, following links except
599      * for the terminal atomic component of the name.
600      * See {@link #lookupLink(Name)} for details.
601      *
602      * @param name the name of the object to look up
603      * @return the object bound to <tt>name</tt>, not following the
604      * terminal link (if any)
605      * @throws NamingException if a naming exception is encountered
606      */

607     public Object JavaDoc lookupLink(String JavaDoc name) throws NamingException JavaDoc {
608         return lookupLink(parser.parse(name));
609     }
610
611     /**
612      * Retrieves the parser associated with the named context.
613      * In a federation of namespaces, different naming systems will
614      * parse names differently. This method allows an application
615      * to get a parser for parsing names into their atomic components
616      * using the naming convention of a particular naming system.
617      * Within any single naming system, <tt>NameParser</tt> objects
618      * returned by this method must be equal (using the <tt>equals()</tt>
619      * test).
620      *
621      * @param name the name of the context from which to get the parser
622      * @return a name parser that can parse compound names into their atomic
623      * components
624      * @throws NamingException if a naming exception is encountered
625      * @see #getNameParser(String)
626      * @see CompoundName
627      */

628     public NameParser JavaDoc getNameParser(Name JavaDoc name) throws NamingException JavaDoc {
629         return parser;
630     }
631
632     /**
633      * Retrieves the parser associated with the named context.
634      * See {@link #getNameParser(Name)} for details.
635      *
636      * @param name the name of the context from which to get the parser
637      * @return a name parser that can parse compound names into their atomic
638      * components
639      * @throws NamingException if a naming exception is encountered
640      */

641     public NameParser JavaDoc getNameParser(String JavaDoc name) throws NamingException JavaDoc {
642         return parser;
643     }
644
645     /**
646      * Composes the name of this context with a name relative to
647      * this context.
648      * Given a name (<code>name</code>) relative to this context, and
649      * the name (<code>prefix</code>) of this context relative to one
650      * of its ancestors, this method returns the composition of the
651      * two names using the syntax appropriate for the naming
652      * system(s) involved. That is, if <code>name</code> names an
653      * object relative to this context, the result is the name of the
654      * same object, but relative to the ancestor context. None of the
655      * names may be null.
656      * <p/>
657      * For example, if this context is named "wiz.com" relative
658      * to the initial context, then
659      * <pre>
660      * composeName("east", "wiz.com") </pre>
661      * might return <code>"east.wiz.com"</code>.
662      * If instead this context is named "org/research", then
663      * <pre>
664      * composeName("user/jane", "org/research") </pre>
665      * might return <code>"org/research/user/jane"</code> while
666      * <pre>
667      * composeName("user/jane", "research") </pre>
668      * returns <code>"research/user/jane"</code>.
669      *
670      * @param name a name relative to this context
671      * @param prefix the name of this context relative to one of its ancestors
672      * @return the composition of <code>prefix</code> and <code>name</code>
673      * @throws NamingException if a naming exception is encountered
674      * @see #composeName(String, String)
675      */

676     public Name JavaDoc composeName(Name JavaDoc name, Name JavaDoc prefix) throws NamingException JavaDoc {
677         Name JavaDoc newName = (Name JavaDoc) (prefix.clone());
678         newName.addAll(name);
679         return newName;
680     }
681
682     /**
683      * Composes the name of this context with a name relative to
684      * this context.
685      * See {@link #composeName(Name, Name)} for details.
686      *
687      * @param name a name relative to this context
688      * @param prefix the name of this context relative to one of its ancestors
689      * @return the composition of <code>prefix</code> and <code>name</code>
690      * @throws NamingException if a naming exception is encountered
691      */

692     public String JavaDoc composeName(String JavaDoc name, String JavaDoc prefix) throws NamingException JavaDoc {
693         Name JavaDoc result = composeName(parser.parse(name), parser.parse(prefix));
694         return result.toString();
695     }
696
697     /**
698      * Adds a new environment property to the environment of this
699      * context. If the property already exists, its value is overwritten.
700      * See class description for more details on environment properties.
701      *
702      * @param propName the name of the environment property to add; may not be null
703      * @param propVal the value of the property to add; may not be null
704      * @return the previous value of the property, or null if the property was
705      * not in the environment before
706      * @throws NamingException if a naming exception is encountered
707      * @see #getEnvironment()
708      * @see #removeFromEnvironment(String)
709      */

710     public Object JavaDoc addToEnvironment(String JavaDoc propName, Object JavaDoc propVal)
711             throws NamingException JavaDoc {
712
713         return env.put(propName, propVal);
714     }
715
716     /**
717      * Removes an environment property from the environment of this
718      * context. See class description for more details on environment
719      * properties.
720      *
721      * @param propName the name of the environment property to remove; may not be null
722      * @return the previous value of the property, or null if the property was
723      * not in the environment
724      * @throws NamingException if a naming exception is encountered
725      * @see #getEnvironment()
726      * @see #addToEnvironment(String, Object)
727      */

728     public Object JavaDoc removeFromEnvironment(String JavaDoc propName)
729             throws NamingException JavaDoc {
730         return env.remove(propName);
731     }
732
733     /**
734      * Retrieves the environment in effect for this context.
735      * See class description for more details on environment properties.
736      * <p/>
737      * <p> The caller should not make any changes to the object returned:
738      * their effect on the context is undefined.
739      * The environment of this context may be changed using
740      * <tt>addToEnvironment()</tt> and <tt>removeFromEnvironment()</tt>.
741      *
742      * @return the environment of this context; never null
743      * @throws NamingException if a naming exception is encountered
744      * @see #addToEnvironment(String, Object)
745      * @see #removeFromEnvironment(String)
746      */

747     public Hashtable JavaDoc getEnvironment() throws NamingException JavaDoc {
748         return JNDIProperties.cloneEnvironment(env);
749     }
750
751     /**
752      * Closes this context.
753      * This method releases this context's resources immediately, instead of
754      * waiting for them to be released automatically by the garbage collector.
755      * <p/>
756      * <p> This method is idempotent: invoking it on a context that has
757      * already been closed has no effect. Invoking any other method
758      * on a closed context is not allowed, and results in undefined behaviour.
759      *
760      * @throws NamingException if a naming exception is encountered
761      */

762     public void close() throws NamingException JavaDoc {
763         try {
764             container.closeSubcontext(this, prefix);
765         }
766         catch(Exception JavaDoc e) {
767             throw new NamingException JavaDoc(e.toString());
768         }
769     }
770
771     /**
772      * Retrieves the full name of this context within its own namespace.
773      * <p/>
774      * <p> Many naming services have a notion of a "full name" for objects
775      * in their respective namespaces. For example, an LDAP entry has
776      * a distinguished name, and a DNS record has a fully qualified name.
777      * This method allows the client application to retrieve this name.
778      * The string returned by this method is not a JNDI composite name
779      * and should not be passed directly to context methods.
780      * In naming systems for which the notion of full name does not
781      * make sense, <tt>OperationNotSupportedException</tt> is thrown.
782      *
783      * @return this context's name in its own namespace; never null
784      * @throws OperationNotSupportedException if the naming system does
785      * not have the notion of a full name
786      * @throws NamingException if a naming exception is encountered
787      * @since 1.3
788      */

789     public String JavaDoc getNameInNamespace() throws NamingException JavaDoc {
790         return prefix.toString();
791     }
792
793     private Name JavaDoc getAbsoluteName(Name JavaDoc name) throws NamingException JavaDoc {
794         // 在 context 初始化的时候,把 / 绑定到 context
795
if(name.startsWith(prefix)) return name;
796
797         // like java://localhost:1099/a/b/c
798
if(name.toString().indexOf("://") > 0) {
799             name = name.getSuffix(3);
800         }
801         // like java:/a/b/c
802
else if(name.toString().indexOf(":/") > 0) {
803             name = name.getSuffix(1);
804         }
805
806         return composeName(name, prefix);
807     }
808
809 }
Popular Tags