Clover coverage report - JFox Service-Oriented Application Framework - 1.0-RC1 Coverage timestamp: 星期五 八月 19 2005 13:21:55 CST FRAMES NO FRAMES |
file stats: | LOC: | 369 |  | Methods: | 16
| NCLOC: | 260 |  | Classes: | 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
Source file | Conditionals | Statements | Methods | TOTAL
ServiceLocatorImpl.java | 9.1% | 9.8% | 18.8% | 10.6% |

1 |  |
     /**
2 |  |
      * @(#)ServiceLocatorImpl.java
3 |  |
      *
4 |  |
      * JFoxSOAF, Service-Oriented Application Framework
5 |  |
      *
6 |  |
      * Copyright(c) JFoxSOAF Team
7 |  |
      *
8 |  |
      * Licensed under the GNU LGPL, Version 2.1 (the "License");
9 |  |
      * you may not use this file except in compliance with the License.
10 |  |
      * You may obtain a copy of the License at
11 |  |
      *
12 |  |
      * http://www.gnu.org/copyleft/lesser.html
13 |  |
      *
14 |  |
      * Unless required by applicable law or agreed to in writing, software
15 |  |
      * distributed under the License is distributed on an "AS IS" BASIS,
16 |  |
      * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 |  |
      * See the License for the specific language governing permissions and
18 |  |
      * limitations under the License.
19 |  |
      *
20 |  |
      * For more information, please visit:
21 |  |
      * http://www.jfox.cn/confluence/display/JFoxSOAF/Home
22 |  |
      * http://www.huihoo.org/jfox/jfoxsoaf
23 |  |
      */
24 |  |
25 |  |
     package org.huihoo.jfox.soaf.services.ejb;
26 |  |
27 |  |
     import java.net.URL;
28 |  |
     import java.rmi.Remote;
29 |  |
     import java.util.Collections;
30 |  |
     import java.util.HashMap;
31 |  |
     import java.util.Map;
32 |  |
33 |  |
     import javax.ejb.EJBHome;
34 |  |
     import javax.ejb.EJBLocalHome;
35 |  |
     import javax.jms.ConnectionFactory;
36 |  |
     import javax.jms.Destination;
37 |  |
     import javax.jms.Queue;
38 |  |
     import javax.jms.QueueConnectionFactory;
39 |  |
     import javax.jms.Topic;
40 |  |
     import javax.jms.TopicConnectionFactory;
41 |  |
     import javax.mail.Session;
42 |  |
     import javax.naming.InitialContext;
43 |  |
     import javax.naming.NamingException;
44 |  |
     import javax.rmi.PortableRemoteObject;
45 |  |
     import javax.sql.DataSource;
46 |  |
     import javax.transaction.UserTransaction;
47 |  |
     import javax.xml.rpc.Service;
48 |  |
49 |  |
     import org.apache.commons.logging.Log;
50 |  |
     import org.apache.commons.logging.LogFactory;
51 |  |
     import org.huihoo.jfox.soaf.exception.ServiceLocatorException;
52 |  |
53 |  |
     /**
54 |  |
      * <p>
55 |  |
      * ServiceLocator Implementation.
56 |  |
      * </p>
57 |  |
      *
58 |  |
      * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
59 |  |
      * @version $Revision: 1.14 $ $Date: 2005/05/22 06:49:31 $
60 |  |
      * @version Revision: 1.0
61 |  |
      */
