KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > servicelocator > web > ServiceLocator


1 package com.jdon.servicelocator.web;
2
3 import java.net.URL JavaDoc;
4 import java.util.Collections JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import javax.ejb.EJBHome JavaDoc;
9 import javax.ejb.EJBLocalHome JavaDoc;
10 import javax.jms.Queue JavaDoc;
11 import javax.jms.QueueConnectionFactory JavaDoc;
12 import javax.jms.Topic JavaDoc;
13 import javax.jms.TopicConnectionFactory JavaDoc;
14 import javax.naming.InitialContext JavaDoc;
15 import javax.naming.NamingException JavaDoc;
16 import javax.rmi.PortableRemoteObject JavaDoc;
17 import javax.sql.DataSource JavaDoc;
18
19 import com.jdon.container.pico.Startable;
20 import com.jdon.servicelocator.ServiceLocatorException;
21 import com.jdon.util.Debug;
22
23
24 /**
25  * This class is an implementation of the Service Locator pattern. It is
26  * used to looukup resources such as EJBHomes, JMS Destinations, etc.
27  * This implementation uses the "singleton" strategy and also the "caching"
28  * strategy.
29  * This implementation is intended to be used on the web tier and
30  * not on the ejb tier.
31  *
32  * registering with container.xml
33  *
34  */

