View Javadoc

1   /*** 
2    * JFoxSOAF, Service-Oriented Application Framework
3    * 
4    * Copyright (C) www.huihoo.org
5    *
6    * Distributable under GNU LGPL
7    * 
8    * For more information, please visit: http://www.huihoo.org/jfox/jfoxsoaf
9    */
10  
11  package org.huihoo.jfox.soaf.services.ejb;
12  
13  import java.net.URL;
14  import java.util.Collections;
15  import java.util.HashMap;
16  import java.util.Map;
17  
18  import javax.ejb.EJBHome;
19  import javax.ejb.EJBLocalHome;
20  import javax.jms.Queue;
21  import javax.jms.QueueConnectionFactory;
22  import javax.jms.Topic;
23  import javax.jms.TopicConnectionFactory;
24  import javax.naming.InitialContext;
25  import javax.naming.NamingException;
26  import javax.rmi.PortableRemoteObject;
27  import javax.sql.DataSource;
28  
29  import org.huihoo.jfox.soaf.exception.ServiceLocatorException;
30  import org.huihoo.jfox.soaf.services.logging.LoggingService;
31  
32  /***
33   * <p>
34   * ServiceLocator Implementation.
35   * </p>
36   * 
37   * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
38   * @version $Revision: 1.3 $ $Date: 2004/10/13 11:00:06 $
39   * @version Revision: 1.0
40   */
41  
42  public class ServiceLocatorImpl implements ServiceLocator {
43  
44      private InitialContext ic;
45  
46      private Map cache;
47  
48      private LoggingService logger;
49  
50      private static ServiceLocatorImpl serviceLocatorImpl;
51  
52      static {
53          try {
54              serviceLocatorImpl = new ServiceLocatorImpl();
55          } catch (ServiceLocatorException se) {
56              se.printStackTrace(System.err);
57          }
58      }
59  
60      private ServiceLocatorImpl() throws ServiceLocatorException {
61          try {
62              ic = new InitialContext();
63              cache = Collections.synchronizedMap(new HashMap());
64          } catch (NamingException ne) {
65              logger.error("ServiceService NamingException " + ne.getMessage());
66              throw new ServiceLocatorException(ne);
67          } catch (Exception e) {
68              logger.error("ServiceLocator Exception " + e.getMessage());
69              throw new ServiceLocatorException(e);
70          }
71      }
72  
73      /***
74       * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getBoolean(java.lang.String)
75       */
76      public boolean getBoolean(String envName) throws ServiceLocatorException {
77          Boolean bool = null;
78          try {
79              bool = (Boolean) ic.lookup(envName);
80          } catch (NamingException ne) {
81              throw new ServiceLocatorException(ne);
82          } catch (Exception e) {
83              throw new ServiceLocatorException(e);
84          }
85          return bool.booleanValue();
86      }
87  
88      /***
89       * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getDataSource(java.lang.String)
90       */
91      public DataSource getDataSource(String dataSourceName)
92              throws ServiceLocatorException {
93          DataSource dataSource = null;
94          try {
95              if (cache.containsKey(dataSourceName)) {
96                  dataSource = (DataSource) cache.get(dataSourceName);
97              } else {
98                  dataSource = (DataSource) ic.lookup(dataSourceName);
99                  cache.put(dataSourceName, dataSource);
100             }
101         } catch (NamingException ne) {
102             throw new ServiceLocatorException(ne);
103         } catch (Exception e) {
104             throw new ServiceLocatorException(e);
105         }
106         return dataSource;
107     }
108 
109     /***
110      * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getLocalHome(java.lang.String)
111      */
112     public EJBLocalHome getLocalHome(String jndiHomeName)
113             throws ServiceLocatorException {
114         EJBLocalHome home = null;
115         try {
116             if (cache.containsKey(jndiHomeName)) {
117                 home = (EJBLocalHome) cache.get(jndiHomeName);
118             } else {
119                 home = (EJBLocalHome) ic.lookup(jndiHomeName);
120                 cache.put(jndiHomeName, home);
121             }
122         } catch (NamingException ne) {
123             throw new ServiceLocatorException(ne);
124         } catch (Exception e) {
125             throw new ServiceLocatorException(e);
126         }
127         return home;
128     }
129 
130     /***
131      * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getQueue(java.lang.String)
132      */
133     public Queue getQueue(String queueName) throws ServiceLocatorException {
134         Queue queue = null;
135         try {
136             if (cache.containsKey(queueName)) {
137                 queue = (Queue) cache.get(queueName);
138             } else {
139                 queue = (Queue) ic.lookup(queueName);
140                 cache.put(queueName, queue);
141             }
142         } catch (NamingException ne) {
143             throw new ServiceLocatorException(ne);
144         } catch (Exception e) {
145             throw new ServiceLocatorException(e);
146         }
147         return queue;
148     }
149 
150     /***
151      * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getQueueConnectionFactory(java.lang.String)
152      */
153     public QueueConnectionFactory getQueueConnectionFactory(
154             String qConnFactoryName) throws ServiceLocatorException {
155         QueueConnectionFactory factory = null;
156         try {
157             if (cache.containsKey(qConnFactoryName)) {
158                 factory = (QueueConnectionFactory) cache.get(qConnFactoryName);
159             } else {
160                 factory = (QueueConnectionFactory) ic.lookup(qConnFactoryName);
161                 cache.put(qConnFactoryName, factory);
162             }
163         } catch (NamingException ne) {
164             throw new ServiceLocatorException(ne);
165         } catch (Exception e) {
166             throw new ServiceLocatorException(e);
167         }
168         return factory;
169     }
170 
171     /***
172      * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getRemoteHome(java.lang.String,
173      *      java.lang.ClassLoadProxy)
174      */
175     public EJBHome getRemoteHome(String jndiHomeName, Class className)
176             throws ServiceLocatorException {
177         EJBHome home = null;
178         try {
179             if (cache.containsKey(jndiHomeName)) {
180                 home = (EJBHome) cache.get(jndiHomeName);
181             } else {
182                 Object objref = ic.lookup(jndiHomeName);
183                 Object obj = PortableRemoteObject.narrow(objref, className);
184                 home = (EJBHome) obj;
185                 cache.put(jndiHomeName, home);
186             }
187         } catch (NamingException ne) {
188             throw new ServiceLocatorException(ne);
189         } catch (Exception e) {
190             throw new ServiceLocatorException(e);
191         }
192         return home;
193     }
194 
195     /***
196      * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getString(java.lang.String)
197      */
198     public String getString(String envName) throws ServiceLocatorException {
199         String envEntry = null;
200         try {
201             envEntry = (String) ic.lookup(envName);
202         } catch (NamingException ne) {
203             throw new ServiceLocatorException(ne);
204         } catch (Exception e) {
205             throw new ServiceLocatorException(e);
206         }
207         return envEntry;
208     }
209 
210     /***
211      * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getTopic(java.lang.String)
212      */
213     public Topic getTopic(String topicName) throws ServiceLocatorException {
214         Topic topic = null;
215         try {
216             if (cache.containsKey(topicName)) {
217                 topic = (Topic) cache.get(topicName);
218             } else {
219                 topic = (Topic) ic.lookup(topicName);
220                 cache.put(topicName, topic);
221             }
222         } catch (NamingException ne) {
223             throw new ServiceLocatorException(ne);
224         } catch (Exception e) {
225             throw new ServiceLocatorException(e);
226         }
227         return topic;
228     }
229 
230     /***
231      * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getTopicConnectionFactory(java.lang.String)
232      */
233     public TopicConnectionFactory getTopicConnectionFactory(
234             String topicConnFactoryName) throws ServiceLocatorException {
235         TopicConnectionFactory factory = null;
236         try {
237             if (cache.containsKey(topicConnFactoryName)) {
238                 factory = (TopicConnectionFactory) cache
239                         .get(topicConnFactoryName);
240             } else {
241                 factory = (TopicConnectionFactory) ic
242                         .lookup(topicConnFactoryName);
243                 cache.put(topicConnFactoryName, factory);
244             }
245         } catch (NamingException ne) {
246             throw new ServiceLocatorException(ne);
247         } catch (Exception e) {
248             throw new ServiceLocatorException(e);
249         }
250         return factory;
251     }
252 
253     /***
254      * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getUrl(java.lang.String)
255      */
256     public URL getUrl(String envName) throws ServiceLocatorException {
257         URL url = null;
258         try {
259             url = (URL) ic.lookup(envName);
260         } catch (NamingException ne) {
261             throw new ServiceLocatorException(ne);
262         } catch (Exception e) {
263             throw new ServiceLocatorException(e);
264         }
265         return url;
266     }
267 }