1 6 package mix; 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 import javax.mail.Session ; 22 23 28 public class ServiceLocator { 29 30 private InitialContext ic; 31 private Map cache; 32 33 private static ServiceLocator me; 34 35 static { 36 try { 37 me = new ServiceLocator(); 38 } catch(NamingException se) { 39 throw new RuntimeException (se); 40 } 41 } 42 43 private ServiceLocator() throws NamingException { 44 ic = new InitialContext (); 45 cache = Collections.synchronizedMap(new HashMap ()); 46 } 47 48 public static ServiceLocator getInstance() { 49 return me; 50 } 51 52 private Object lookup(String jndiName) throws NamingException { 53 Object cachedObj = cache.get(jndiName); 54 if (cachedObj == null) { 55 cachedObj = ic.lookup(jndiName); 56 cache.put(jndiName, cachedObj); 57 } 58 return cachedObj; 59 } 60 61 67 public EJBLocalHome getLocalHome(String jndiHomeName) throws NamingException { 68 return (EJBLocalHome ) lookup(jndiHomeName); 69 } 70 71 77 public EJBHome getRemoteHome(String jndiHomeName, Class className) throws NamingException { 78 Object objref = lookup(jndiHomeName); 79 return (EJBHome ) PortableRemoteObject.narrow(objref, className); 80 } 81 82 86 public ConnectionFactory getConnectionFactory(String connFactoryName) throws NamingException { 87 return (ConnectionFactory ) lookup(connFactoryName); 88 } 89 90 94 public Destination getDestination(String destName) throws NamingException { 95 return (Destination ) lookup(destName); 96 } 97 98 102 public DataSource getDataSource(String dataSourceName) throws NamingException { 103 return (DataSource ) lookup(dataSourceName); 104 } 105 106 110 public Session getSession(String sessionName) throws NamingException { 111 return (Session ) lookup(sessionName); 112 } 113 114 118 public URL getUrl(String envName) throws NamingException { 119 return (URL ) lookup(envName); 120 } 121 122 126 public boolean getBoolean(String envName) throws NamingException { 127 Boolean bool = (Boolean ) lookup(envName); 128 return bool.booleanValue(); 129 } 130 131 135 public String getString(String envName) throws NamingException { 136 return (String ) lookup(envName); 137 } 138 } 139 140 | Popular Tags |