1 package org.ejbca.core.ejb; 2 3 import java.net.URL ; 4 import java.util.Map ; 5 import java.util.Collections ; 6 import java.util.HashMap ; 7 import javax.naming.InitialContext ; 8 import javax.naming.NameNotFoundException ; 9 import javax.naming.NamingException ; 10 import javax.naming.Context ; 11 import javax.rmi.PortableRemoteObject ; 12 import javax.sql.DataSource ; 13 import javax.ejb.EJBHome ; 14 import javax.ejb.EJBLocalHome ; 15 import javax.mail.Session ; 16 17 24 public class ServiceLocator { 25 26 27 private transient Map ejbHomes = Collections.synchronizedMap(new HashMap ()); 28 29 30 private transient Context ctx; 31 32 33 private static transient ServiceLocator instance; 34 35 39 private ServiceLocator() throws ServiceLocatorException { 40 try { 41 this.ctx = new InitialContext (); 42 } catch (NamingException e){ 43 throw new ServiceLocatorException(e); 44 } 45 } 46 47 52 public static final ServiceLocator getInstance() throws ServiceLocatorException { 53 if (instance == null){ 56 instance = new ServiceLocator(); 57 } 58 return instance; 59 } 60 61 67 public EJBLocalHome getLocalHome(String jndiHomeName) throws ServiceLocatorException { 68 EJBLocalHome home = (EJBLocalHome )ejbHomes.get(jndiHomeName); 69 if (home == null) { 70 home = (EJBLocalHome )getObject(jndiHomeName); 71 ejbHomes.put(jndiHomeName, home); 72 } 73 return home; 74 } 75 76 82 public EJBHome getRemoteHome(String jndiHomeName, Class className) throws ServiceLocatorException { 83 EJBHome home = (EJBHome )ejbHomes.get(className); 84 if (home == null) { 85 Object objref = getObject(jndiHomeName); 86 home = (EJBHome ) PortableRemoteObject.narrow(objref, className); 87 ejbHomes.put(className, home); 88 } 89 return home; 90 } 91 92 97 public DataSource getDataSource(String dataSourceName) throws ServiceLocatorException { 98 return (DataSource )getObject(dataSourceName); 99 } 100 101 107 public URL getUrl(String envName) throws ServiceLocatorException { 108 return (URL )getObject(envName); 109 } 110 111 117 public boolean getBoolean(String envName) throws ServiceLocatorException { 118 return ((Boolean )getObject(envName)).booleanValue(); 119 } 120 121 127 public String getString(String envName) throws ServiceLocatorException { 128 String ret = null; 129 try { 130 ret = (String )getObject(envName); 131 } catch (ServiceLocatorException e) { 132 if (e.getCause() instanceof NameNotFoundException ) { 133 ret = null; 135 } 136 } 137 return ret; 138 } 139 140 146 public Session getMailSession(String envName) throws ServiceLocatorException { 147 return (Session )getObject(envName); 148 } 149 150 156 public Object getObject(String envName) throws ServiceLocatorException { 157 try { 158 return ctx.lookup(envName); 159 } catch (NamingException e) { 160 throw new ServiceLocatorException(e); 161 } 162 } 163 } 164 | Popular Tags |