1 37 package com.sun.j2ee.blueprints.servicelocator.web; 38 39 import java.util.*; 40 import java.net.*; 41 import javax.ejb.*; 42 import javax.jms.*; 43 import javax.naming.*; 44 import javax.rmi.*; 45 import javax.sql.*; 46 import javax.transaction.*; 47 48 import com.sun.j2ee.blueprints.servicelocator.ServiceLocatorException; 49 50 58 public class ServiceLocator { 59 60 private InitialContext ic; 61 private Map cache = Collections.synchronizedMap(new HashMap()); 63 64 private static ServiceLocator instance = new ServiceLocator(); 65 66 public static ServiceLocator getInstance() { 67 return instance; 68 } 69 70 private ServiceLocator() throws ServiceLocatorException { 71 try { 72 ic = new InitialContext(); 73 } catch (Exception e) { 74 throw new ServiceLocatorException(e); 75 } 76 } 77 78 84 public EJBLocalHome getLocalHome(String jndiHomeName) throws ServiceLocatorException { 85 EJBLocalHome home = (EJBLocalHome) cache.get(jndiHomeName); 86 if (home == null) { 87 try { 88 home = (EJBLocalHome) ic.lookup(jndiHomeName); 89 cache.put(jndiHomeName, home); 90 } catch (Exception e) { 91 throw new ServiceLocatorException(e); 92 } 93 } 94 return home; 95 } 96 97 103 public EJBHome getRemoteHome(String jndiHomeName, Class className) throws ServiceLocatorException { 104 EJBHome home = (EJBHome) cache.get(jndiHomeName); 105 if (home == null) { 106 try { 107 Object objref = ic.lookup(jndiHomeName); 108 Object obj = PortableRemoteObject.narrow(objref, className); 109 home = (EJBHome)obj; 110 cache.put(jndiHomeName, home); 111 } catch (Exception e) { 112 throw new ServiceLocatorException(e); 113 } 114 } 115 return home; 116 } 117 118 121 public ConnectionFactory getJMSConnectionFactory(String jmsConnFactoryName) 122 throws ServiceLocatorException { 123 ConnectionFactory factory = (ConnectionFactory) cache.get(jmsConnFactoryName); 124 if (factory == null) { 125 try { 126 factory = (ConnectionFactory) ic.lookup(jmsConnFactoryName); 127 cache.put(jmsConnFactoryName, factory); 128 } catch (Exception e) { 129 throw new ServiceLocatorException(e); 130 } 131 } 132 return factory; 133 } 134 135 138 public javax.jms.Destination getJMSDestination(String destName) 139 throws ServiceLocatorException { 140 javax.jms.Destination dest = (javax.jms.Destination ) cache.get(destName); 141 if (dest == null) { 142 try { 143 dest =(javax.jms.Destination )ic.lookup(destName); 144 cache.put(destName, dest); 145 } catch (Exception e) { 146 throw new ServiceLocatorException(e); 147 } 148 } 149 return dest; 150 } 151 152 156 public DataSource getDataSource(String dataSourceName) throws ServiceLocatorException { 157 DataSource dataSource = (DataSource) cache.get(dataSourceName); 158 if (dataSource == null) { 159 try { 160 dataSource = (DataSource)ic.lookup(dataSourceName); 161 cache.put(dataSourceName, dataSource ); 162 } catch (Exception e) { 163 throw new ServiceLocatorException(e); 164 } 165 } 166 return dataSource; 167 } 168 169 173 public UserTransaction getUserTransaction(String utName) throws ServiceLocatorException { 174 try { 175 return (UserTransaction) ic.lookup(utName); 176 } catch (Exception e) { 177 throw new ServiceLocatorException(e); 178 } 179 } 180 181 182 186 public URL getUrl(String envName) throws ServiceLocatorException { 187 try { 188 return (URL)ic.lookup(envName); 189 } catch (Exception e) { 190 throw new ServiceLocatorException(e); 191 } 192 } 193 194 198 public boolean getBoolean(String envName) throws ServiceLocatorException { 199 try { 200 return ((Boolean )ic.lookup(envName)).booleanValue(); 201 } catch (Exception e) { 202 throw new ServiceLocatorException(e); 203 } 204 } 205 206 210 public String getString(String envName) throws ServiceLocatorException { 211 try { 212 return (String )ic.lookup(envName); 213 } catch (Exception e) { 214 throw new ServiceLocatorException(e); 215 } 216 } 217 } 218 | Popular Tags |