package com.acme.structure.util;


import com.acme.structure.ServiceLocator;

import java.io.Serializable;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.Session;

import javax.naming.InitialContext;
import javax.naming.NamingException;


public class JMSHelper
{
    protected InitialContext ic = null;
    protected ConnectionFactory connectionFactory = null;
    protected Queue queue = null;
    protected Connection connection = null;
    protected String integrationHost = null;

    //private Logger logger = Logger.getLogger(JMSHelper.class);

    public JMSHelper()
    {
        this("127.0.0.1");
    }

    public JMSHelper(String integrationHost)
    {
        this.integrationHost = integrationHost;
    }

    public void sendMessage(String destination, Serializable messageObject) throws NamingException, JMSException
    {
        sendMessage(destination, messageObject, 0);
    }

    public void sendMessage(String destination, Serializable messageObject, int status) throws NamingException, JMSException
    {
        try
        {
            //InitialContext ic = getInitialContext();
            //queue = (Queue)ic.lookup(destination);
            queue = (Queue)ServiceLocator.getInstance().getResource(destination);

            Session session = createSessionByServiceLocator("jms/ConnectionFactory");

            MessageProducer sender = session.createProducer(queue);
            //sender.setDisableMessageTimestamp(true);
            //sender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            //sender.setTimeToLive(5000);

            ObjectMessage messageImp = session.createObjectMessage(messageObject);
            messageImp.setJMSExpiration(System.currentTimeMillis() + 5000);
            messageImp.setIntProperty("status", status);

            sender.send(messageImp);
        }
        catch (Exception exc)
        {
            //logger.info("exception while sending message to : " + destination);
            //logger.error(exc.toString());
            exc.printStackTrace();
        }
        finally
        {
            connection.close();
        }
    }

    private Session createSessionByServiceLocator(String connectionFactoryName) throws NamingException, JMSException
    {
        InitialContext ic = ServiceLocator.getInstance().getInitialContext();
        connectionFactory = (ConnectionFactory)ic.lookup(connectionFactoryName);
        connection = connectionFactory.createConnection();
        return connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    }

    //    private Session createSession(String connectionFactoryName) throws NamingException, JMSException
    //    {
    //        InitialContext ic = getInitialContext();
    //        connectionFactory = (ConnectionFactory)ic.lookup(connectionFactoryName);
    //        connection = connectionFactory.createConnection();
    //        return connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    //    }
    //
    //    private InitialContext getInitialContext() throws NamingException
    //    {
    //        Properties props = IOUtility.loadProperties("/com/arsivist/structure/resource/jndiconnection.properties", this.getClass());
    //        String userName = null;
    //        String password = null;
    //        String providerURL = null;
    //        String initialContextFactory = null;
    //        if (props != null)
    //        {
    //            userName = props.getProperty("username");
    //            password = props.getProperty("password");
    //            providerURL = props.getProperty("providerurl");
    //            initialContextFactory = props.getProperty("jndicontextfactory");
    //        }
    //
    //        return getInitialContext(userName, password, providerURL, initialContextFactory);
    //    }
    //
    //    private InitialContext getInitialContext(String userName, String password, String providerURL, String initialContextFactory) throws NamingException
    //    {
    //        if (ic == null)
    //        {
    //            Properties properties = new Properties();
    //
    //            if (userName != null && password != null)
    //            {
    //                properties.put(InitialContext.INITIAL_CONTEXT_FACTORY, initialContextFactory);
    //                properties.put(InitialContext.SECURITY_PRINCIPAL, userName);
    //                properties.put(InitialContext.SECURITY_CREDENTIALS, password);
    //            } else
    //                properties.put(InitialContext.INITIAL_CONTEXT_FACTORY, initialContextFactory);
    //
    //            properties.put(InitialContext.PROVIDER_URL, providerURL);
    //
    //            return (ic = new InitialContext(properties));
    //        } else
    //            return ic;
    //    }

}
