package com.oneplace.customerprofile.jms; import javax.jms.Queue; import javax.jms.QueueConnectionFactory; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.oneplace.customerprofile.types.ProfileException; public class JMSConnnectionFactory { private static final String JNDI_LOOKUP_EMS_CONNECTION = "cp-ems-connection-factory"; private static final String JNDI_LOOKUP_JMS_CONNECTION = "cp-jms-connection-factory"; private static final String JNDI_LOOKUP_AAE_CONNECTION = "cp-aae-connection-factory"; private static QueueConnectionFactory emsConnFactory = null; private static QueueConnectionFactory jmsConnFactory = null; private static QueueConnectionFactory emsAAEConnFactory = null; private static final Log log = LogFactory.getLog(JMSConnnectionFactory.class); private static final Object lockObj = new Object(); /** * Lookups the Queue Connection Factory from the JNDI * * @return Queue Connection Factory * @throws ProfileException */ public static QueueConnectionFactory getConnectionFactory(String factoryJndiName) throws ProfileException { QueueConnectionFactory returnObj = null; if (JNDI_LOOKUP_JMS_CONNECTION.equals(factoryJndiName)) { if (jmsConnFactory == null) { synchronized(lockObj) { if (jmsConnFactory == null) { jmsConnFactory = lookupConnectFactory(JNDI_LOOKUP_JMS_CONNECTION); } } } returnObj = jmsConnFactory; } else if (JNDI_LOOKUP_EMS_CONNECTION.equals(factoryJndiName)) { if (emsConnFactory == null) { synchronized(lockObj) { if (emsConnFactory == null) { emsConnFactory = lookupConnectFactory(JNDI_LOOKUP_EMS_CONNECTION); } } } returnObj = emsConnFactory; } else if (JNDI_LOOKUP_AAE_CONNECTION.equals(factoryJndiName)) { if (emsAAEConnFactory == null) { synchronized(lockObj) { if (emsAAEConnFactory == null) { emsAAEConnFactory = lookupConnectFactory(JNDI_LOOKUP_AAE_CONNECTION); } } } returnObj = emsAAEConnFactory; } return returnObj; } /** * Looks up the Queue from the JNDI * * @param queueJndiName JNDI Name where the queue is bound to * @return queue instance * @throws ProfileException */ public static Queue getQueue(String queueJndiName) throws ProfileException { Context ctx = null; Queue queue = null; final String methodName = "lookupConnectFactory"; try { log.info("begin :" + methodName); ctx = new InitialContext(); queue = (Queue) ctx.lookup(queueJndiName); log.info("end :" + methodName); } catch (NamingException ne) { final String message = queueJndiName + " : JMS Queue Lookup failed with: " + ne.getMessage(); log.fatal(message, ne); throw new ProfileException(ProfileException.CODE_JMS_FAILED, message, ne); } finally { closeContext(ctx, methodName); } return queue; } private static QueueConnectionFactory lookupConnectFactory(String factoryJndiName) throws ProfileException { Context ctx = null; QueueConnectionFactory factory = null; final String methodName = "lookupConnectFactory"; try { log.info("begin :" + methodName); ctx = new InitialContext(); factory = (QueueConnectionFactory) ctx.lookup(factoryJndiName); log.info("end :" + methodName); } catch (NamingException ne) { final String message = "JMS Connection Factory Lookup failed with: " + ne.getMessage(); log.fatal(message, ne); throw new ProfileException(ProfileException.CODE_JMS_CONN_FACTORY_LOOKUP_FAILED, message, ne); } finally { closeContext(ctx, methodName); } return factory; } private static void closeContext(Context ctx, final String methodName) { try { if (ctx != null) ctx.close(); } catch (Exception e) { log.warn(methodName + " JNDI Context close() method failed", e); } } }