001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.activemq.state; 019 020import java.util.ArrayList; 021import java.util.Collection; 022import java.util.Collections; 023import java.util.Iterator; 024import java.util.List; 025import java.util.Map; 026import java.util.Set; 027import java.util.concurrent.ConcurrentHashMap; 028import java.util.concurrent.ConcurrentMap; 029import java.util.concurrent.atomic.AtomicBoolean; 030 031import org.apache.activemq.command.ActiveMQDestination; 032import org.apache.activemq.command.ConnectionInfo; 033import org.apache.activemq.command.ConsumerId; 034import org.apache.activemq.command.ConsumerInfo; 035import org.apache.activemq.command.DestinationInfo; 036import org.apache.activemq.command.SessionId; 037import org.apache.activemq.command.SessionInfo; 038import org.apache.activemq.command.TransactionId; 039 040public class ConnectionState { 041 042 ConnectionInfo info; 043 private final ConcurrentMap<TransactionId, TransactionState> transactions = new ConcurrentHashMap<TransactionId, TransactionState>(); 044 private final ConcurrentMap<SessionId, SessionState> sessions = new ConcurrentHashMap<SessionId, SessionState>(); 045 private final List<DestinationInfo> tempDestinations = Collections.synchronizedList(new ArrayList<DestinationInfo>()); 046 private final AtomicBoolean shutdown = new AtomicBoolean(false); 047 private boolean connectionInterruptProcessingComplete = true; 048 private ConcurrentMap<ConsumerId, ConsumerInfo> recoveringPullConsumers; 049 050 public ConnectionState(ConnectionInfo info) { 051 this.info = info; 052 // Add the default session id. 053 addSession(new SessionInfo(info, -1)); 054 } 055 056 @Override 057 public String toString() { 058 return info.toString(); 059 } 060 061 public void reset(ConnectionInfo info) { 062 this.info = info; 063 transactions.clear(); 064 sessions.clear(); 065 tempDestinations.clear(); 066 shutdown.set(false); 067 // Add the default session id. 068 addSession(new SessionInfo(info, -1)); 069 } 070 071 public void addTempDestination(DestinationInfo info) { 072 checkShutdown(); 073 tempDestinations.add(info); 074 } 075 076 public void removeTempDestination(ActiveMQDestination destination) { 077 for (Iterator<DestinationInfo> iter = tempDestinations.iterator(); iter.hasNext();) { 078 DestinationInfo di = iter.next(); 079 if (di.getDestination().equals(destination)) { 080 iter.remove(); 081 } 082 } 083 } 084 085 public void addTransactionState(TransactionId id) { 086 checkShutdown(); 087 transactions.put(id, new TransactionState(id)); 088 } 089 090 public TransactionState getTransactionState(TransactionId id) { 091 return transactions.get(id); 092 } 093 094 public Collection<TransactionState> getTransactionStates() { 095 return transactions.values(); 096 } 097 098 public TransactionState removeTransactionState(TransactionId id) { 099 return transactions.remove(id); 100 } 101 102 public void addSession(SessionInfo info) { 103 checkShutdown(); 104 sessions.put(info.getSessionId(), new SessionState(info)); 105 } 106 107 public SessionState removeSession(SessionId id) { 108 return sessions.remove(id); 109 } 110 111 public SessionState getSessionState(SessionId id) { 112 return sessions.get(id); 113 } 114 115 public ConnectionInfo getInfo() { 116 return info; 117 } 118 119 public Set<SessionId> getSessionIds() { 120 return sessions.keySet(); 121 } 122 123 public List<DestinationInfo> getTempDestinations() { 124 return tempDestinations; 125 } 126 127 public Collection<SessionState> getSessionStates() { 128 return sessions.values(); 129 } 130 131 private void checkShutdown() { 132 if (shutdown.get()) { 133 throw new IllegalStateException("Disposed"); 134 } 135 } 136 137 public void shutdown() { 138 if (shutdown.compareAndSet(false, true)) { 139 for (Iterator<SessionState> iter = sessions.values().iterator(); iter.hasNext();) { 140 SessionState ss = iter.next(); 141 ss.shutdown(); 142 } 143 } 144 } 145 146 public Map<ConsumerId, ConsumerInfo> getRecoveringPullConsumers() { 147 if (recoveringPullConsumers == null) { 148 recoveringPullConsumers = new ConcurrentHashMap<ConsumerId, ConsumerInfo>(); 149 } 150 return recoveringPullConsumers; 151 } 152 153 public void setConnectionInterruptProcessingComplete(boolean connectionInterruptProcessingComplete) { 154 this.connectionInterruptProcessingComplete = connectionInterruptProcessingComplete; 155 } 156 157 public boolean isConnectionInterruptProcessingComplete() { 158 return connectionInterruptProcessingComplete; 159 } 160}