001: package org.ejbca.core.ejb;
002:
003: import java.net.URL;
004: import java.util.Map;
005: import java.util.Collections;
006: import java.util.HashMap;
007: import javax.naming.InitialContext;
008: import javax.naming.NameNotFoundException;
009: import javax.naming.NamingException;
010: import javax.naming.Context;
011: import javax.rmi.PortableRemoteObject;
012: import javax.sql.DataSource;
013: import javax.ejb.EJBHome;
014: import javax.ejb.EJBLocalHome;
015: import javax.mail.Session;
016:
017: /**
018: * A simple implementation of the ServiceLocator/HomeFactory J2EE Pattern.
019: * {@link http://developer.java.sun.com/developer/restricted/patterns/ServiceLocator.html}
020: *
021: * It is used to look up JNDI related resources such as EJB homes, datasources, ...
022: * @version $Id: ServiceLocator.java,v 1.2 2006/10/02 07:54:37 anatom Exp $
023: */
024: public class ServiceLocator {
025:
026: /** ejb home cache */
027: private transient Map ejbHomes = Collections
028: .synchronizedMap(new HashMap());
029:
030: /** the jndi context */
031: private transient Context ctx;
032:
033: /** the singleton instance */
034: private static transient ServiceLocator instance;
035:
036: /**
037: * Create a new service locator object.
038: * @throws ServiceLocatorException if the context failed to be initialized
039: */
040: private ServiceLocator() throws ServiceLocatorException {
041: try {
042: this .ctx = new InitialContext();
043: } catch (NamingException e) {
044: throw new ServiceLocatorException(e);
045: }
046: }
047:
048: /**
049: * return the singleton instance
050: * @return the singleton instance
051: * @throws ServiceLocatorException if the instance could not be initialized the first time
052: */
053: public static final ServiceLocator getInstance()
054: throws ServiceLocatorException {
055: // synchronization is intentionally left out. It 'should' not have dramatic
056: // consequences as it is not that destructive.
057: if (instance == null) {
058: instance = new ServiceLocator();
059: }
060: return instance;
061: }
062:
063: /**
064: * return the ejb local home.
065: * clients need to cast to the type of EJBHome they desire
066: * @param jndiHomeName the jndi home name matching the requested local home.
067: * @return the Local EJB Home corresponding to the home name
068: */
069: public EJBLocalHome getLocalHome(String jndiHomeName)
070: throws ServiceLocatorException {
071: EJBLocalHome home = (EJBLocalHome) ejbHomes.get(jndiHomeName);
072: if (home == null) {
073: home = (EJBLocalHome) getObject(jndiHomeName);
074: ejbHomes.put(jndiHomeName, home);
075: }
076: return home;
077: }
078:
079: /**
080: * return the ejb remote home.
081: * clients need to cast to the type of EJBHome they desire
082: * @param jndiHomeName the jndi home name matching the requested remote home.
083: * @return the Local EJB Home corresponding to the home name
084: */
085: public EJBHome getRemoteHome(String jndiHomeName, Class className)
086: throws ServiceLocatorException {
087: EJBHome home = (EJBHome) ejbHomes.get(className);
088: if (home == null) {
089: Object objref = getObject(jndiHomeName);
090: home = (EJBHome) PortableRemoteObject.narrow(objref,
091: className);
092: ejbHomes.put(className, home);
093: }
094: return home;
095: }
096:
097: /**
098: * return the datasource object corresponding the the env entry name
099: * @return the DataSource corresponding to the env entry name parameter
100: * @throws ServiceLocatorException if the lookup fails
101: */
102: public DataSource getDataSource(String dataSourceName)
103: throws ServiceLocatorException {
104: return (DataSource) getObject(dataSourceName);
105: }
106:
107: /**
108: * return the URL object corresponding to the env entry name
109: * @param envName the env entry name
110: * @return the URL value corresponding to the env entry name.
111: * @throws ServiceLocatorException if the lookup fails
112: */
113: public URL getUrl(String envName) throws ServiceLocatorException {
114: return (URL) getObject(envName);
115: }
116:
117: /**
118: * return a boolean value corresponding to the env entry
119: * @param envName the env entry name
120: * @return the boolean value corresponding to the env entry.
121: * @throws ServiceLocatorException if the lookup fails
122: */
123: public boolean getBoolean(String envName)
124: throws ServiceLocatorException {
125: return ((Boolean) getObject(envName)).booleanValue();
126: }
127:
128: /**
129: * return a string value corresponding to the env entry
130: * @param envName the env entry name
131: * @return the boolean value corresponding to the env entry.
132: * @throws ServiceLocatorException if the lookup fails
133: */
134: public String getString(String envName)
135: throws ServiceLocatorException {
136: String ret = null;
137: try {
138: ret = (String) getObject(envName);
139: } catch (ServiceLocatorException e) {
140: if (e.getCause() instanceof NameNotFoundException) {
141: // ignore this and return null, otherwise we can not have empty values in Glassfish
142: ret = null;
143: }
144: }
145: return ret;
146: }
147:
148: /**
149: * return a mail session corresponding to the env entry
150: * @param envName the env entry name
151: * @return the mail session corresponding to the env entry.
152: * @throws ServiceLocatorException if the lookup fails
153: */
154: public Session getMailSession(String envName)
155: throws ServiceLocatorException {
156: return (Session) getObject(envName);
157: }
158:
159: /**
160: * return a known java object corresponding to the env entry
161: * @param envName the env entry name
162: * @return the java object corresponding to the env entry
163: * @throws ServiceLocatorException if the lookup fails
164: */
165: public Object getObject(String envName)
166: throws ServiceLocatorException {
167: try {
168: return ctx.lookup(envName);
169: } catch (NamingException e) {
170: throw new ServiceLocatorException(e);
171: }
172: }
173: }
|