KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > core > ivm > naming > JndiReference


1 package org.openejb.core.ivm.naming;
2
3 import java.util.Hashtable JavaDoc;
4
5 import javax.naming.Context JavaDoc;
6 import javax.naming.InitialContext JavaDoc;
7 import javax.naming.NamingException JavaDoc;
8
9 /**
10  * This class is used when the object to be referenced is accessible through
11  * some other JNDI name space.
12  *
13  * The object is not resolved until it's requested.
14  */

15 public class JndiReference implements Reference{
16     
17     private Context JavaDoc context;
18     private Hashtable JavaDoc envProperties;
19     private String JavaDoc jndiName;
20     private String JavaDoc contextJndiName;
21     /*
22     * This constructor is used when the object to be referenced is accessible through
23     * some other JNDI name space. The context is provided and the lookup name, but the
24     * object is not resolved until it's requested.
25     */

26     public JndiReference(javax.naming.Context JavaDoc linkedContext, String JavaDoc jndiName){
27         this.context = linkedContext;
28         this.jndiName = jndiName;
29     }
30     
31     /*
32     */

33     public JndiReference(String JavaDoc contextJndiName, String JavaDoc jndiName){
34         this.contextJndiName = contextJndiName;
35         this.jndiName = jndiName;
36     }
37     
38     public JndiReference(Hashtable JavaDoc envProperties, String JavaDoc jndiName){
39         if (envProperties == null || envProperties.size() == 0 ) {
40             this.envProperties = null;
41         } else {
42             this.envProperties = envProperties;
43         }
44         this.jndiName = jndiName;
45     }
46     
47     public Object JavaDoc getObject( ) throws NamingException{
48         Context JavaDoc externalContext = getContext();
49         synchronized(externalContext){
50             /* According to the JNDI SPI specification multiple threads may not access the same JNDI
51             Context *instance* concurrently. Since we don't know the origines of the federated context we must
52             synchonrize access to it. JNDI SPI Sepecifiation 1.2 Section 2.2
53             */

54             return externalContext.lookup(jndiName);
55         }
56     }
57
58     protected Context JavaDoc getContext() throws NamingException{
59         if (context == null) {
60             if ( contextJndiName != null ) {
61                 context = (Context JavaDoc)org.openejb.OpenEJB.getJNDIContext().lookup(contextJndiName);
62             } else {
63                 context = new InitialContext JavaDoc(envProperties);
64             }
65         }
66         return context;
67     }
68 }
69
Popular Tags