KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > servicelocator > ejb > 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
38 package com.sun.j2ee.blueprints.servicelocator.ejb;
39
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
47 import com.sun.j2ee.blueprints.servicelocator.ServiceLocatorException;
48
49 /**
50  * This class is an implementation of the Service Locator pattern. It is
51  * used to looukup resources such as EJBHomes, JMS Destinations, etc.
52  */

53 public class ServiceLocator {
54     
55     private transient InitialContext ic;
56     
57     public ServiceLocator() throws ServiceLocatorException {
58         try {
59             ic = new InitialContext();
60         } catch (Exception JavaDoc e) {
61             throw new ServiceLocatorException(e);
62         }
63     }
64     
65     /**
66      * will get the ejb Local home factory.
67      * clients need to cast to the type of EJBHome they desire
68      *
69      * @return the Local EJB Home corresponding to the homeName
70      */

71     public EJBLocalHome getLocalHome(String JavaDoc jndiHomeName) throws ServiceLocatorException {
72         try {
73             return (EJBLocalHome) ic.lookup(jndiHomeName);
74         } catch (Exception JavaDoc e) {
75             throw new ServiceLocatorException(e);
76         }
77     }
78     
79     /**
80      * will get the ejb Remote home factory.
81      * clients need to cast to the type of EJBHome they desire
82      *
83      * @return the EJB Home corresponding to the homeName
84      */

85     public EJBHome getRemoteHome(String JavaDoc jndiHomeName, Class JavaDoc className) throws ServiceLocatorException {
86         try {
87             Object JavaDoc objref = ic.lookup(jndiHomeName);
88             return (EJBHome) PortableRemoteObject.narrow(objref, className);
89         } catch (Exception JavaDoc e) {
90             throw new ServiceLocatorException(e);
91         }
92     }
93     
94     /**
95      * @return the factory for the factory to get JMS connections from
96      */

97     public ConnectionFactory getJMSConnectionFactory(String JavaDoc jmsConnFactoryName)
98     throws ServiceLocatorException {
99         try {
100             return (ConnectionFactory) ic.lookup(jmsConnFactoryName);
101         } catch (Exception JavaDoc e) {
102             throw new ServiceLocatorException(e);
103         }
104     }
105     
106     /**
107      * @return the JMS Destination to send messages to
108      */

109     public Destination getJMSDestination(String JavaDoc destName) throws ServiceLocatorException {
110         try {
111             return (Destination)ic.lookup(destName);
112         } catch (Exception JavaDoc e) {
113             throw new ServiceLocatorException(e);
114         }
115     }
116         
117     /**
118      * This method obtains the datasource itself for a caller
119      * @return the DataSource corresponding to the name parameter
120      */

121     public DataSource getDataSource(String JavaDoc dataSourceName) throws ServiceLocatorException {
122         try {
123             return (DataSource)ic.lookup(dataSourceName);
124         } catch (Exception JavaDoc e) {
125             throw new ServiceLocatorException(e);
126         }
127     }
128     
129     /**
130      * @return the URL value corresponding
131      * to the env entry name.
132      */

133     public URL getUrl(String JavaDoc envName) throws ServiceLocatorException {
134         try {
135             return (URL)ic.lookup(envName);
136         } catch (Exception JavaDoc e) {
137             throw new ServiceLocatorException(e);
138         }
139     }
140     
141     /**
142      * @return the boolean value corresponding
143      * to the env entry such as SEND_CONFIRMATION_MAIL property.
144      */

145     public boolean getBoolean(String JavaDoc envName) throws ServiceLocatorException {
146         try {
147             return ((Boolean JavaDoc)ic.lookup(envName)).booleanValue();
148         } catch (Exception JavaDoc e) {
149             throw new ServiceLocatorException(e);
150         }
151     }
152     
153     /**
154      * @return the String value corresponding
155      * to the env entry name.
156      */

157     public String JavaDoc getString(String JavaDoc envName) throws ServiceLocatorException {
158         try {
159             return (String JavaDoc)ic.lookup(envName);
160         } catch (Exception JavaDoc e) {
161             throw new ServiceLocatorException(e);
162         }
163     }
164 }
165
Popular Tags