35 public class ServiceLocator implements Startable{
36   private final static String JavaDoc module = ServiceLocator.class.getName();
37
38   private InitialContext JavaDoc ic;
39   private Map JavaDoc cache; //used to hold references to EJBHomes/JMS Resources for re-use
40

41   public ServiceLocator() {
42     try {
43       ic = new InitialContext JavaDoc();
44       cache = Collections.synchronizedMap(new HashMap JavaDoc());
45     } catch (NamingException JavaDoc ne) {
46       System.err.print("NamingException error: " + ne);
47     } catch (Exception JavaDoc e) {
48       System.err.print(e);
49     }
50   }
51
52   public void start() {
53      Debug.logVerbose("[JdonFramework]ServiceLocator start .....", module);
54      cache.clear();
55    }
56
57    public void stop() {
58      cache.clear();
59    }
60
61
62   /**
63    * will get the ejb Local home factory. If this ejb home factory has already been
64    * clients need to cast to the type of EJBHome they desire
65    *
66    * @return the EJB Home corresponding to the homeName
67    */

68   public EJBLocalHome JavaDoc getLocalHome(String JavaDoc jndiHomeName) throws
69       ServiceLocatorException {
70     Debug.logVerbose("[JdonFramework] -- > getLocalHome.... ", module);
71     EJBLocalHome JavaDoc home = null;
72     try {
73       if (cache.containsKey(jndiHomeName)) {
74         home = (EJBLocalHome JavaDoc) cache.get(jndiHomeName);
75       } else {
76         Debug.logVerbose("[JdonFramework] lookUp LocalHome.... ", module);
77         home = (EJBLocalHome JavaDoc) ic.lookup(jndiHomeName);
78         cache.put(jndiHomeName, home);
79       }
80     } catch (NamingException JavaDoc ne) {
81       throw new ServiceLocatorException(ne);
82     } catch (Exception JavaDoc e) {
83       throw new ServiceLocatorException(e);
84     }
85     return home;
86   }
87
88   /**
89    * will get the ejb Remote home factory. If this ejb home factory has already been
90    * clients need to cast to the type of EJBHome they desire
91    *
92    * @return the EJB Home corresponding to the homeName
93    */

94   public EJBHome JavaDoc getRemoteHome(String JavaDoc jndiHomeName, Class JavaDoc className) throws
95       ServiceLocatorException {
96     EJBHome JavaDoc home = null;
97     try {
98       if (cache.containsKey(jndiHomeName)) {
99         home = (EJBHome JavaDoc) cache.get(jndiHomeName);
100       } else {
101         Object JavaDoc objref = ic.lookup(jndiHomeName);
102         Object JavaDoc obj = PortableRemoteObject.narrow(objref, className);
103         home = (EJBHome JavaDoc) obj;
104         cache.put(jndiHomeName, home);
105       }
106     } catch (NamingException JavaDoc ne) {
107       throw new ServiceLocatorException(ne);
108     } catch (Exception JavaDoc e) {
109       throw new ServiceLocatorException(e);
110     }
111
112     return home;
113   }
114
115   /**
116    * @return the factory for the factory to get queue connections from
117    */

118   public QueueConnectionFactory JavaDoc getQueueConnectionFactory(String JavaDoc
119       qConnFactoryName) throws ServiceLocatorException {
120     QueueConnectionFactory JavaDoc factory = null;
121     try {
122       if (cache.containsKey(qConnFactoryName)) {
123         factory = (QueueConnectionFactory JavaDoc) cache.get(qConnFactoryName);
124       } else {
125         factory = (QueueConnectionFactory JavaDoc) ic.lookup(qConnFactoryName);
126         cache.put(qConnFactoryName, factory);
127       }
128     } catch (NamingException JavaDoc ne) {
129       throw new ServiceLocatorException(ne);
130     } catch (Exception JavaDoc e) {
131       throw new ServiceLocatorException(e);
132     }
133     return factory;
134   }
135
136   /**
137    * @return the Queue Destination to send messages to
138    */

139   public Queue JavaDoc getQueue(String JavaDoc queueName) throws ServiceLocatorException {
140     Queue JavaDoc queue = null;
141     try {
142       if (cache.containsKey(queueName)) {
143         queue = (Queue JavaDoc) cache.get(queueName);
144       } else {
145         queue = (Queue JavaDoc) ic.lookup(queueName);
146         cache.put(queueName, queue);
147       }
148     } catch (NamingException JavaDoc ne) {
149       throw new ServiceLocatorException(ne);
150     } catch (Exception JavaDoc e) {
151       throw new ServiceLocatorException(e);
152     }
153
154     return queue;
155   }
156
157   /**
158    * This method helps in obtaining the topic factory
159    * @return the factory for the factory to get topic connections from
160    */

161   public TopicConnectionFactory JavaDoc getTopicConnectionFactory(String JavaDoc
162       topicConnFactoryName) throws ServiceLocatorException {
163     TopicConnectionFactory JavaDoc factory = null;
164     try {
165       if (cache.containsKey(topicConnFactoryName)) {
166         factory = (TopicConnectionFactory JavaDoc) cache.get(topicConnFactoryName);
167       } else {
168         factory = (TopicConnectionFactory JavaDoc) ic.lookup(topicConnFactoryName);
169         cache.put(topicConnFactoryName, factory);
170       }
171     } catch (NamingException JavaDoc ne) {
172       throw new ServiceLocatorException(ne);
173     } catch (Exception JavaDoc e) {
174       throw new ServiceLocatorException(e);
175     }
176     return factory;
177   }
178
179   /**
180    * This method obtains the topc itself for a caller
181    * @return the Topic Destination to send messages to
182    */

183   public Topic JavaDoc getTopic(String JavaDoc topicName) throws ServiceLocatorException {
184     Topic JavaDoc topic = null;
185     try {
186       if (cache.containsKey(topicName)) {
187         topic = (Topic JavaDoc) cache.get(topicName);
188       } else {
189         topic = (Topic JavaDoc) ic.lookup(topicName);
190         cache.put(topicName, topic);
191       }
192     } catch (NamingException JavaDoc ne) {
193       throw new ServiceLocatorException(ne);
194     } catch (Exception JavaDoc e) {
195       throw new ServiceLocatorException(e);
196     }
197     return topic;
198   }
199
200   /**
201    * This method obtains the datasource itself for a caller
202    * @return the DataSource corresponding to the name parameter
203    */

204   public DataSource JavaDoc getDataSource(String JavaDoc dataSourceName) throws
205       ServiceLocatorException {
206     DataSource JavaDoc dataSource = null;
207     try {
208       if (cache.containsKey(dataSourceName)) {
209         dataSource = (DataSource JavaDoc) cache.get(dataSourceName);
210       } else {
211         dataSource = (DataSource JavaDoc) ic.lookup(dataSourceName);
212         cache.put(dataSourceName, dataSource);
213       }
214     } catch (NamingException JavaDoc ne) {
215       throw new ServiceLocatorException(ne);
216     } catch (Exception JavaDoc e) {
217       throw new ServiceLocatorException(e);
218     }
219     return dataSource;
220   }
221
222   /**
223    * @return the URL value corresponding
224    * to the env entry name.
225    */

226   public URL JavaDoc getUrl(String JavaDoc envName) throws ServiceLocatorException {
227     URL JavaDoc url = null;
228     try {
229       url = (URL JavaDoc) ic.lookup(envName);
230     } catch (NamingException JavaDoc ne) {
231       throw new ServiceLocatorException(ne);
232     } catch (Exception JavaDoc e) {
233       throw new ServiceLocatorException(e);
234     }
235
236     return url;
237   }
238
239   /**
240    * @return the boolean value corresponding
241    * to the env entry such as SEND_CONFIRMATION_MAIL property.
242    */

243   public boolean getBoolean(String JavaDoc envName) throws ServiceLocatorException {
244     Boolean JavaDoc bool = null;
245     try {
246       bool = (Boolean JavaDoc) ic.lookup(envName);
247     } catch (NamingException JavaDoc ne) {
248       throw new ServiceLocatorException(ne);
249     } catch (Exception JavaDoc e) {
250       throw new ServiceLocatorException(e);
251     }
252     return bool.booleanValue();
253   }
254
255   /**
256    * @return the String value corresponding
257    * to the env entry name.
258    */

259   public String JavaDoc getString(String JavaDoc envName) throws ServiceLocatorException {
260     String JavaDoc envEntry = null;
261     try {
262       envEntry = (String JavaDoc) ic.lookup(envName);
263     } catch (NamingException JavaDoc ne) {
264       throw new ServiceLocatorException(ne);
265     } catch (Exception JavaDoc e) {
266       throw new ServiceLocatorException(e);
267     }
268     return envEntry;
269   }
270
271   public Object JavaDoc getDAO(String JavaDoc jndiDAOName) throws ServiceLocatorException {
272
273     Object JavaDoc object = null;
274     try {
275       String JavaDoc className = (String JavaDoc) ic.lookup(jndiDAOName);
276       object = Class.forName(className).newInstance();
277     } catch (NamingException JavaDoc ne) {
278       throw new ServiceLocatorException(ne);
279     } catch (Exception JavaDoc se) {
280       throw new ServiceLocatorException(se);
281     }
282     return object;
283   }
284
285 }
286
Popular Tags