KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > naming > java > javaURLContext


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.java;
24
25 import java.util.*;
26 import java.io.*;
27 import javax.naming.*;
28 import com.sun.enterprise.Switch;
29 import com.sun.enterprise.naming.*;
30 import com.sun.enterprise.util.ORBManager;
31 import com.sun.enterprise.distributedtx.UserTransactionImpl;
32 import com.sun.enterprise.distributedtx.TransactionSynchronizationRegistryImpl;
33
34 //START OF IASRI 4660742
35
import java.util.logging.*;
36 import com.sun.logging.*;
37 //END OF IASRI 4660742
38

39
40 /**
41  * This class is a context implementation for the java:comp namespace.
42  * The context determines the component id from the invocation manager
43  * of the component that is invoking the method and then looks up the
44  * object in that component's local namespace.
45  */

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

53     // Global objects in component namespace
54
private static final String JavaDoc ORB_STRING = "java:comp/ORB";
55     private static final String JavaDoc HANDLE_DELEGATE = "java:comp/HandleDelegate";
56     private static final String JavaDoc USER_TX = "java:comp/UserTransaction";
57     private static final String JavaDoc EJB_CONTEXT = "java:comp/EJBContext";
58     private static final String JavaDoc EJB_TIMER_SERVICE = "java:comp/TimerService";
59     private static final String JavaDoc TRANSACTION_SYNC_REGISTRY =
60                                 "java:comp/TransactionSynchronizationRegistry";
61     private static final String JavaDoc TRANSACTION_MGR = "java:pm/TransactionManager";
62     public static final String JavaDoc APPSERVER_TRANSACTION_MGR = "java:appserver/TransactionManager";
63
64
65     private static final boolean debug = false;
66
67     private NamingManagerImpl namingManager;
68     private Hashtable myEnv;
69     //private Context ctx; XXX not needed ?
70
private String JavaDoc myName="";
71
72     private SerialContext serialContext = null;
73     
74     /**
75      * Create a context with the specified environment.
76      */

77     public javaURLContext(Hashtable environment)
78     throws NamingException
79     {
80         myEnv = (environment != null) ? (Hashtable)(environment.clone()) : null;
81     if (namingManager == null ) {
82       namingManager = (NamingManagerImpl)
83         Switch.getSwitch().getNamingManager();
84     }
85     }
86
87     /**
88      * Create a context with the specified name+environment.
89      * Called only from NamingManagerImpl.
90      */

91     public javaURLContext(String JavaDoc name, Hashtable env)
92     throws NamingException
93     {
94     this(env);
95     this.myName = name;
96     }
97
98     /**
99      * this constructor is called from SerialContext class
100     */

101     public javaURLContext(Hashtable env, SerialContext serialContext)
102     throws NamingException
103     {
104     this(env);
105     this.serialContext = serialContext;
106     }
107   
108   /**
109    * add SerialContext to preserve stickiness to cloned instance
110    * why clone() : to avoid the case of multiple threads modifying
111    * the context returned for ctx.lookup("java:com/env/ejb")
112    */

113     public javaURLContext addStickyContext(SerialContext serialContext)
114       throws NamingException {
115     try {
116         javaURLContext jCtx = (javaURLContext)this.clone();
117         jCtx.serialContext = serialContext;
118         return jCtx;
119     } catch (java.lang.CloneNotSupportedException JavaDoc ex) {
120          NamingException ne = new NamingException("problem with cloning javaURLContext instance");
121          ne.initCause(ex);
122          throw ne;
123     }
124     }
125
126     /**
127      * Lookup an object in the serial context.
128      * @return the object that is being looked up.
129      * @exception NamingException if there is a naming exception.
130      */

