package com.eactive.eai.common.jms;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.jms.Queue;
import javax.jms.QueueConnectionFactory;
import javax.jms.Topic;
import javax.jms.TopicConnectionFactory;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.common.util.ServiceLocatorException;

public class JmsServiceLocator {
	static private Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
	private static JmsServiceLocator instance;
	private InitialContext initial;
	private Map cache;

	protected JmsServiceLocator() throws ServiceLocatorException {
		try {
			initial = new InitialContext();
			cache = Collections.synchronizedMap(new HashMap());
		} catch (NamingException e) {
			throw new ServiceLocatorException(ExceptionUtil.getErrorCode(e, "RECEAICUL101"));
		} catch (Exception e) {
			throw new ServiceLocatorException(ExceptionUtil.getErrorCode(e, "RECEAICUL102"));
		}
	}

	public static synchronized JmsServiceLocator getInstance() throws ServiceLocatorException {
		if (instance == null) {
			instance = new JmsServiceLocator();
			return instance;
		} else {
			return instance;
		}
	}

	public QueueConnectionFactory getQueueConnectionFactory(String qConnFactoryName) throws ServiceLocatorException {
		QueueConnectionFactory factory = null;

		try {
			if (cache.containsKey(qConnFactoryName)) {
				factory = (QueueConnectionFactory) cache.get(qConnFactoryName);
			} else {
				factory = (QueueConnectionFactory) initial.lookup(qConnFactoryName);
				cache.put(qConnFactoryName, factory);
			}
		} catch (NamingException ne) {
			throw new ServiceLocatorException(ExceptionUtil.getErrorCode(ne, "RECEAICUL113"));
		} catch (Exception e) {
			throw new ServiceLocatorException(ExceptionUtil.getErrorCode(e, "RECEAICUL114"));
		}
		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) initial.lookup(queueName);
				cache.put(queueName, queue);
			}
		} catch (NamingException ne) {
			throw new ServiceLocatorException(ExceptionUtil.getErrorCode(ne, "RECEAICUL115"));
		} catch (Exception e) {
			throw new ServiceLocatorException(ExceptionUtil.getErrorCode(e, "RECEAICUL116"));
		}
		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) initial.lookup(topicConnFactoryName);
				cache.put(topicConnFactoryName, factory);
			}
		} catch (NamingException ne) {
			throw new ServiceLocatorException(ExceptionUtil.getErrorCode(ne, "RECEAICUL117"));
		} catch (Exception e) {
			throw new ServiceLocatorException(ExceptionUtil.getErrorCode(e, "RECEAICUL118"));
		}
		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) initial.lookup(topicName);
				cache.put(topicName, topic);
			}
		} catch (NamingException ne) {
			throw new ServiceLocatorException(ExceptionUtil.getErrorCode(ne, "RECEAICUL119"));
		} catch (Exception e) {
			throw new ServiceLocatorException(ExceptionUtil.getErrorCode(e, "RECEAICUL120"));
		}
		return topic;
	}
}
