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