131     public Object JavaDoc lookup(String JavaDoc name) throws NamingException {
132         if (_logger.isLoggable(Level.FINE))
133         _logger.log(Level.FINE,"In javaURLContext.lookup, name = "+name + " serialcontext..." + serialContext);
134                 
135     if ( name.equals("") ) {
136         /** javadocs for Context.lookup:
137          * If name is empty, returns a new instance of this context
138          * (which represents the same naming context as this context,
139          * but its environment may be modified independently and it may
140          * be accessed concurrently).
141          */

142         return new javaURLContext(myName, myEnv);
143     }
144
145     String JavaDoc fullName = name;
146     if ( !myName.equals("") ) {
147         if ( myName.equals("java:") )
148         fullName = myName + name;
149         else
150         fullName = myName + "/" + name;
151     }
152
153     try {
154         if ( fullName.startsWith("java:comp/env") ) {
155         // name is in component specific namespace
156
return namingManager.lookup(fullName, serialContext);
157         }
158         else {
159         // name is for a global object
160
if( fullName.equals(ORB_STRING) ) {
161             // return the singleton ORB instance
162
return ORBManager.getORB();
163         }
164         else if ( fullName.equals(USER_TX) ) {
165             // UserTransactionImpl is mutable so return new instance
166
return new UserTransactionImpl();
167         }
168         else if ( fullName.equals(EJB_TIMER_SERVICE) ) {
169             // return the EJB Timer Service. Only works for ejbs.
170
return Switch.getSwitch().getContainerFactory().
171                         getEJBContextObject("javax.ejb.TimerService");
172         } else if ( fullName.equals(EJB_CONTEXT) ) {
173                     // return the EJB Timer Service. Only works for ejbs.
174
return Switch.getSwitch().getContainerFactory().
175                         getEJBContextObject("javax.ejb.EJBContext");
176         } else if ( fullName.equals(HANDLE_DELEGATE) ) {
177             return Switch.getSwitch().getHandleDelegate();
178         }
179         else if ( fullName.equals(TRANSACTION_MGR) ) {
180             return Switch.getSwitch().getContainerFactory().getTransactionMgr();
181         }
182         else if ( fullName.equals(APPSERVER_TRANSACTION_MGR) ) {
183             return com.sun.enterprise.transaction.TransactionManagerHelper.getTransactionManager();
184         }
185         else if ( fullName.equals(TRANSACTION_SYNC_REGISTRY) ) {
186             return TransactionSynchronizationRegistryImpl.getInstance();
187         }
188         else {
189             // try NamingManager
190
return namingManager.lookup(fullName, serialContext);
191         }
192         }
193     } catch ( NamingException ex ) {
194         throw ex;
195     } catch ( Exception JavaDoc ex ) {
196         throw (NamingException)(new NameNotFoundException("No object bound for "+fullName)).initCause(ex);
197     }
198     }
199
200     /**
201      * Lookup a name in either the cosnaming or serial context.
202      * @return the object that is being looked up.
203      * @exception NamingException if there is a naming exception.
204      */

205     public Object JavaDoc lookup(Name name) throws NamingException {
206         // Flat namespace; no federation; just call string version
207
return lookup(name.toString());
208     }
209
210     /**
211      * Bind an object in the namespace. Binds the reference to the
212      * actual object in either the cosnaming or serial context.
213      * @exception NamingException if there is a naming exception.
214      */

215     public void bind(String JavaDoc name, Object JavaDoc obj) throws NamingException {
216     throw new NamingException("java:comp namespace cannot be modified");
217     }
218
219     /**
220      * Bind an object in the namespace. Binds the reference to the
221      * actual object in either the cosnaming or serial context.
222      * @exception NamingException if there is a naming exception.
223      */

224     public void bind(Name name, Object JavaDoc obj) throws NamingException {
225     throw new NamingException("java:comp namespace cannot be modified");
226     }
227
228     /**
229      * Rebind an object in the namespace. Rebinds the reference to the
230      * actual object in either the cosnaming or serial context.
231      * @exception NamingException if there is a naming exception.
232      */

233     public void rebind(String JavaDoc name, Object JavaDoc obj) throws NamingException {
234     throw new NamingException("java:comp namespace cannot be modified");
235     }
236
237     /**
238      * Rebind an object in the namespace. Rebinds the reference to the
239      * actual object in either the cosnaming or serial context.
240      * @exception NamingException if there is a naming exception.
241      */

242     public void rebind(Name name, Object JavaDoc obj) throws NamingException {
243     throw new NamingException("java:comp namespace cannot be modified");
244     }
245
246     /**
247      * Unbind an object from the namespace.
248      * @exception NamingException if there is a naming exception.
249      */

250     public void unbind(String JavaDoc name) throws NamingException {
251     throw new NamingException("java:comp namespace cannot be modified");
252     }
253
254     /**
255      * Unbind an object from the namespace.
256      * @exception NamingException if there is a naming exception.
257      */

258     public void unbind(Name name) throws NamingException {
259     throw new NamingException("java:comp namespace cannot be modified");
260     }
261
262     /**
263      * The rename operation is not supported by this context. It throws
264      * an OperationNotSupportedException.
265      */

266     public void rename(String JavaDoc oldname, String JavaDoc newname) throws NamingException {
267     throw new NamingException("java:comp namespace cannot be modified");
268     }
269
270     /**
271      * The rename operation is not supported by this context. It throws
272      * an OperationNotSupportedException.
273      */

274     public void rename(Name oldname, Name newname)
275             throws NamingException {
276     throw new NamingException("java:comp namespace cannot be modified");
277     }
278
279     /**
280      * The destroySubcontext operation is not supported by this context.
281      * It throws an OperationNotSupportedException.
282      */

283     public void destroySubcontext(String JavaDoc name) throws NamingException {
284     throw new NamingException("java:comp namespace cannot be modified");
285     }
286
287     /**
288      * The destroySubcontext operation is not supported by this context.
289      * It throws an OperationNotSupportedException.
290      */

291     public void destroySubcontext(Name name) throws NamingException {
292     throw new NamingException("java:comp namespace cannot be modified");
293     }
294
295     public Context createSubcontext(String JavaDoc name) throws NamingException {
296     throw new NamingException("java:comp namespace cannot be modified");
297     }
298
299     public Context createSubcontext(Name name) throws NamingException {
300     throw new NamingException("java:comp namespace cannot be modified");
301     }
302
303
304     /**
305      * Lists the contents of a context or subcontext. The operation is
306      * delegated to the serial context.
307      * @return an enumeration of the contents of the context.
308      * @exception NamingException if there is a naming exception.
309      */

