package com.javahonk.messaging;


import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Session;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.tibco.tibjms.TibjmsConnectionFactory;

/**
 * @author Java Honk
 *
 */
public class ConnectionFactory {
	
	private static final Logger logger = LogManager.getLogger(ConnectionFactory.class.getName());
	
	private String userName;
	private String userPassword;
	private TibjmsConnectionFactory factory;
	
	public ConnectionFactory(String userName, String userPassword, TibjmsConnectionFactory factory) {
		this.userName = userName;
		this.userPassword = userPassword;
		this.factory = factory;

	}
	
	public Connection createConnection() {
		
		Connection connection = null;

		try {
			connection =  factory.createConnection(userName, userPassword);			
			
		} catch (JMSException e) {
			logger.error("Failed to create connection", e);
		}
		
		return connection;
	}
	
	public Session createSession(Connection connection) {
		Session session = null;
		
		try {
			session = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
		} catch (JMSException e) {
			logger.error("Failed to create session", e);
		} 
		
		return session;
		
		
	}

}
