1 6 package league.ejb; 7 8 import java.util.HashMap ; 9 import java.util.Map ; 10 import java.util.Collections ; 11 import java.net.URL ; 12 13 import javax.ejb.EJBHome ; 14 import javax.ejb.EJBLocalHome ; 15 import javax.jms.ConnectionFactory ; 16 import javax.jms.Destination ; 17 import javax.naming.InitialContext ; 18 import javax.naming.NamingException ; 19 import javax.rmi.PortableRemoteObject ; 20 import javax.sql.DataSource ; 21 22 27 public class CachingServiceLocator { 28 29 private InitialContext ic; 30 private Map cache; 31 32 private static CachingServiceLocator me; 33 34 static { 35 try { 36 me = new CachingServiceLocator(); 37 } catch(NamingException se) { 38 throw new RuntimeException (se); 39 } 40 } 41 42 private CachingServiceLocator() throws NamingException { 43 ic = new InitialContext (); 44 cache = Collections.synchronizedMap(new HashMap ()); 45 } 46 47 public static CachingServiceLocator getInstance() { 48 return me; 49 } 50 51 private Object lookup(String jndiName) throws NamingException { 52 Object cachedObj = cache.get(jndiName); 53 if (cachedObj == null) { 54 cachedObj = ic.lookup(jndiName); 55 cache.put(jndiName, cachedObj); 56 } 57 return cachedObj; 58 } 59 60 66 public EJBLocalHome getLocalHome(String jndiHomeName) throws NamingException { 67 return (EJBLocalHome ) lookup(jndiHomeName); 68 } 69 70 76 public EJBHome getRemoteHome(String jndiHomeName, Class className) throws NamingException { 77 Object objref = lookup(jndiHomeName); 78 return (EJBHome ) PortableRemoteObject.narrow(objref, className); 79 } 80 81 85 public ConnectionFactory getConnectionFactory(String connFactoryName) throws NamingException { 86 return (ConnectionFactory ) lookup(connFactoryName); 87 } 88 89 93 public Destination getDestination(String destName) throws NamingException { 94 return (Destination ) lookup(destName); 95 } 96 97 101 public DataSource getDataSource(String dataSourceName) throws NamingException { 102 return (DataSource ) lookup(dataSourceName); 103 } 104 105 109 public URL getUrl(String envName) throws NamingException { 110 return (URL ) lookup(envName); 111 } 112 113 117 public boolean getBoolean(String envName) throws NamingException { 118 Boolean bool = (Boolean ) lookup(envName); 119 return bool.booleanValue(); 120 } 121 122 126 public String getString(String envName) throws NamingException { 127 return (String ) lookup(envName); 128 } 129 } 130 131 | Popular Tags |