62 |  |
63 |  |
     public class ServiceLocatorImpl implements ServiceLocator {
64 |  |
65 |  |
         private InitialContext ic = null;
66 |  |
67 |  |
         private Map cache = null;
68 |  |
69 |  |
         private final Log logger = LogFactory.getLog(getClass());
70 |  |
71 |  |
         private DataSource dataSource = null;
72 |  |
73 |  |
         private Session mailSession = null;
74 |  |
75 | 6 |
         public ServiceLocatorImpl() throws ServiceLocatorException {
76 | 6 |
             try {
77 | 6 |
                 ic = new InitialContext();
78 | 6 |
                 cache = Collections.synchronizedMap(new HashMap());
79 |  |
             } catch (NamingException ne) {
80 | 0 |
                 logger.error("ServiceService NamingException " + ne.getMessage());
81 | 0 |
                 throw new ServiceLocatorException(ne);
82 |  |
             } catch (Exception e) {
83 | 0 |
                 logger.error("ServiceLocator Exception " + e.getMessage());
84 | 0 |
                 throw new ServiceLocatorException(e);
85 |  |
             }
86 |  |
         }
87 |  |
88 |  |
         /**
89 |  |
          * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getBoolean(java.lang.String)
90 |  |
          */
91 | 0 |
         public boolean getBoolean(String envName) throws ServiceLocatorException {
92 | 0 |
             Boolean bool = null;
93 | 0 |
             try {
94 | 0 |
                 bool = (Boolean) ic.lookup(envName);
95 |  |
             } catch (NamingException ne) {
96 | 0 |
                 throw new ServiceLocatorException(ne);
97 |  |
             } catch (Exception e) {
98 | 0 |
                 throw new ServiceLocatorException(e);
99 |  |
             }
100 | 0 |
             return bool.booleanValue();
101 |  |
         }
102 |  |
103 |  |
         /**
104 |  |
          * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getLocalHome(java.lang.String)
105 |  |
          */
106 | 0 |
         public EJBLocalHome getLocalHome(String jndiHomeName)
107 |  |
                 throws ServiceLocatorException {
108 | 0 |
             EJBLocalHome home = null;
109 | 0 |
             try {
110 | 0 |
                 if (cache.containsKey(jndiHomeName)) {
111 | 0 |
                     home = (EJBLocalHome) cache.get(jndiHomeName);
112 |  |
                 } else {
113 | 0 |
                     home = (EJBLocalHome) ic.lookup(jndiHomeName);
114 | 0 |
                     cache.put(jndiHomeName, home);
115 |  |
                 }
116 |  |
             } catch (NamingException ne) {
117 | 0 |
                 throw new ServiceLocatorException(ne);
118 |  |
             } catch (Exception e) {
119 | 0 |
                 throw new ServiceLocatorException(e);
120 |  |
             }
121 | 0 |
             return home;
122 |  |
         }
123 |  |
124 |  |
         /**
125 |  |
          * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getRemoteHome(java.lang.String,
126 |  |
          *      java.lang.ClassLoadProxy)
127 |  |
          */
128 | 2 |
         public EJBHome getRemoteHome(String jndiHomeName, Class className)
129 |  |
                 throws ServiceLocatorException {
130 | 2 |
             EJBHome home = null;
131 | 2 |
             try {
132 | 2 |
                 if (cache.containsKey(jndiHomeName)) {
133 | 0 |
                     home = (EJBHome) cache.get(jndiHomeName);
134 |  |
                 } else {
135 | 2 |
                     Object objref = ic.lookup(jndiHomeName);
136 | 0 |
                     Object obj = PortableRemoteObject.narrow(objref, className);
137 | 0 |
                     home = (EJBHome) obj;
138 | 0 |
                     cache.put(jndiHomeName, home);
139 |  |
                 }
140 |  |
             } catch (NamingException ne) {
141 | 2 |
                 throw new ServiceLocatorException(ne);
142 |  |
             } catch (Exception e) {
143 | 0 |
                 throw new ServiceLocatorException(e);
144 |  |
             }
145 | 0 |
             return home;
146 |  |
         }
147 |  |
148 |  |
         /**
149 |  |
          * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getQueue(java.lang.String)
150 |  |
          */
151 | 0 |
         public Queue getQueue(String queueName) throws ServiceLocatorException {
152 | 0 |
             Queue queue = null;
153 | 0 |
             try {
154 | 0 |
                 if (cache.containsKey(queueName)) {
155 | 0 |
                     queue = (Queue) cache.get(queueName);
156 |  |
                 } else {
157 | 0 |
                     queue = (Queue) ic.lookup(queueName);
158 | 0 |
                     cache.put(queueName, queue);
159 |  |
                 }
160 |  |
             } catch (NamingException ne) {
161 | 0 |
                 throw new ServiceLocatorException(ne);
162 |  |
             } catch (Exception e) {
163 | 0 |
                 throw new ServiceLocatorException(e);
164 |  |
             }
165 | 0 |
             return queue;
166 |  |
         }
167 |  |
168 |  |
         /**
169 |  |
          * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getQueueConnectionFactory(java.lang.String)
170 |  |
          */
171 | 0 |
         public QueueConnectionFactory getQueueConnectionFactory(
172 |  |
                 String qConnFactoryName) throws ServiceLocatorException {
173 | 0 |
             QueueConnectionFactory factory = null;
174 | 0 |
             try {
175 | 0 |
                 if (cache.containsKey(qConnFactoryName)) {
176 | 0 |
                     factory = (QueueConnectionFactory) cache.get(qConnFactoryName);
177 |  |
                 } else {
178 | 0 |
                     factory = (QueueConnectionFactory) ic.lookup(qConnFactoryName);
179 | 0 |
                     cache.put(qConnFactoryName, factory);
180 |  |
                 }
181 |  |
             } catch (NamingException ne) {
182 | 0 |
                 throw new ServiceLocatorException(ne);
183 |  |
             } catch (Exception e) {
184 | 0 |
                 throw new ServiceLocatorException(e);
185 |  |
             }
186 | 0 |
             return factory;
187 |  |
         }
188 |  |
189 |  |
         /**
190 |  |
          * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getTopic(java.lang.String)
191 |  |
          */
192 | 0 |
         public Topic getTopic(String topicName) throws ServiceLocatorException {
193 | 0 |
             Topic topic = null;
194 | 0 |
             try {
195 | 0 |
                 if (cache.containsKey(topicName)) {
196 | 0 |
                     topic = (Topic) cache.get(topicName);
197 |  |
                 } else {
198 | 0 |
                     topic = (Topic) ic.lookup(topicName);
199 | 0 |
                     cache.put(topicName, topic);
200 |  |
                 }
201 |  |
             } catch (NamingException ne) {
202 | 0 |
                 throw new ServiceLocatorException(ne);
203 |  |
             } catch (Exception e) {
204 | 0 |
                 throw new ServiceLocatorException(e);
205 |  |
             }
206 | 0 |
             return topic;
207 |  |
         }
208 |  |
209 |  |
         /**
210 |  |
          * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getTopicConnectionFactory(java.lang.String)
211 |  |
          */
212 | 0 |
         public TopicConnectionFactory getTopicConnectionFactory(
213 |  |
                 String topicConnFactoryName) throws ServiceLocatorException {
214 | 0 |
             TopicConnectionFactory factory = null;
215 | 0 |
             try {
216 | 0 |
                 if (cache.containsKey(topicConnFactoryName)) {
217 | 0 |
                     factory = (TopicConnectionFactory) cache
218 |  |
                             .get(topicConnFactoryName);
219 |  |
                 } else {
220 | 0 |
                     factory = (TopicConnectionFactory) ic
221 |  |
                             .lookup(topicConnFactoryName);
222 | 0 |
                     cache.put(topicConnFactoryName, factory);
223 |  |
                 }
224 |  |
             } catch (NamingException ne) {
225 | 0 |
                 throw new ServiceLocatorException(ne);
226 |  |
             } catch (Exception e) {
227 | 0 |
                 throw new ServiceLocatorException(e);
228 |  |
             }
229 | 0 |
             return factory;
230 |  |
         }
231 |  |
232 |  |
         /**
233 |  |
          * @return the factory for the factory to get queue connections from
234 |  |
          */
235 | 0 |
         public ConnectionFactory getJMSConnectionFactory(String ConnFactoryName)
236 |  |
                 throws ServiceLocatorException {
237 | 0 |
             ConnectionFactory factory = (ConnectionFactory) cache
238 |  |
                     .get(ConnFactoryName);
239 | 0 |
             if (factory == null) {
240 | 0 |
                 try {
241 | 0 |
                     factory = (ConnectionFactory) ic.lookup(ConnFactoryName);
242 | 0 |
                     cache.put(ConnFactoryName, factory);
243 |  |
                 } catch (Exception e) {
244 | 0 |
                     throw new ServiceLocatorException(e);
245 |  |
                 }
246 |  |
             }
247 | 0 |
             return factory;
248 |  |
         }
249 |  |
250 |  |
         /**
251 |  |
          * @return the Queue Destination to send messages to
252 |  |
          */
253 | 0 |
         public Destination getJMSDestination(String destName)
254 |  |
                 throws ServiceLocatorException {
255 | 0 |
             Destination dest = (Destination) cache.get(destName);
256 | 0 |
             if (dest == null) {
257 | 0 |
                 try {
258 | 0 |
                     dest = (Destination) ic.lookup(destName);
259 | 0 |
                     cache.put(destName, dest);
260 |  |
                 } catch (Exception e) {
261 | 0 |
                     throw new ServiceLocatorException(e);
262 |  |
                 }
263 |  |
             }
264 | 0 |
             return dest;
265 |  |
         }
266 |  |
267 |  |
         /**
268 |  |
          * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getDataSource(String)
269 |  |
          */
270 | 1 |
         public DataSource getDataSource(String dataSourceName)
271 |  |
                 throws ServiceLocatorException {
272 |  |
273 | 1 |
             try {
274 | 1 |
                 if (cache.containsKey(dataSourceName)) {
275 | 0 |
                     dataSource = (DataSource) cache.get(dataSourceName);
276 |  |
                 } else {
277 | 1 |
                     dataSource = (DataSource) ic.lookup(dataSourceName);
278 | 0 |
                     cache.put(dataSourceName, dataSource);
279 |  |
                 }
280 |  |
             } catch (NamingException ne) {
281 | 1 |
                 throw new ServiceLocatorException(ne);
282 |  |
             } catch (Exception e) {
283 | 0 |
                 throw new ServiceLocatorException(e);
284 |  |
             }
285 | 0 |
             return dataSource;
286 |  |
         }
287 |  |
288 |  |
         /**
289 |  |
          * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getDataSource(String)
290 |  |
          */
291 | 0 |
         public Session getMailSession(String mailSessionName)
292 |  |
                 throws ServiceLocatorException {
293 |  |
294 | 0 |
             try {
295 | 0 |
                 if (cache.containsKey(mailSessionName)) {
296 | 0 |
                     mailSession = (Session) cache.get(mailSessionName);
297 |  |
                 } else {
298 | 0 |
                     mailSession = (Session) ic.lookup(mailSessionName);
299 | 0 |
                     cache.put(mailSessionName, mailSession);
300 |  |
                 }
301 |  |
             } catch (NamingException ne) {
302 | 0 |
                 throw new ServiceLocatorException(ne);
303 |  |
             } catch (Exception e) {
304 | 0 |
                 throw new ServiceLocatorException(e);
305 |  |
             }
306 | 0 |
             return mailSession;
307 |  |
         }
308 |  |
309 |  |
         /**
310 |  |
          * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getString(String)
311 |  |
          */
312 | 0 |
         public String getString(String envName) throws ServiceLocatorException {
313 | 0 |
             String envEntry = null;
314 | 0 |
             try {
315 | 0 |
                 envEntry = (String) ic.lookup(envName);
316 |  |
             } catch (NamingException ne) {
317 | 0 |
                 throw new ServiceLocatorException(ne);
318 |  |
             } catch (Exception e) {
319 | 0 |
                 throw new ServiceLocatorException(e);
320 |  |
             }
321 | 0 |
             return envEntry;
322 |  |
         }
323 |  |
324 |  |
         /**
325 |  |
          * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getUserTransaction(String)
326 |  |
          */
327 | 0 |
         public UserTransaction getUserTransaction(String utName)
328 |  |
                 throws ServiceLocatorException {
329 | 0 |
             try {
330 | 0 |
                 return (UserTransaction) ic.lookup(utName);
331 |  |
             } catch (Exception e) {
332 | 0 |
                 throw new ServiceLocatorException(e);
333 |  |
             }
334 |  |
         }
335 |  |
336 |  |
         /**
337 |  |
          * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getUrl(String)
338 |  |
          */
339 | 0 |
         public URL getUrl(String envName) throws ServiceLocatorException {
340 | 0 |
             URL url = null;
341 | 0 |
             try {
342 | 0 |
                 url = (URL) ic.lookup(envName);
343 |  |
             } catch (NamingException ne) {
344 | 0 |
                 throw new ServiceLocatorException(ne);
345 |  |
             } catch (Exception e) {
346 | 0 |
                 throw new ServiceLocatorException(e);
347 |  |
             }
348 | 0 |
             return url;
349 |  |
         }
350 |  |
351 |  |
         /**
352 |  |
          * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getServicePort(String, Class)
353 |  |
          */
354 | 0 |
         public Remote getServicePort(String jndiHomeName, Class className)
355 |  |
                 throws ServiceLocatorException {
356 | 0 |
             Remote servicePort = (Remote) cache.get(jndiHomeName);
357 | 0 |
             if (servicePort == null) {
358 | 0 |
                 try {
359 | 0 |
                     Service service = (Service) ic.lookup(jndiHomeName);
360 | 0 |
                     servicePort = service.getPort(className);
361 | 0 |
                     cache.put(jndiHomeName, servicePort);
362 |  |
                 } catch (Exception e) {
363 | 0 |
                     throw new ServiceLocatorException(e);
364 |  |
                 }
365 |  |
             }
366 | 0 |
             return servicePort;
367 |  |
         }
368 |  |
369 |  |
     }