KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mix > ServiceLocator


1 /*
2  * ServiceLocator.java
3  *
4  * Created on 12. leden 2006, 17:53
5  */

6 package mix;
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 import javax.mail.Session JavaDoc;
22
23 /**
24  *
25  * @author jungi
26  * @version
27  */

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

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

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

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

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

102     public DataSource JavaDoc getDataSource(String JavaDoc dataSourceName) throws NamingException JavaDoc {
103         return (DataSource JavaDoc) lookup(dataSourceName);
104     }
105     
106     /**
107      * This method obtains the mail session
108      * @return the Session corresponding to the name parameter
109      */

110     public Session JavaDoc getSession(String JavaDoc sessionName) throws NamingException JavaDoc {
111         return (Session JavaDoc) lookup(sessionName);
112     }
113     
114     /**
115      * @return the URL value corresponding
116      * to the env entry name.
117      */

118     public URL JavaDoc getUrl(String JavaDoc envName) throws NamingException JavaDoc {
119         return (URL JavaDoc) lookup(envName);
120     }
121     
122     /**
123      * @return the boolean value corresponding
124      * to the env entry such as SEND_CONFIRMATION_MAIL property.
125      */

126     public boolean getBoolean(String JavaDoc envName) throws NamingException JavaDoc {
127         Boolean JavaDoc bool = (Boolean JavaDoc) lookup(envName);
128         return bool.booleanValue();
129     }
130     
131     /**
132      * @return the String value corresponding
133      * to the env entry name.
134      */

135     public String JavaDoc getString(String JavaDoc envName) throws NamingException JavaDoc {
136         return (String JavaDoc) lookup(envName);
137     }
138 }
139
140
Popular Tags