package gov.nih.nci.cabig.caaers.esb.client.impl;

import gov.nih.nci.cabig.caaers.tools.configuration.Configuration;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 *
 * @author Rhett Sutphin
 */

public class CaaersConnectionFactory implements ConnectionFactory {
    private ConnectionFactory delegate;
    private Configuration configuration;
    private static final Log log = LogFactory.getLog(CaaersConnectionFactory.class);
    
    public CaaersConnectionFactory(Configuration configuration) throws Exception{
    	this.configuration = configuration;
    	createConnection(null, null);
    }
    
    public Connection createConnection() throws JMSException {
        return createConnection(null, null);
    }

    public Connection createConnection(String username, String password) throws JMSException {
        if (delegate == null) {
            delegate = createDelegate(configuration.get(Configuration.ESB_URL));
        }
        return delegate.createConnection(username, password);
    }

    protected ConnectionFactory createDelegate(final String url) {
    	String newUrl = (StringUtils.isEmpty(url))? "tcp://10.10.10.2:61616" : url;
    	log.info("ESB url :" + newUrl);
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
        factory.setBrokerURL(newUrl);
        return factory;
    }

    ////// CONFIGURATION

}

@@ -44,16 +44,78 @@
 .impl;%0A%0A
+import gov.nih.nci.cabig.caaers.CaaersConfigurationException;%0A
 import g
@@ -169,24 +169,24 @@
 figuration;%0A
-
 %0Aimport java
@@ -859,43 +859,8 @@
 on;%0A
-    %09createConnection(null, null);%0A
     
@@ -1344,120 +1344,158 @@
    %09
-String newUrl = (StringUtils.isEmpty(url))? %22tcp://10.10.10.2:61616%22 : url;%0A    %09log.info(%22ESB url :%22 + newUrl);
+if (url == null) %7B%0A    %09%09   throw new CaaersConfigurationException(%22No ESB Queue URL set.  Please set the property and then restart caAERS.%22);%0A    %09%7D%0A
 %0A   
@@ -1601,12 +1601,9 @@
 URL(
-newU
+u
 rl);

fix for esb url