/** 
 * 
 * Copyright 2004 Protique Ltd
 * 
 * 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 org.activemq.jndi;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;

import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.Topic;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.spi.InitialContextFactory;

import org.activemq.ActiveMQConnectionFactory;
import org.activemq.broker.Broker;
import org.activemq.message.ActiveMQQueue;
import org.activemq.message.ActiveMQTopic;

import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;

/**
 * A factory of the ActiveMQ InitialContext which contains {@link ConnectionFactory}
 * instances as well as a child context called <i>destinations</i> which contain all of the
 * current active destinations, in child context depending on the QoS such as
 * transient or durable and queue or topic.
 *
 * @version $Revision: 1.1.1.1 $
 */
public class ActiveMQInitialContextFactory implements InitialContextFactory {
    protected static final String[] defaultConnectionFactoryNames = {
        "ConnectionFactory", "QueueConnectionFactory", "TopicConnectionFactory"
    };

    private String connectionPrefix = "connection.";
    private String queuePrefix = "queue.";
    private String topicPrefix = "topic.";

    public Context getInitialContext(Hashtable environment) throws NamingException {
        // lets create a factory
        Map data = new ConcurrentHashMap();
        Broker broker = null;
        
        String[] names = getConnectionFactoryNames(environment);
        for (int i = 0; i < names.length; i++) {
            
            String name = names[i];
            ActiveMQConnectionFactory factory = createConnectionFactory(name, environment);
            
            if( broker==null ) {
                try {
                    broker = factory.getEmbeddedBroker();
                }
                catch (JMSException ignore) {
                }
            }
            data.put(name,factory);
        }
                
        createQueues(data, environment);
        createTopics(data, environment);
        if (broker != null) {
            data.put("destinations", broker.getDestinationContext(environment));
        }
        
        return new ReadOnlyContext(environment, data);
    }

    private ActiveMQConnectionFactory createConnectionFactory(String name, Hashtable environment) {
        Hashtable temp = new Hashtable(environment);
        String prefix = connectionPrefix+name+".";
        for (Iterator iter = environment.keySet().iterator(); iter.hasNext();) {
            String key = (String) iter.next();
            if( key.startsWith(prefix) ) {
                Object value = environment.get(key);
                // Rename the key...
                temp.remove(key);
                key = key.substring(prefix.length());
                temp.put(key, value);
            }
        }
        return createConnectionFactory(temp);
    }

    // Properties
    //-------------------------------------------------------------------------
    public String getTopicPrefix() {
        return topicPrefix;
    }

    public void setTopicPrefix(String topicPrefix) {
        this.topicPrefix = topicPrefix;
    }

    public String getQueuePrefix() {
        return queuePrefix;
    }

    public void setQueuePrefix(String queuePrefix) {
        this.queuePrefix = queuePrefix;
    }

    // Implementation methods
    //-------------------------------------------------------------------------
    protected String[] getConnectionFactoryNames(Map environment) {
        String factoryNames = (String) environment.get("connectionFactoryNames");
        if (factoryNames != null) {
            List list = new ArrayList();
            for (StringTokenizer enumeration = new StringTokenizer(factoryNames, ","); enumeration.hasMoreTokens();) {
                list.add(enumeration.nextToken().trim());
            }
            int size = list.size();
            if (size > 0) {
                String[] answer = new String[size];
                list.toArray(answer);
                return answer;
            }
        }
        return defaultConnectionFactoryNames;
    }

    protected void createQueues(Map data, Hashtable environment) {
        for (Iterator iter = environment.entrySet().iterator(); iter.hasNext();) {
            Map.Entry entry = (Map.Entry) iter.next();
            String key = entry.getKey().toString();
            if (key.startsWith(queuePrefix)) {
                String jndiName = key.substring(queuePrefix.length());
                data.put(jndiName, createQueue(entry.getValue().toString()));
            }
        }
    }

    protected void createTopics(Map data, Hashtable environment) {
        for (Iterator iter = environment.entrySet().iterator(); iter.hasNext();) {
            Map.Entry entry = (Map.Entry) iter.next();
            String key = entry.getKey().toString();
            if (key.startsWith(topicPrefix)) {
                String jndiName = key.substring(topicPrefix.length());
                data.put(jndiName, createTopic(entry.getValue().toString()));
            }
        }
    }

    /**
     * Factory method to create new Queue instances
     */
    protected Queue createQueue(String name) {
        return new ActiveMQQueue(name);
    }

    /**
     * Factory method to create new Topic instances
     */
    protected Topic createTopic(String name) {
        return new ActiveMQTopic(name);
    }
	
    /**
     * Factory method to create a new connection factory from the given environment
     */
    protected ActiveMQConnectionFactory createConnectionFactory(Hashtable environment) {
        ActiveMQConnectionFactory answer = new ActiveMQConnectionFactory();
        Properties properties = new Properties();
        properties.putAll(environment);
        answer.setProperties(properties);
        return answer;
    }

    public String getConnectionPrefix() {
        return connectionPrefix;
    }
    

    public void setConnectionPrefix(String connectionPrefix) {
        this.connectionPrefix = connectionPrefix;
    }
    
}
