Home » activemq-parent-5.3.1-source-release » org.apache » activemq » state » [javadoc | source]

    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  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.apache.activemq.state;
   19   
   20   import java.util.ArrayList;
   21   import java.util.Collection;
   22   import java.util.Collections;
   23   import java.util.Iterator;
   24   import java.util.List;
   25   import java.util.Set;
   26   import java.util.concurrent.ConcurrentHashMap;
   27   import java.util.concurrent.atomic.AtomicBoolean;
   28   
   29   import org.apache.activemq.command.ActiveMQDestination;
   30   import org.apache.activemq.command.ConnectionInfo;
   31   import org.apache.activemq.command.DestinationInfo;
   32   import org.apache.activemq.command.SessionId;
   33   import org.apache.activemq.command.SessionInfo;
   34   import org.apache.activemq.command.TransactionId;
   35   
   36   public class ConnectionState {
   37   
   38       ConnectionInfo info;
   39       private final ConcurrentHashMap<TransactionId, TransactionState> transactions = new ConcurrentHashMap<TransactionId, TransactionState>();
   40       private final ConcurrentHashMap<SessionId, SessionState> sessions = new ConcurrentHashMap<SessionId, SessionState>();
   41       private final List<DestinationInfo> tempDestinations = Collections.synchronizedList(new ArrayList<DestinationInfo>());
   42       private final AtomicBoolean shutdown = new AtomicBoolean(false);
   43   
   44       public ConnectionState(ConnectionInfo info) {
   45           this.info = info;
   46           // Add the default session id.
   47           addSession(new SessionInfo(info, -1));
   48       }
   49   
   50       public String toString() {
   51           return info.toString();
   52       }
   53   
   54       public void reset(ConnectionInfo info) {
   55           this.info = info;
   56           transactions.clear();
   57           sessions.clear();
   58           tempDestinations.clear();
   59           shutdown.set(false);
   60           // Add the default session id.
   61           addSession(new SessionInfo(info, -1));
   62       }
   63   
   64       public void addTempDestination(DestinationInfo info) {
   65           checkShutdown();
   66           tempDestinations.add(info);
   67       }
   68   
   69       public void removeTempDestination(ActiveMQDestination destination) {
   70           for (Iterator<DestinationInfo> iter = tempDestinations.iterator(); iter.hasNext();) {
   71               DestinationInfo di = iter.next();
   72               if (di.getDestination().equals(destination)) {
   73                   iter.remove();
   74               }
   75           }
   76       }
   77   
   78       public void addTransactionState(TransactionId id) {
   79           checkShutdown();
   80           transactions.put(id, new TransactionState(id));
   81       }
   82   
   83       public TransactionState getTransactionState(TransactionId id) {
   84           return transactions.get(id);
   85       }
   86   
   87       public Collection<TransactionState> getTransactionStates() {
   88           return transactions.values();
   89       }
   90   
   91       public TransactionState removeTransactionState(TransactionId id) {
   92           return transactions.remove(id);
   93       }
   94   
   95       public void addSession(SessionInfo info) {
   96           checkShutdown();
   97           sessions.put(info.getSessionId(), new SessionState(info));
   98       }
   99   
  100       public SessionState removeSession(SessionId id) {
  101           return sessions.remove(id);
  102       }
  103   
  104       public SessionState getSessionState(SessionId id) {
  105           return sessions.get(id);
  106       }
  107   
  108       public ConnectionInfo getInfo() {
  109           return info;
  110       }
  111   
  112       public Set<SessionId> getSessionIds() {
  113           return sessions.keySet();
  114       }
  115   
  116       public List<DestinationInfo> getTempDesinations() {
  117           return tempDestinations;
  118       }
  119   
  120       public Collection<SessionState> getSessionStates() {
  121           return sessions.values();
  122       }
  123   
  124       private void checkShutdown() {
  125           if (shutdown.get()) {
  126               throw new IllegalStateException("Disposed");
  127           }
  128       }
  129   
  130       public void shutdown() {
  131           if (shutdown.compareAndSet(false, true)) {
  132               for (Iterator<SessionState> iter = sessions.values().iterator(); iter.hasNext();) {
  133                   SessionState ss = iter.next();
  134                   ss.shutdown();
  135               }
  136           }
  137       }
  138   }

Home » activemq-parent-5.3.1-source-release » org.apache » activemq » state » [javadoc | source]