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.service.impl;
    19
    20  import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
    21  import org.codehaus.activemq.DuplicateDurableSubscriptionException;
    22  import org.codehaus.activemq.broker.BrokerClient;
    23  import org.codehaus.activemq.filter.Filter;
    24  import org.codehaus.activemq.message.ConsumerInfo;
    25  import org.codehaus.activemq.service.Dispatcher;
    26  import org.codehaus.activemq.service.Subscription;
    27  import org.codehaus.activemq.service.SubscriptionContainer;
    28
    29  import javax.jms.JMSException;
    30  import java.util.Iterator;
    31  import java.util.Map;
    32
    33  /***
    34   * A default RAM only implementation of the {@link SubscriptionContainer}
    35   *
    36   * @version $Revision: 1.1 $
    37   */
    38  public class SubscriptionContainerImpl implements SubscriptionContainer {
    39      private Map subscriptions;
    40
    41      public SubscriptionContainerImpl() {
    42          this(new ConcurrentHashMap());
    43      }
    44
    45      public SubscriptionContainerImpl(Map subscriptions) {
    46          this.subscriptions = subscriptions;
    47      }
    48
    49      public String toString() {
    50          return super.toString() + "[size:" + subscriptions.size() + "]";
    51      }
    52
    53      public Subscription getSubscription(String consumerId) {
    54          return (Subscription) subscriptions.get(consumerId);
    55      }
    56
    57      public void addSubscription(String consumerId, Subscription subscription) {
    58      }
    59
    60      public Subscription removeSubscription(String consumerId) {
    61          return (Subscription) subscriptions.remove(consumerId);
    62      }
    63
    64      public Iterator subscriptionIterator() {
    65          return subscriptions.values().iterator();
    66      }
    67
    68      public Subscription makeSubscription(Dispatcher dispatcher, ConsumerInfo info, Filter filter) {
    69          Subscription subscription = createSubscription(dispatcher, info, filter);
    70          subscriptions.put(info.getConsumerId(), subscription);
    71          return subscription;
    72      }
    73
    74      public void checkForDuplicateDurableSubscription(BrokerClient client, ConsumerInfo info) throws JMSException {
    75          for (Iterator iter = subscriptions.values().iterator(); iter.hasNext();) {
    76              Subscription subscription = (Subscription) iter.next();
    77              if (subscription.isSameDurableSubscription(info)) {
    78                  throw new DuplicateDurableSubscriptionException(info);
    79              }
    80          }
    81      }
    82
    83      protected Subscription createSubscription(Dispatcher dispatcher, ConsumerInfo info, Filter filter) {
    84          return new SubscriptionImpl(dispatcher, info, filter);
    85      }
    86  }