KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > servicelocator > web > ServiceLocator


1 /*
2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistribution in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any
21 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
22 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
24 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
25 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
26 * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
27 * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
28 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
29 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
30 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
31 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * You acknowledge that Software is not designed, licensed or intended
34 * for use in the design, construction, operation or maintenance of
35 * any nuclear facility.
36 */

37 package com.sun.j2ee.blueprints.servicelocator.web;
38
39 import java.util.*;
40 import java.net.*;
41 import javax.ejb.*;
42 import javax.jms.*;
43 import javax.naming.*;
44 import javax.rmi.*;
45 import javax.sql.*;
46 import javax.transaction.*;
47
48 import com.sun.j2ee.blueprints.servicelocator.ServiceLocatorException;
49
50 /**
51  * This class is an implementation of the Service Locator pattern. It is
52  * used to looukup resources such as EJBHomes, JMS Destinations, etc.
53  * This implementation uses the "singleton" strategy and also the "caching"
54  * strategy.
55  * This implementation is intended to be used on the web tier and
56  * not on the ejb tier.
57  */

58 public class ServiceLocator {
59     
60     private InitialContext ic;
61     //used to hold references to EJBHomes/JMS Resources for re-use
62
private Map cache = Collections.synchronizedMap(new HashMap());
63     
64     private static ServiceLocator instance = new ServiceLocator();
65     
66     public static ServiceLocator getInstance() {
67         return instance;
68     }
69     
70     private ServiceLocator() throws ServiceLocatorException {
71         try {
72             ic = new InitialContext();
73         } catch (Exception JavaDoc e) {
74             throw new ServiceLocatorException(e);
75         }
76     }
77     
78     /**
79      * will get the ejb Local home factory. If this ejb home factory has already been
80      * clients need to cast to the type of EJBHome they desire
81      *
82      * @return the EJB Home corresponding to the homeName
83      */

84     public EJBLocalHome getLocalHome(String JavaDoc jndiHomeName) throws ServiceLocatorException {
85         EJBLocalHome home = (EJBLocalHome) cache.get(jndiHomeName);
86         if (home == null) {
87             try {
88                 home = (EJBLocalHome) ic.lookup(jndiHomeName);
89                 cache.put(jndiHomeName, home);
90             } catch (Exception JavaDoc e) {
91                 throw new ServiceLocatorException(e);
92             }
93         }
94         return home;
95     }
96     
97     /**
98      * will get the ejb Remote home factory. If this ejb home factory has already been
99      * clients need to cast to the type of EJBHome they desire
100      *
101      * @return the EJB Home corresponding to the homeName
102      */

103     public EJBHome getRemoteHome(String JavaDoc jndiHomeName, Class JavaDoc className) throws ServiceLocatorException {
104         EJBHome home = (EJBHome) cache.get(jndiHomeName);
105         if (home == null) {
106             try {
107                 Object JavaDoc objref = ic.lookup(jndiHomeName);
108                 Object JavaDoc obj = PortableRemoteObject.narrow(objref, className);
109                 home = (EJBHome)obj;
110                 cache.put(jndiHomeName, home);
111             } catch (Exception JavaDoc e) {
112                 throw new ServiceLocatorException(e);
113             }
114         }
115         return home;
116     }
117     
118     /**
119      * @return the factory for the factory to get queue connections from
120      */

121     public ConnectionFactory getJMSConnectionFactory(String JavaDoc jmsConnFactoryName)
122     throws ServiceLocatorException {
123         ConnectionFactory factory = (ConnectionFactory) cache.get(jmsConnFactoryName);
124         if (factory == null) {
125             try {
126                 factory = (ConnectionFactory) ic.lookup(jmsConnFactoryName);
127                 cache.put(jmsConnFactoryName, factory);
128             } catch (Exception JavaDoc e) {
129                 throw new ServiceLocatorException(e);
130             }
131         }
132         return factory;
133     }
134     
135     /**
136      * @return the Queue Destination to send messages to
137      */

138     public javax.jms.Destination JavaDoc getJMSDestination(String JavaDoc destName)
139         throws ServiceLocatorException {
140         javax.jms.Destination JavaDoc dest = (javax.jms.Destination JavaDoc) cache.get(destName);
141         if (dest == null) {
142             try {
143                 dest =(javax.jms.Destination JavaDoc)ic.lookup(destName);
144                 cache.put(destName, dest);
145             } catch (Exception JavaDoc e) {
146                 throw new ServiceLocatorException(e);
147             }
148         }
149         return dest;
150     }
151     
152     /**
153      * This method obtains the datasource itself for a caller
154      * @return the DataSource corresponding to the name parameter
155      */

156     public DataSource getDataSource(String JavaDoc dataSourceName) throws ServiceLocatorException {
157         DataSource dataSource = (DataSource) cache.get(dataSourceName);
158         if (dataSource == null) {
159             try {
160                 dataSource = (DataSource)ic.lookup(dataSourceName);
161                 cache.put(dataSourceName, dataSource );
162             } catch (Exception JavaDoc e) {
163                 throw new ServiceLocatorException(e);
164             }
165         }
166         return dataSource;
167     }
168     
169     /**
170      * This method obtains the UserTransaction itself for a caller
171      * @return the UserTransaction corresponding to the name parameter
172      */

173     public UserTransaction getUserTransaction(String JavaDoc utName) throws ServiceLocatorException {
174         try {
175             return (UserTransaction) ic.lookup(utName);
176         } catch (Exception JavaDoc e) {
177             throw new ServiceLocatorException(e);
178         }
179     }
180     
181     
182     /**
183      * @return the URL value corresponding
184      * to the env entry name.
185      */

186     public URL getUrl(String JavaDoc envName) throws ServiceLocatorException {
187         try {
188             return (URL)ic.lookup(envName);
189         } catch (Exception JavaDoc e) {
190             throw new ServiceLocatorException(e);
191         }
192     }
193     
194     /**
195      * @return the boolean value corresponding
196      * to the env entry such as SEND_CONFIRMATION_MAIL property.
197      */

198     public boolean getBoolean(String JavaDoc envName) throws ServiceLocatorException {
199         try {
200             return ((Boolean JavaDoc)ic.lookup(envName)).booleanValue();
201         } catch (Exception JavaDoc e) {
202             throw new ServiceLocatorException(e);
203         }
204     }
205     
206     /**
207      * @return the String value corresponding
208      * to the env entry name.
209      */

210     public String JavaDoc getString(String JavaDoc envName) throws ServiceLocatorException {
211         try {
212             return (String JavaDoc)ic.lookup(envName);
213         } catch (Exception JavaDoc e) {
214             throw new ServiceLocatorException(e);
215         }
216     }
217 }
218
Popular Tags