KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.jdon.servicelocator.ejb;
2
3
4 import java.net.URL JavaDoc;
5
6 import javax.ejb.EJBHome JavaDoc;
7 import javax.ejb.EJBLocalHome JavaDoc;
8 import javax.jms.QueueConnectionFactory JavaDoc;
9 import javax.jms.Queue JavaDoc;
10 import javax.jms.TopicConnectionFactory JavaDoc;
11 import javax.jms.Topic JavaDoc;
12 import javax.naming.InitialContext JavaDoc;
13 import javax.naming.NamingException JavaDoc;
14 import javax.rmi.PortableRemoteObject JavaDoc;
15 import javax.sql.DataSource JavaDoc;
16
17 import com.jdon.servicelocator.ServiceLocatorException;
18 import org.apache.log4j.Logger;
19 /**
20  * This class is an implementation of the Service Locator pattern. It is
21  * used to looukup resources such as EJBHomes, JMS Destinations, etc.
22  */

23 public class ServiceLocator implements java.io.Serializable JavaDoc {
24   private final static Logger logger = Logger.getLogger(ServiceLocator.class);
25   private InitialContext JavaDoc ic;
26
27   private static ServiceLocator me;
28
29   public ServiceLocator() throws ServiceLocatorException {
30     try {
31       ic = new InitialContext JavaDoc();
32     } catch (NamingException JavaDoc ne) {
33       throw new ServiceLocatorException(ne);
34     } catch (Exception JavaDoc e) {
35       throw new ServiceLocatorException(e);
36     }
37   }
38
39   /**
40    * will get the ejb Local home factory.
41    * clients need to cast to the type of EJBHome they desire
42    *
43    * @return the Local EJB Home corresponding to the homeName
44    */

45   public EJBLocalHome JavaDoc getLocalHome(String JavaDoc jndiHomeName) throws
46       ServiceLocatorException {
47     EJBLocalHome JavaDoc home = null;
48     try {
49       home = (EJBLocalHome JavaDoc) ic.lookup(jndiHomeName);
50     } catch (NamingException JavaDoc ne) {
51       throw new ServiceLocatorException(ne);
52     } catch (Exception JavaDoc e) {
53       throw new ServiceLocatorException(e);
54     }
55     return home;
56   }
57
58   /**
59    * will get the ejb Remote home factory.
60    * clients need to cast to the type of EJBHome they desire
61    *
62    * @return the EJB Home corresponding to the homeName
63    */

64   public EJBHome JavaDoc getRemoteHome(String JavaDoc jndiHomeName, Class JavaDoc className) throws
65       ServiceLocatorException {
66     EJBHome JavaDoc home = null;
67     try {
68       Object JavaDoc objref = ic.lookup(jndiHomeName);
69       Object JavaDoc obj = PortableRemoteObject.narrow(objref, className);
70       home = (EJBHome JavaDoc) obj;
71     } catch (NamingException JavaDoc ne) {
72       throw new ServiceLocatorException(ne);
73     } catch (Exception JavaDoc e) {
74       throw new ServiceLocatorException(e);
75     }
76     return home;
77   }
78
79   /**
80    * @return the factory for the factory to get queue connections from
81    */

82   public QueueConnectionFactory JavaDoc getQueueConnectionFactory(String JavaDoc
83       qConnFactoryName) throws ServiceLocatorException {
84     QueueConnectionFactory JavaDoc factory = null;
85     try {
86       factory = (QueueConnectionFactory JavaDoc) ic.lookup(qConnFactoryName);
87     } catch (NamingException JavaDoc ne) {
88       throw new ServiceLocatorException(ne);
89     } catch (Exception JavaDoc e) {
90       throw new ServiceLocatorException(e);
91     }
92     return factory;
93   }
94
95   /**
96    * @return the Queue Destination to send messages to
97    */

98   public Queue JavaDoc getQueue(String JavaDoc queueName) throws ServiceLocatorException {
99     Queue JavaDoc queue = null;
100     try {
101       queue = (Queue JavaDoc) ic.lookup(queueName);
102     } catch (NamingException JavaDoc ne) {
103       throw new ServiceLocatorException(ne);
104     } catch (Exception JavaDoc e) {
105       throw new ServiceLocatorException(e);
106     }
107     return queue;
108   }
109
110   /**
111    * This method helps in obtaining the topic factory
112    * @return the factory for the factory to get topic connections from
113    */

114   public TopicConnectionFactory JavaDoc getTopicConnectionFactory(String JavaDoc
115       topicConnFactoryName) throws ServiceLocatorException {
116     TopicConnectionFactory JavaDoc factory = null;
117     try {
118       factory = (TopicConnectionFactory JavaDoc) ic.lookup(topicConnFactoryName);
119     } catch (NamingException JavaDoc ne) {
120       throw new ServiceLocatorException(ne);
121     } catch (Exception JavaDoc e) {
122       throw new ServiceLocatorException(e);
123     }
124     return factory;
125   }
126
127   /**
128    * This method obtains the topc itself for a caller
129    * @return the Topic Destination to send messages to
130    */

131   public Topic JavaDoc getTopic(String JavaDoc topicName) throws ServiceLocatorException {
132     Topic JavaDoc topic = null;
133     try {
134       topic = (Topic JavaDoc) ic.lookup(topicName);
135     } catch (NamingException JavaDoc ne) {
136       throw new ServiceLocatorException(ne);
137     } catch (Exception JavaDoc e) {
138       throw new ServiceLocatorException(e);
139     }
140     return topic;
141   }
142
143   /**
144    * This method obtains the datasource itself for a caller
145    * @return the DataSource corresponding to the name parameter
146    */

147   public DataSource JavaDoc getDataSource(String JavaDoc dataSourceName) throws
148       ServiceLocatorException {
149     DataSource JavaDoc dataSource = null;
150     try {
151       dataSource = (DataSource JavaDoc) ic.lookup(dataSourceName);
152     } catch (NamingException JavaDoc ne) {
153       throw new ServiceLocatorException(ne);
154     } catch (Exception JavaDoc e) {
155       throw new ServiceLocatorException(e);
156     }
157     return dataSource;
158   }
159
160   /**
161    * @return the URL value corresponding
162    * to the env entry name.
163    */

164   public URL JavaDoc getUrl(String JavaDoc envName) throws ServiceLocatorException {
165     URL JavaDoc url = null;
166     try {
167       url = (URL JavaDoc) ic.lookup(envName);
168     } catch (NamingException JavaDoc ne) {
169       throw new ServiceLocatorException(ne);
170     } catch (Exception JavaDoc e) {
171       throw new ServiceLocatorException(e);
172     }
173
174     return url;
175   }
176
177   /**
178    * @return the boolean value corresponding
179    * to the env entry such as SEND_CONFIRMATION_MAIL property.
180    */

181   public boolean getBoolean(String JavaDoc envName) throws ServiceLocatorException {
182     Boolean JavaDoc bool = null;
183     try {
184       bool = (Boolean JavaDoc) ic.lookup(envName);
185     } catch (NamingException JavaDoc ne) {
186       throw new ServiceLocatorException(ne);
187     } catch (Exception JavaDoc e) {
188       throw new ServiceLocatorException(e);
189     }
190     return bool.booleanValue();
191   }
192
193   /**
194    * @return the String value corresponding
195    * to the env entry name.
196    */

197   public String JavaDoc getString(String JavaDoc envName) throws ServiceLocatorException {
198     String JavaDoc envEntry = null;
199     try {
200       envEntry = (String JavaDoc) ic.lookup(envName);
201     } catch (NamingException JavaDoc ne) {
202       throw new ServiceLocatorException(ne);
203     } catch (Exception JavaDoc e) {
204       throw new ServiceLocatorException(e);
205     }
206     return envEntry;
207   }
208
209   public Object JavaDoc getDAO(String JavaDoc jndiDAOName) throws ServiceLocatorException {
210
211     Object JavaDoc object = null;
212     try {
213       String JavaDoc className = (String JavaDoc) ic.lookup(jndiDAOName);
214       ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
215       object = classLoader.loadClass(className).newInstance();
216     } catch (NamingException JavaDoc ne) {
217       throw new ServiceLocatorException(ne);
218     } catch (Exception JavaDoc se) {
219       throw new ServiceLocatorException(se);
220     }
221     return object;
222   }
223
224 }
225
Popular Tags