310     public NamingEnumeration list(String JavaDoc name)
311             throws NamingException {
312         if (name.equals("")) {
313             // listing this context
314
if ( namingManager == null )
315         throw new NamingException();
316         return namingManager.list(myName);
317         }
318
319         // Check if 'name' names a context
320
Object JavaDoc target = lookup(name);
321         if (target instanceof Context) {
322             return ((Context)target).list("");
323         }
324         throw new NotContextException(name + " cannot be listed");
325     }
326
327     /**
328      * Lists the contents of a context or subcontext. The operation is
329      * delegated to the serial context.
330      * @return an enumeration of the contents of the context.
331      * @exception NamingException if there is a naming exception.
332      */

333     public NamingEnumeration list(Name name)
334             throws NamingException {
335         // Flat namespace; no federation; just call string version
336
return list(name.toString());
337     }
338
339     /**
340      * Lists the bindings of a context or subcontext. The operation is
341      * delegated to the serial context.
342      * @return an enumeration of the bindings of the context.
343      * @exception NamingException if there is a naming exception.
344      */

345     public NamingEnumeration listBindings(String JavaDoc name)
346             throws NamingException {
347         if (name.equals("")) {
348             // listing this context
349
if ( namingManager == null )
350         throw new NamingException();
351         return namingManager.listBindings(myName);
352         }
353
354         // Perhaps 'name' names a context
355
Object JavaDoc target = lookup(name);
356         if (target instanceof Context) {
357             return ((Context)target).listBindings("");
358         }
359         throw new NotContextException(name + " cannot be listed");
360     }
361
362     /**
363      * Lists the bindings of a context or subcontext. The operation is
364      * delegated to the serial context.
365      * @return an enumeration of the bindings of the context.
366      * @exception NamingException if there is a naming exception.
367      */

368     public NamingEnumeration listBindings(Name name)
369             throws NamingException {
370         // Flat namespace; no federation; just call string version
371
return listBindings(name.toString());
372     }
373
374     /**
375      * This context does not treat links specially. A lookup operation is
376      * performed.
377      */

378     public Object JavaDoc lookupLink(String JavaDoc name) throws NamingException {
379         // This flat context does not treat links specially
380
return lookup(name);
381     }
382
383     /**
384      * This context does not treat links specially. A lookup operation is
385      * performed.
386      */

387     public Object JavaDoc lookupLink(Name name) throws NamingException {
388         // Flat namespace; no federation; just call string version
389
return lookupLink(name.toString());
390     }
391
392     /**
393      * Return the name parser for the specified name.
394      * @return the NameParser instance.
395      * @exception NamingException if there is an exception.
396      */

397     public NameParser getNameParser(String JavaDoc name)
398             throws NamingException {
399     if ( namingManager == null )
400         throw new NamingException();
401         return namingManager.getNameParser();
402     }
403
404     /**
405      * Return the name parser for the specified name.
406      * @return the NameParser instance.
407      * @exception NamingException if there is an exception.
408      */

409     public NameParser getNameParser(Name name) throws NamingException {
410         // Flat namespace; no federation; just call string version
411
return getNameParser(name.toString());
412     }
413
414     public String JavaDoc composeName(String JavaDoc name, String JavaDoc prefix)
415             throws NamingException {
416         Name result = composeName(new CompositeName(name),
417                                   new CompositeName(prefix));
418         return result.toString();
419     }
420
421     public Name composeName(Name name, Name prefix)
422             throws NamingException {
423         Name result = (Name)(prefix.clone());
424         result.addAll(name);
425         return result;
426     }
427
428     /**
429      * Add a property to the environment.
430      */

431     public Object JavaDoc addToEnvironment(String JavaDoc propName, Object JavaDoc propVal)
432             throws NamingException {
433         if (myEnv == null) {
434             myEnv = new Hashtable(5, 0.75f);
435         }
436         return myEnv.put(propName, propVal);
437     }
438
439     /**
440      * Remove a property from the environment.
441      */

442     public Object JavaDoc removeFromEnvironment(String JavaDoc propName)
443         throws NamingException
444     {
445         if (myEnv == null) {
446             return null;
447         }
448         return myEnv.remove(propName);
449     }
450
451     /**
452      * Get the context's environment.
453      */

454     public Hashtable getEnvironment() throws NamingException {
455         if (myEnv == null) {
456             // Must return non-null
457
myEnv = new Hashtable(3, 0.75f);
458         }
459         return myEnv;
460     }
461
462     /**
463      * New JNDI 1.2 operation.
464      */

465     public void close() throws NamingException {
466         myEnv = null;
467     }
468
469     /**
470      * Return the name of this context within the namespace. The name
471      * can be passed as an argument to (new InitialContext()).lookup()
472      * to reproduce this context.
473      */

474     public String JavaDoc getNameInNamespace() throws NamingException {
475         return myName;
476     }
477 }
478
479
480
Popular Tags