Package org.codehaus.activemq.jndi

Source Code of org.codehaus.activemq.jndi.ActiveMQInitialContextFactory

/**
*
* 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.codehaus.activemq.jndi;

import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
import org.codehaus.activemq.ActiveMQConnectionFactory;
import org.codehaus.activemq.broker.Broker;
import org.codehaus.activemq.message.ActiveMQQueue;
import org.codehaus.activemq.message.ActiveMQTopic;

import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.Topic;
import javax.naming.CommunicationException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.spi.InitialContextFactory;
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;

/**
* 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.4 $
*/
public class ActiveMQInitialContextFactory implements InitialContextFactory {
    protected static final String[] defaultConnectionFactoryNames = {
        "ConnectionFactory", "QueueConnectionFactory", "TopicConnectionFactory"
    };

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

    public Context getInitialContext(Hashtable environment) throws NamingException {
        // lets create a factory
        Map data = new ConcurrentHashMap();
        ActiveMQConnectionFactory factory = createConnectionFactory(environment);

        String[] names = getConnectionFactoryNames(environment);
        for (int i = 0; i < names.length; i++) {
            String name = names[i];
            data.put(name, factory);
        }
       
        // Bind jms/DURABLE_SUB_CONNECTION_FACTORY to be a ConnectionFactory that can be used for durable subscriptions.
        ActiveMQConnectionFactory durableFactory = createDurableConnectionFactory(environment);
        Map jmsContext = new ConcurrentHashMap();
        jmsContext.put("DURABLE_SUB_CONNECTION_FACTORY", durableFactory);
        data.put("jms", new ReadOnlyContext(environment, jmsContext));

        createQueues(data, environment);
        createTopics(data, environment);

        try {
            Broker broker = factory.getEmbeddedBroker();
            if (broker != null) {
                data.put("destinations", broker.getDestinationContext(environment));
            }
        }
        catch (JMSException e) {
            CommunicationException exception = new CommunicationException("Failed to access embedded broker: " + e);
            exception.setRootCause(e);
            throw exception;
        }
        return new ReadOnlyContext(environment, data);
    }

    // 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()));
            }
        }
    }

    protected ActiveMQConnectionFactory createDurableConnectionFactory(Hashtable environment) {
        ActiveMQConnectionFactory answer = createConnectionFactory(environment);
        if (answer.getClientID() == null || answer.getClientID().length() == 0) {
            answer.setClientID("clientid");
        }
        return answer;
    }

    /**
     * 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;
    }
}
TOP

Related Classes of org.codehaus.activemq.jndi.ActiveMQInitialContextFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.