/**
 * Copyright (C) 2016 Daniel Straub (ds@ctrlaltdel.de)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.ctrlaltdel.jms;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSConsumer;
import javax.jms.JMSException;
import javax.jms.JMSProducer;
import javax.jms.JMSRuntimeException;
import javax.jms.Session;
import javax.jms.Topic;

/**
 * DestinationJmsContext
 */
public class DefaultJMSContext extends JMSContextAdapter {

    private final Connection connection;
    private final Session session;

    public DefaultJMSContext(ConnectionFactory connectionFactory) {
        this(connectionFactory, Session.AUTO_ACKNOWLEDGE);
    }

    public DefaultJMSContext(ConnectionFactory connectionFactory, int sessionMode) {
        try {
            this.connection = connectionFactory.createConnection();
            this.connection.start();
            this.session = createSession(connection, sessionMode);
        } catch (JMSException e) {
            throw new JMSRuntimeException(e.getMessage());
        }
    }

    private Session createSession(Connection connection, int sessionMode) throws JMSException {
        try {
            return connection.createSession(sessionMode);
        } catch (AbstractMethodError e) {
            // not a JMS2.0 Connection, corrret sessionMode to JMS 1 value ...
            if (sessionMode == Session.SESSION_TRANSACTED) {
                sessionMode = Session.AUTO_ACKNOWLEDGE;
            }
            return connection.createSession(false, sessionMode);
        }
    }

    @Override
    protected Session getSession() {
        return session;
    }

    @Override
    public JMSProducer createProducer() {
        try {
            connection.start();
        } catch (JMSException e) {
            throw new JMSRuntimeException(e.getMessage());
        }
        return new JMSProducerImpl(session);
    }

    @Override
    public String getClientID() {
        try {
            return connection.getClientID();
        } catch (JMSException e) {
            throw new JMSRuntimeException(e.getMessage());
        }
    }

    @Override
    public void close() {
        try {
            session.close();
            connection.stop();
            connection.close();
        } catch (Exception e) {
            //
        }
    }

    @Override
    public JMSConsumer createConsumer(Destination destination, String messageSelector, boolean noLocal) {
        return new JMSConsumerImpl(destination, session, messageSelector, noLocal);
    }

    @Override
    public JMSConsumer createDurableConsumer(Topic topic, String subscriberName, String messageSelector, boolean noLocal) {
        return new JMSConsumerImpl(topic, session, messageSelector, noLocal, subscriberName);
    }

    @Override
    public String toString() {
        return connection.toString();
    }
}
