package com.sun.j2ee.blueprints.servicelocator.web;
import java.util.HashMap;
import java.util.Map;
import java.util.Collections;
import java.net.URL;
import javax.ejb.EJBHome;
import javax.ejb.EJBLocalHome;
import javax.jms.QueueConnectionFactory;
import javax.jms.Queue;
import javax.jms.TopicConnectionFactory;
import javax.jms.Topic;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.sql.DataSource;
import com.sun.j2ee.blueprints.servicelocator.ServiceLocatorException;
public class ServiceLocator {
private InitialContext ic;
private Map cache;
private static ServiceLocator me;
static {
try {
me = new ServiceLocator();
} catch(ServiceLocatorException se) {
System.err.println(se);
se.printStackTrace(System.err);
}
}
private ServiceLocator() throws ServiceLocatorException {
try {
ic = new InitialContext();
cache = Collections.synchronizedMap(new HashMap());
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
}
static public ServiceLocator getInstance() {
return me;
}
public EJBLocalHome getLocalHome(String jndiHomeName) throws ServiceLocatorException {
EJBLocalHome home = null;
try {
if (cache.containsKey(jndiHomeName)) {
home = (EJBLocalHome) cache.get(jndiHomeName);
} else {
home = (EJBLocalHome) ic.lookup(jndiHomeName);
cache.put(jndiHomeName, home);
}
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
return home;
}
public EJBHome getRemoteHome(String jndiHomeName, Class className) throws ServiceLocatorException {
EJBHome home = null;
try {
if (cache.containsKey(jndiHomeName)) {
home = (EJBHome) cache.get(jndiHomeName);
} else {
Object objref = ic.lookup(jndiHomeName);
Object obj = PortableRemoteObject.narrow(objref, className);
home = (EJBHome)obj;
cache.put(jndiHomeName, home);
}
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
return home;
}
public QueueConnectionFactory getQueueConnectionFactory(String qConnFactoryName)
throws ServiceLocatorException {
QueueConnectionFactory factory = null;
try {
if (cache.containsKey(qConnFactoryName)) {
factory = (QueueConnectionFactory) cache.get(qConnFactoryName);
} else {
factory = (QueueConnectionFactory) ic.lookup(qConnFactoryName);
cache.put(qConnFactoryName, factory);
}
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
return factory;
}
public Queue getQueue(String queueName) throws ServiceLocatorException {
Queue queue = null;
try {
if (cache.containsKey(queueName)) {
queue = (Queue) cache.get(queueName);
} else {
queue =(Queue)ic.lookup(queueName);
cache.put(queueName, queue);
}
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
return queue;
}
public TopicConnectionFactory getTopicConnectionFactory(String topicConnFactoryName) throws ServiceLocatorException {
TopicConnectionFactory factory = null;
try {
if (cache.containsKey(topicConnFactoryName)) {
factory = (TopicConnectionFactory) cache.get(topicConnFactoryName);
} else {
factory = (TopicConnectionFactory) ic.lookup(topicConnFactoryName);
cache.put(topicConnFactoryName, factory);
}
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
return factory;
}
public Topic getTopic(String topicName) throws ServiceLocatorException {
Topic topic = null;
try {
if (cache.containsKey(topicName)) {
topic = (Topic) cache.get(topicName);
} else {
topic = (Topic)ic.lookup(topicName);
cache.put(topicName, topic);
}
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
return topic;
}
public DataSource getDataSource(String dataSourceName) throws ServiceLocatorException {
DataSource dataSource = null;
try {
if (cache.containsKey(dataSourceName)) {
dataSource = (DataSource) cache.get(dataSourceName);
} else {
dataSource = (DataSource)ic.lookup(dataSourceName);
cache.put(dataSourceName, dataSource );
}
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
return dataSource;
}
public URL getUrl(String envName) throws ServiceLocatorException {
URL url = null;
try {
url = (URL)ic.lookup(envName);
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
return url;
}
public boolean getBoolean(String envName) throws ServiceLocatorException {
Boolean bool = null;
try {
bool = (Boolean)ic.lookup(envName);
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
return bool.booleanValue();
}
public String getString(String envName) throws ServiceLocatorException {
String envEntry = null;
try {
envEntry = (String)ic.lookup(envName);
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
return envEntry ;
}
}