KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > ejb > ServiceLocator


1 package org.ejbca.core.ejb;
2
3 import java.net.URL JavaDoc;
4 import java.util.Map JavaDoc;
5 import java.util.Collections JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import javax.naming.InitialContext JavaDoc;
8 import javax.naming.NameNotFoundException JavaDoc;
9 import javax.naming.NamingException JavaDoc;
10 import javax.naming.Context JavaDoc;
11 import javax.rmi.PortableRemoteObject JavaDoc;
12 import javax.sql.DataSource JavaDoc;
13 import javax.ejb.EJBHome JavaDoc;
14 import javax.ejb.EJBLocalHome JavaDoc;
15 import javax.mail.Session JavaDoc;
16
17 /**
18  * A simple implementation of the ServiceLocator/HomeFactory J2EE Pattern.
19  * {@link http://developer.java.sun.com/developer/restricted/patterns/ServiceLocator.html}
20  *
21  * It is used to look up JNDI related resources such as EJB homes, datasources, ...
22  * @version $Id: ServiceLocator.java,v 1.2 2006/10/02 07:54:37 anatom Exp $
23  */

24 public class ServiceLocator {
25
26     /** ejb home cache */
27     private transient Map JavaDoc ejbHomes = Collections.synchronizedMap(new HashMap JavaDoc());
28
29     /** the jndi context */
30     private transient Context JavaDoc ctx;
31
32     /** the singleton instance */
33     private static transient ServiceLocator instance;
34
35     /**
36      * Create a new service locator object.
37      * @throws ServiceLocatorException if the context failed to be initialized
38      */

39     private ServiceLocator() throws ServiceLocatorException {
40         try {
41             this.ctx = new InitialContext JavaDoc();
42         } catch (NamingException JavaDoc e){
43             throw new ServiceLocatorException(e);
44         }
45     }
46
47     /**
48      * return the singleton instance
49      * @return the singleton instance
50      * @throws ServiceLocatorException if the instance could not be initialized the first time
51      */

52     public static final ServiceLocator getInstance() throws ServiceLocatorException {
53         // synchronization is intentionally left out. It 'should' not have dramatic
54
// consequences as it is not that destructive.
55
if (instance == null){
56             instance = new ServiceLocator();
57         }
58         return instance;
59     }
60
61     /**
62      * return the ejb local home.
63      * clients need to cast to the type of EJBHome they desire
64      * @param jndiHomeName the jndi home name matching the requested local home.
65      * @return the Local EJB Home corresponding to the home name
66      */

67     public EJBLocalHome JavaDoc getLocalHome(String JavaDoc jndiHomeName) throws ServiceLocatorException {
68         EJBLocalHome JavaDoc home = (EJBLocalHome JavaDoc)ejbHomes.get(jndiHomeName);
69         if (home == null) {
70             home = (EJBLocalHome JavaDoc)getObject(jndiHomeName);
71             ejbHomes.put(jndiHomeName, home);
72         }
73         return home;
74     }
75
76     /**
77      * return the ejb remote home.
78      * clients need to cast to the type of EJBHome they desire
79      * @param jndiHomeName the jndi home name matching the requested remote home.
80      * @return the Local EJB Home corresponding to the home name
81      */

82     public EJBHome JavaDoc getRemoteHome(String JavaDoc jndiHomeName, Class JavaDoc className) throws ServiceLocatorException {
83         EJBHome JavaDoc home = (EJBHome JavaDoc)ejbHomes.get(className);
84         if (home == null) {
85             Object JavaDoc objref = getObject(jndiHomeName);
86             home = (EJBHome JavaDoc) PortableRemoteObject.narrow(objref, className);
87             ejbHomes.put(className, home);
88         }
89         return home;
90     }
91
92     /**
93      * return the datasource object corresponding the the env entry name
94      * @return the DataSource corresponding to the env entry name parameter
95      * @throws ServiceLocatorException if the lookup fails
96      */

97     public DataSource JavaDoc getDataSource(String JavaDoc dataSourceName) throws ServiceLocatorException {
98         return (DataSource JavaDoc)getObject(dataSourceName);
99     }
100
101     /**
102      * return the URL object corresponding to the env entry name
103      * @param envName the env entry name
104      * @return the URL value corresponding to the env entry name.
105      * @throws ServiceLocatorException if the lookup fails
106      */

107     public URL JavaDoc getUrl(String JavaDoc envName) throws ServiceLocatorException {
108         return (URL JavaDoc)getObject(envName);
109     }
110
111     /**
112      * return a boolean value corresponding to the env entry
113      * @param envName the env entry name
114      * @return the boolean value corresponding to the env entry.
115      * @throws ServiceLocatorException if the lookup fails
116      */

117     public boolean getBoolean(String JavaDoc envName) throws ServiceLocatorException {
118         return ((Boolean JavaDoc)getObject(envName)).booleanValue();
119     }
120
121     /**
122      * return a string value corresponding to the env entry
123      * @param envName the env entry name
124      * @return the boolean value corresponding to the env entry.
125      * @throws ServiceLocatorException if the lookup fails
126      */

127     public String JavaDoc getString(String JavaDoc envName) throws ServiceLocatorException {
128         String JavaDoc ret = null;
129         try {
130             ret = (String JavaDoc)getObject(envName);
131         } catch (ServiceLocatorException e) {
132             if (e.getCause() instanceof NameNotFoundException JavaDoc) {
133                 // ignore this and return null, otherwise we can not have empty values in Glassfish
134
ret = null;
135             }
136         }
137         return ret;
138     }
139
140     /**
141      * return a mail session corresponding to the env entry
142      * @param envName the env entry name
143      * @return the mail session corresponding to the env entry.
144      * @throws ServiceLocatorException if the lookup fails
145      */

146     public Session JavaDoc getMailSession(String JavaDoc envName) throws ServiceLocatorException {
147         return (Session JavaDoc)getObject(envName);
148     }
149
150     /**
151      * return a known java object corresponding to the env entry
152      * @param envName the env entry name
153      * @return the java object corresponding to the env entry
154      * @throws ServiceLocatorException if the lookup fails
155      */

156     public Object JavaDoc getObject(String JavaDoc envName) throws ServiceLocatorException {
157         try {
158             return ctx.lookup(envName);
159         } catch (NamingException JavaDoc e) {
160             throw new ServiceLocatorException(e);
161         }
162     }
163 }
164
Popular Tags