/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.activeblaze.jms;

import org.apache.activeblaze.BlazeRuntimeException;
import org.apache.activeblaze.group.BlazeGroupChannelFactory;
import org.apache.activeblaze.group.BlazeGroupConfiguration;
import org.apache.activeblaze.jndi.JNDIStorable;
import org.apache.activeblaze.util.IdGenerator;
import org.apache.activeblaze.util.PropertyUtil;

import javax.jms.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Jms ConnectionFactory implementation
 */
public class BlazeJmsConnectionFactory extends JNDIStorable implements ConnectionFactory, QueueConnectionFactory,
        TopicConnectionFactory {
    private static final IdGenerator NAME_GENERATOR = new IdGenerator();
    private final BlazeGroupChannelFactory groupChannelFactory;
    private final Map<String, String> props = new HashMap<String, String>();
    private int consumerMaxDispatchQueueDepth = 10000;

    /**
     * Constructor
     */
    public BlazeJmsConnectionFactory() {
        this(new BlazeGroupConfiguration());
    }

    /**
     * Constructor
     */
    public BlazeJmsConnectionFactory(BlazeGroupConfiguration config) {
        this.groupChannelFactory = new BlazeGroupChannelFactory(config);
    }

    /**
     * @return the underlying <Code>BlazeGroupConfiguration</Code>
     */
    public BlazeGroupConfiguration getConfiguration() {
        return this.groupChannelFactory.getConfiguration();
    }

    /**
     * Set properties
     */
    public void setProperties(Properties props) {
        Map<String, String> map = new HashMap<String, String>();
        for (Map.Entry<Object, Object> entry : props.entrySet()) {
            map.put(entry.getKey().toString(), entry.getValue().toString());
        }
        setProperties(map);
    }

    @Override
    public void setProperties(Map<String, String> map) {
        populateProperties(map);
    }

    /**
     * @see org.apache.activeblaze.jndi.JNDIStorable#buildFromProperties(Map<String, String> map)
     */
    @Override
    protected void buildFromProperties(Map<String, String> map) {
        PropertyUtil.setProperties(this.groupChannelFactory.getConfiguration(), map);
        PropertyUtil.setProperties(this, map);
    }

    /**
     * @see org.apache.activeblaze.jndi.JNDIStorable#populateProperties(Map<String, String> map)
     */
    @Override
    protected void populateProperties(Map<String, String> map) {
        try {
            Map<String, String> result = PropertyUtil.getProperties(this.groupChannelFactory.getConfiguration());
            map.putAll(result);
            result = PropertyUtil.getProperties(this);
            map.putAll(result);
            this.props.putAll(map);
        } catch (Exception e) {
            throw new BlazeRuntimeException(e);
        }
    }

    /**
     * @return a TopicConnection
     * @see javax.jms.TopicConnectionFactory#createTopicConnection()
     */
    public TopicConnection createTopicConnection() throws JMSException {
        try {
            TopicConnection result = new BlazeJmsConnection(this.groupChannelFactory.createGroupChannel(NAME_GENERATOR
                    .generateId()));
            PropertyUtil.setProperties(result, this.props);
            return result;
        } catch (Exception e) {
            throw BlazeJmsExceptionSupport.create(e);
        }
    }

    /**
     * @return a TopicConnection
     * @see javax.jms.TopicConnectionFactory#createTopicConnection(java.lang.String, java.lang.String)
     */
    public TopicConnection createTopicConnection(String userName, String password) throws JMSException {
        try {
            TopicConnection result = new BlazeJmsConnection(this.groupChannelFactory.createGroupChannel(NAME_GENERATOR
                    .generateId()));
            PropertyUtil.setProperties(result, this.props);
            return result;
        } catch (Exception e) {
            throw BlazeJmsExceptionSupport.create(e);
        }
    }

    /**
     * @return a Connection
     * @see javax.jms.ConnectionFactory#createConnection()
     */
    public Connection createConnection() throws JMSException {
        try {
            Connection result = new BlazeJmsConnection(this.groupChannelFactory.createGroupChannel(NAME_GENERATOR
                    .generateId()));
            PropertyUtil.setProperties(result, this.props);
            return result;
        } catch (Exception e) {
            throw BlazeJmsExceptionSupport.create(e);
        }
    }

    /**
     * @return Connection
     * @see javax.jms.ConnectionFactory#createConnection(java.lang.String, java.lang.String)
     */
    public Connection createConnection(String userName, String password) throws JMSException {
        try {
            Connection result = new BlazeJmsConnection(this.groupChannelFactory.createGroupChannel(NAME_GENERATOR
                    .generateId()));
            PropertyUtil.setProperties(result, this.props);
            return result;
        } catch (Exception e) {
            throw BlazeJmsExceptionSupport.create(e);
        }
    }

    /**
     * @return a QueueConnection
     * @see javax.jms.QueueConnectionFactory#createQueueConnection()
     */
    public QueueConnection createQueueConnection() throws JMSException {
        try {
            QueueConnection result = new BlazeJmsConnection(this.groupChannelFactory.createGroupChannel(NAME_GENERATOR
                    .generateId()));
            PropertyUtil.setProperties(result, this.props);
            return result;
        } catch (Exception e) {
            throw BlazeJmsExceptionSupport.create(e);
        }
    }

    /**
     * @return a QueueConnection
     * @see javax.jms.QueueConnectionFactory#createQueueConnection(java.lang.String, java.lang.String)
     */
    public QueueConnection createQueueConnection(String userName, String password) throws JMSException {
        try {
            QueueConnection result = new BlazeJmsConnection(this.groupChannelFactory.createGroupChannel(NAME_GENERATOR
                    .generateId()));
            PropertyUtil.setProperties(result, this.props);
            return result;
        } catch (Exception e) {
            throw BlazeJmsExceptionSupport.create(e);
        }
    }

    /**
     * Get the consumerMaxDispatchQueueDepth
     *
     * @return the consumerMaxDispatchQueueDepth
     */
    public int getConsumerMaxDispatchQueueDepth() {
        return consumerMaxDispatchQueueDepth;
    }

    /**
     * Set the consumerMaxDispatchQueueDepth
     *
     * @param consumerMaxDispatchQueueDepth the consumerMaxDispatchQueueDepth to set
     */
    public void setConsumerMaxDispatchQueueDepth(int consumerMaxDispatchQueueDepth) {
        this.consumerMaxDispatchQueueDepth = consumerMaxDispatchQueueDepth;
    }
}
