View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq.jndi;
    19
    20  import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
    21  import org.codehaus.activemq.ActiveMQConnectionFactory;
    22  import org.codehaus.activemq.broker.Broker;
    23  import org.codehaus.activemq.message.ActiveMQQueue;
    24  import org.codehaus.activemq.message.ActiveMQTopic;
    25
    26  import javax.jms.ConnectionFactory;
    27  import javax.jms.JMSException;
    28  import javax.jms.Queue;
    29  import javax.jms.Topic;
    30  import javax.naming.CommunicationException;
    31  import javax.naming.Context;
    32  import javax.naming.NamingException;
    33  import javax.naming.spi.InitialContextFactory;
    34  import java.util.Hashtable;
    35  import java.util.Iterator;
    36  import java.util.Map;
    37  import java.util.Properties;
    38
    39  /***
    40   * A factory of the ActiveMQ InitialContext which contains {@link ConnectionFactory}
    41   * instances as well as a child context called <i>destinations</i> which contain all of the
    42   * current active destinations, in child context depending on the QoS such as
    43   * transient or durable and queue or topic.
    44   *
    45   * @version $Revision: 1.3 $
    46   */
    47  public class ActiveMQInitialContextFactory implements InitialContextFactory {
    48      private String queuePrefix = "queue.";
    49      private String topicPrefix = "topic.";
    50
    51      public Context getInitialContext(Hashtable environment) throws NamingException {
    52          // lets create a factory
    53          Map data = new ConcurrentHashMap();
    54          ActiveMQConnectionFactory factory = createConnectionFactory(environment);
    55          data.put("ConnectionFactory", factory);
    56          data.put("QueueConnectionFactory", factory);
    57          data.put("TopicConnectionFactory", factory);
    58
    59          createQueues(data, environment);
    60          createTopics(data, environment);
    61
    62          try {
    63              Broker broker = factory.getEmbeddedBroker();
    64              if (broker != null) {
    65                  data.put("destinations", broker.getDestinationContext(environment));
    66              }
    67          }
    68          catch (JMSException e) {
    69              CommunicationException exception = new CommunicationException("Failed to access embedded broker: " + e);
    70              exception.setRootCause(e);
    71              throw exception;
    72          }
    73          return new ReadOnlyContext(environment, data);
    74      }
    75
    76      // Properties
    77      //-------------------------------------------------------------------------
    78      public String getTopicPrefix() {
    79          return topicPrefix;
    80      }
    81
    82      public void setTopicPrefix(String topicPrefix) {
    83          this.topicPrefix = topicPrefix;
    84      }
    85
    86      public String getQueuePrefix() {
    87          return queuePrefix;
    88      }
    89
    90      public void setQueuePrefix(String queuePrefix) {
    91          this.queuePrefix = queuePrefix;
    92      }
    93
    94      // Implementation methods
    95      //-------------------------------------------------------------------------
    96      protected void createQueues(Map data, Hashtable environment) {
    97          for (Iterator iter = environment.entrySet().iterator(); iter.hasNext();) {
    98              Map.Entry entry = (Map.Entry) iter.next();
    99              String key = entry.getKey().toString();
    100             if (key.startsWith(queuePrefix)) {
    101                 String jndiName = key.substring(queuePrefix.length());
    102                 data.put(jndiName, createQueue(entry.getValue().toString()));
    103             }
    104         }
    105     }
    106
    107     protected void createTopics(Map data, Hashtable environment) {
    108         for (Iterator iter = environment.entrySet().iterator(); iter.hasNext();) {
    109             Map.Entry entry = (Map.Entry) iter.next();
    110             String key = entry.getKey().toString();
    111             if (key.startsWith(topicPrefix)) {
    112                 String jndiName = key.substring(topicPrefix.length());
    113                 data.put(jndiName, createTopic(entry.getValue().toString()));
    114             }
    115         }
    116     }
    117
    118     /***
    119      * Factory method to create new Queue instances
    120      */
    121     protected Queue createQueue(String name) {
    122         return new ActiveMQQueue(name);
    123     }
    124
    125     /***
    126      * Factory method to create new Topic instances
    127      */
    128     protected Topic createTopic(String name) {
    129         return new ActiveMQTopic(name);
    130     }
    131
    132     /***
    133      * Factory method to create a new connection factory from the given environment
    134      */
    135     protected ActiveMQConnectionFactory createConnectionFactory(Hashtable environment) {
    136         ActiveMQConnectionFactory answer = new ActiveMQConnectionFactory();
    137         Properties properties = new Properties();
    138         properties.putAll(environment);
    139         answer.setProperties(properties);
    140         return answer;
    141     }
    142 }