1 package com.jdon.servicelocator.ejb; 2 3 4 import java.net.URL ; 5 6 import javax.ejb.EJBHome ; 7 import javax.ejb.EJBLocalHome ; 8 import javax.jms.QueueConnectionFactory ; 9 import javax.jms.Queue ; 10 import javax.jms.TopicConnectionFactory ; 11 import javax.jms.Topic ; 12 import javax.naming.InitialContext ; 13 import javax.naming.NamingException ; 14 import javax.rmi.PortableRemoteObject ; 15 import javax.sql.DataSource ; 16 17 import com.jdon.servicelocator.ServiceLocatorException; 18 import org.apache.log4j.Logger; 19 23 public class ServiceLocator implements java.io.Serializable { 24 private final static Logger logger = Logger.getLogger(ServiceLocator.class); 25 private InitialContext ic; 26 27 private static ServiceLocator me; 28 29 public ServiceLocator() throws ServiceLocatorException { 30 try { 31 ic = new InitialContext (); 32 } catch (NamingException ne) { 33 throw new ServiceLocatorException(ne); 34 } catch (Exception e) { 35 throw new ServiceLocatorException(e); 36 } 37 } 38 39 45 public EJBLocalHome getLocalHome(String jndiHomeName) throws 46 ServiceLocatorException { 47 EJBLocalHome home = null; 48 try { 49 home = (EJBLocalHome ) ic.lookup(jndiHomeName); 50 } catch (NamingException ne) { 51 throw new ServiceLocatorException(ne); 52 } catch (Exception e) { 53 throw new ServiceLocatorException(e); 54 } 55 return home; 56 } 57 58 64 public EJBHome getRemoteHome(String jndiHomeName, Class className) throws 65 ServiceLocatorException { 66 EJBHome home = null; 67 try { 68 Object objref = ic.lookup(jndiHomeName); 69 Object obj = PortableRemoteObject.narrow(objref, className); 70 home = (EJBHome ) obj; 71 } catch (NamingException ne) { 72 throw new ServiceLocatorException(ne); 73 } catch (Exception e) { 74 throw new ServiceLocatorException(e); 75 } 76 return home; 77 } 78 79 82 public QueueConnectionFactory getQueueConnectionFactory(String 83 qConnFactoryName) throws ServiceLocatorException { 84 QueueConnectionFactory factory = null; 85 try { 86 factory = (QueueConnectionFactory ) ic.lookup(qConnFactoryName); 87 } catch (NamingException ne) { 88 throw new ServiceLocatorException(ne); 89 } catch (Exception e) { 90 throw new ServiceLocatorException(e); 91 } 92 return factory; 93 } 94 95 98 public Queue getQueue(String queueName) throws ServiceLocatorException { 99 Queue queue = null; 100 try { 101 queue = (Queue ) ic.lookup(queueName); 102 } catch (NamingException ne) { 103 throw new ServiceLocatorException(ne); 104 } catch (Exception e) { 105 throw new ServiceLocatorException(e); 106 } 107 return queue; 108 } 109 110 114 public TopicConnectionFactory getTopicConnectionFactory(String 115 topicConnFactoryName) throws ServiceLocatorException { 116 TopicConnectionFactory factory = null; 117 try { 118 factory = (TopicConnectionFactory ) ic.lookup(topicConnFactoryName); 119 } catch (NamingException ne) { 120 throw new ServiceLocatorException(ne); 121 } catch (Exception e) { 122 throw new ServiceLocatorException(e); 123 } 124 return factory; 125 } 126 127 131 public Topic getTopic(String topicName) throws ServiceLocatorException { 132 Topic topic = null; 133 try { 134 topic = (Topic ) ic.lookup(topicName); 135 } catch (NamingException ne) { 136 throw new ServiceLocatorException(ne); 137 } catch (Exception e) { 138 throw new ServiceLocatorException(e); 139 } 140 return topic; 141 } 142 143 147 public DataSource getDataSource(String dataSourceName) throws 148 ServiceLocatorException { 149 DataSource dataSource = null; 150 try { 151 dataSource = (DataSource ) ic.lookup(dataSourceName); 152 } catch (NamingException ne) { 153 throw new ServiceLocatorException(ne); 154 } catch (Exception e) { 155 throw new ServiceLocatorException(e); 156 } 157 return dataSource; 158 } 159 160 164 public URL getUrl(String envName) throws ServiceLocatorException { 165 URL url = null; 166 try { 167 url = (URL ) ic.lookup(envName); 168 } catch (NamingException ne) { 169 throw new ServiceLocatorException(ne); 170 } catch (Exception e) { 171 throw new ServiceLocatorException(e); 172 } 173 174 return url; 175 } 176 177 181 public boolean getBoolean(String envName) throws ServiceLocatorException { 182 Boolean bool = null; 183 try { 184 bool = (Boolean ) ic.lookup(envName); 185 } catch (NamingException ne) { 186 throw new ServiceLocatorException(ne); 187 } catch (Exception e) { 188 throw new ServiceLocatorException(e); 189 } 190 return bool.booleanValue(); 191 } 192 193 197 public String getString(String envName) throws ServiceLocatorException { 198 String envEntry = null; 199 try { 200 envEntry = (String ) ic.lookup(envName); 201 } catch (NamingException ne) { 202 throw new ServiceLocatorException(ne); 203 } catch (Exception e) { 204 throw new ServiceLocatorException(e); 205 } 206 return envEntry; 207 } 208 209 public Object getDAO(String jndiDAOName) throws ServiceLocatorException { 210 211 Object object = null; 212 try { 213 String className = (String ) ic.lookup(jndiDAOName); 214 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 215 object = classLoader.loadClass(className).newInstance(); 216 } catch (NamingException ne) { 217 throw new ServiceLocatorException(ne); 218 } catch (Exception se) { 219 throw new ServiceLocatorException(se); 220 } 221 return object; 222 } 223 224 } 225 | Popular Tags |