1 package com.jdon.servicelocator.web; 2 3 import java.net.URL ; 4 import java.util.Collections ; 5 import java.util.HashMap ; 6 import java.util.Map ; 7 8 import javax.ejb.EJBHome ; 9 import javax.ejb.EJBLocalHome ; 10 import javax.jms.Queue ; 11 import javax.jms.QueueConnectionFactory ; 12 import javax.jms.Topic ; 13 import javax.jms.TopicConnectionFactory ; 14 import javax.naming.InitialContext ; 15 import javax.naming.NamingException ; 16 import javax.rmi.PortableRemoteObject ; 17 import javax.sql.DataSource ; 18 19 import com.jdon.container.pico.Startable; 20 import com.jdon.servicelocator.ServiceLocatorException; 21 import com.jdon.util.Debug; 22 23 24 35 public class ServiceLocator implements Startable{ 36 private final static String module = ServiceLocator.class.getName(); 37 38 private InitialContext ic; 39 private Map cache; 41 public ServiceLocator() { 42 try { 43 ic = new InitialContext (); 44 cache = Collections.synchronizedMap(new HashMap ()); 45 } catch (NamingException ne) { 46 System.err.print("NamingException error: " + ne); 47 } catch (Exception e) { 48 System.err.print(e); 49 } 50 } 51 52 public void start() { 53 Debug.logVerbose("[JdonFramework]ServiceLocator start .....", module); 54 cache.clear(); 55 } 56 57 public void stop() { 58 cache.clear(); 59 } 60 61 62 68 public EJBLocalHome getLocalHome(String jndiHomeName) throws 69 ServiceLocatorException { 70 Debug.logVerbose("[JdonFramework] -- > getLocalHome.... ", module); 71 EJBLocalHome home = null; 72 try { 73 if (cache.containsKey(jndiHomeName)) { 74 home = (EJBLocalHome ) cache.get(jndiHomeName); 75 } else { 76 Debug.logVerbose("[JdonFramework] lookUp LocalHome.... ", module); 77 home = (EJBLocalHome ) ic.lookup(jndiHomeName); 78 cache.put(jndiHomeName, home); 79 } 80 } catch (NamingException ne) { 81 throw new ServiceLocatorException(ne); 82 } catch (Exception e) { 83 throw new ServiceLocatorException(e); 84 } 85 return home; 86 } 87 88 94 public EJBHome getRemoteHome(String jndiHomeName, Class className) throws 95 ServiceLocatorException { 96 EJBHome home = null; 97 try { 98 if (cache.containsKey(jndiHomeName)) { 99 home = (EJBHome ) cache.get(jndiHomeName); 100 } else { 101 Object objref = ic.lookup(jndiHomeName); 102 Object obj = PortableRemoteObject.narrow(objref, className); 103 home = (EJBHome ) obj; 104 cache.put(jndiHomeName, home); 105 } 106 } catch (NamingException ne) { 107 throw new ServiceLocatorException(ne); 108 } catch (Exception e) { 109 throw new ServiceLocatorException(e); 110 } 111 112 return home; 113 } 114 115 118 public QueueConnectionFactory getQueueConnectionFactory(String 119 qConnFactoryName) throws ServiceLocatorException { 120 QueueConnectionFactory factory = null; 121 try { 122 if (cache.containsKey(qConnFactoryName)) { 123 factory = (QueueConnectionFactory ) cache.get(qConnFactoryName); 124 } else { 125 factory = (QueueConnectionFactory ) ic.lookup(qConnFactoryName); 126 cache.put(qConnFactoryName, factory); 127 } 128 } catch (NamingException ne) { 129 throw new ServiceLocatorException(ne); 130 } catch (Exception e) { 131 throw new ServiceLocatorException(e); 132 } 133 return factory; 134 } 135 136 139 public Queue getQueue(String queueName) throws ServiceLocatorException { 140 Queue queue = null; 141 try { 142 if (cache.containsKey(queueName)) { 143 queue = (Queue ) cache.get(queueName); 144 } else { 145 queue = (Queue ) ic.lookup(queueName); 146 cache.put(queueName, queue); 147 } 148 } catch (NamingException ne) { 149 throw new ServiceLocatorException(ne); 150 } catch (Exception e) { 151 throw new ServiceLocatorException(e); 152 } 153 154 return queue; 155 } 156 157 161 public TopicConnectionFactory getTopicConnectionFactory(String 162 topicConnFactoryName) throws ServiceLocatorException { 163 TopicConnectionFactory factory = null; 164 try { 165 if (cache.containsKey(topicConnFactoryName)) { 166 factory = (TopicConnectionFactory ) cache.get(topicConnFactoryName); 167 } else { 168 factory = (TopicConnectionFactory ) ic.lookup(topicConnFactoryName); 169 cache.put(topicConnFactoryName, factory); 170 } 171 } catch (NamingException ne) { 172 throw new ServiceLocatorException(ne); 173 } catch (Exception e) { 174 throw new ServiceLocatorException(e); 175 } 176 return factory; 177 } 178 179 183 public Topic getTopic(String topicName) throws ServiceLocatorException { 184 Topic topic = null; 185 try { 186 if (cache.containsKey(topicName)) { 187 topic = (Topic ) cache.get(topicName); 188 } else { 189 topic = (Topic ) ic.lookup(topicName); 190 cache.put(topicName, topic); 191 } 192 } catch (NamingException ne) { 193 throw new ServiceLocatorException(ne); 194 } catch (Exception e) { 195 throw new ServiceLocatorException(e); 196 } 197 return topic; 198 } 199 200 204 public DataSource getDataSource(String dataSourceName) throws 205 ServiceLocatorException { 206 DataSource dataSource = null; 207 try { 208 if (cache.containsKey(dataSourceName)) { 209 dataSource = (DataSource ) cache.get(dataSourceName); 210 } else { 211 dataSource = (DataSource ) ic.lookup(dataSourceName); 212 cache.put(dataSourceName, dataSource); 213 } 214 } catch (NamingException ne) { 215 throw new ServiceLocatorException(ne); 216 } catch (Exception e) { 217 throw new ServiceLocatorException(e); 218 } 219 return dataSource; 220 } 221 222 226 public URL getUrl(String envName) throws ServiceLocatorException { 227 URL url = null; 228 try { 229 url = (URL ) ic.lookup(envName); 230 } catch (NamingException ne) { 231 throw new ServiceLocatorException(ne); 232 } catch (Exception e) { 233 throw new ServiceLocatorException(e); 234 } 235 236 return url; 237 } 238 239 243 public boolean getBoolean(String envName) throws ServiceLocatorException { 244 Boolean bool = null; 245 try { 246 bool = (Boolean ) ic.lookup(envName); 247 } catch (NamingException ne) { 248 throw new ServiceLocatorException(ne); 249 } catch (Exception e) { 250 throw new ServiceLocatorException(e); 251 } 252 return bool.booleanValue(); 253 } 254 255 259 public String getString(String envName) throws ServiceLocatorException { 260 String envEntry = null; 261 try { 262 envEntry = (String ) ic.lookup(envName); 263 } catch (NamingException ne) { 264 throw new ServiceLocatorException(ne); 265 } catch (Exception e) { 266 throw new ServiceLocatorException(e); 267 } 268 return envEntry; 269 } 270 271 public Object getDAO(String jndiDAOName) throws ServiceLocatorException { 272 273 Object object = null; 274 try { 275 String className = (String ) ic.lookup(jndiDAOName); 276 object = Class.forName(className).newInstance(); 277 } catch (NamingException ne) { 278 throw new ServiceLocatorException(ne); 279 } catch (Exception se) { 280 throw new ServiceLocatorException(se); 281 } 282 return object; 283 } 284 285 } 286 | Popular Tags |