KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > league > ejb > CachingServiceLocator


1 /*
2  * CachingServiceLocator.java
3  *
4  * Created on March 9, 2005, 1:43 PM
5  */

6 package league.ejb;
7
8 import java.util.HashMap JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Collections JavaDoc;
11 import java.net.URL JavaDoc;
12
13 import javax.ejb.EJBHome JavaDoc;
14 import javax.ejb.EJBLocalHome JavaDoc;
15 import javax.jms.ConnectionFactory JavaDoc;
16 import javax.jms.Destination JavaDoc;
17 import javax.naming.InitialContext JavaDoc;
18 import javax.naming.NamingException JavaDoc;
19 import javax.rmi.PortableRemoteObject JavaDoc;
20 import javax.sql.DataSource JavaDoc;
21
22 /**
23  *
24  * @author honza
25  * @version
26  */

27 public class CachingServiceLocator {
28     
29     private InitialContext JavaDoc ic;
30     private Map JavaDoc cache;
31     
32     private static CachingServiceLocator me;
33     
34     static {
35         try {
36             me = new CachingServiceLocator();
37         } catch(NamingException JavaDoc se) {
38             throw new RuntimeException JavaDoc(se);
39         }
40     }
41     
42     private CachingServiceLocator() throws NamingException JavaDoc {
43         ic = new InitialContext JavaDoc();
44         cache = Collections.synchronizedMap(new HashMap JavaDoc());
45     }
46     
47     public static CachingServiceLocator getInstance() {
48         return me;
49     }
50     
51     private Object JavaDoc lookup(String JavaDoc jndiName) throws NamingException JavaDoc {
52         Object JavaDoc cachedObj = cache.get(jndiName);
53         if (cachedObj == null) {
54             cachedObj = ic.lookup(jndiName);
55             cache.put(jndiName, cachedObj);
56         }
57         return cachedObj;
58     }
59     
60     /**
61      * will get the ejb Local home factory. If this ejb home factory has already been
62      * clients need to cast to the type of EJBHome they desire
63      *
64      * @return the EJB Home corresponding to the homeName
65      */

66     public EJBLocalHome JavaDoc getLocalHome(String JavaDoc jndiHomeName) throws NamingException JavaDoc {
67         return (EJBLocalHome JavaDoc) lookup(jndiHomeName);
68     }
69     
70     /**
71      * will get the ejb Remote home factory. If this ejb home factory has already been
72      * clients need to cast to the type of EJBHome they desire
73      *
74      * @return the EJB Home corresponding to the homeName
75      */

76     public EJBHome JavaDoc getRemoteHome(String JavaDoc jndiHomeName, Class JavaDoc className) throws NamingException JavaDoc {
77         Object JavaDoc objref = lookup(jndiHomeName);
78         return (EJBHome JavaDoc) PortableRemoteObject.narrow(objref, className);
79     }
80     
81     /**
82      * This method helps in obtaining the topic factory
83      * @return the factory for the factory to get topic connections from
84      */

85     public ConnectionFactory JavaDoc getConnectionFactory(String JavaDoc connFactoryName) throws NamingException JavaDoc {
86         return (ConnectionFactory JavaDoc) lookup(connFactoryName);
87     }
88     
89     /**
90      * This method obtains the topc itself for a caller
91      * @return the Topic Destination to send messages to
92      */

93     public Destination JavaDoc getDestination(String JavaDoc destName) throws NamingException JavaDoc {
94         return (Destination JavaDoc) lookup(destName);
95     }
96     
97     /**
98      * This method obtains the datasource
99      * @return the DataSource corresponding to the name parameter
100      */

101     public DataSource JavaDoc getDataSource(String JavaDoc dataSourceName) throws NamingException JavaDoc {
102         return (DataSource JavaDoc) lookup(dataSourceName);
103     }
104     
105     /**
106      * @return the URL value corresponding
107      * to the env entry name.
108      */

109     public URL JavaDoc getUrl(String JavaDoc envName) throws NamingException JavaDoc {
110         return (URL JavaDoc) lookup(envName);
111     }
112     
113     /**
114      * @return the boolean value corresponding
115      * to the env entry such as SEND_CONFIRMATION_MAIL property.
116      */

117     public boolean getBoolean(String JavaDoc envName) throws NamingException JavaDoc {
118         Boolean JavaDoc bool = (Boolean JavaDoc) lookup(envName);
119         return bool.booleanValue();
120     }
121     
122     /**
123      * @return the String value corresponding
124      * to the env entry name.
125      */

126     public String JavaDoc getString(String JavaDoc envName) throws NamingException JavaDoc {
127         return (String JavaDoc) lookup(envName);
128     }
129 }
130
131
Popular Tags