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    
018    package org.apache.activemq.state;
019    
020    import java.util.ArrayList;
021    import java.util.Collection;
022    import java.util.Collections;
023    import java.util.Iterator;
024    import java.util.List;
025    import java.util.Set;
026    import java.util.concurrent.ConcurrentHashMap;
027    import java.util.concurrent.atomic.AtomicBoolean;
028    
029    import org.apache.activemq.command.ActiveMQDestination;
030    import org.apache.activemq.command.ConnectionInfo;
031    import org.apache.activemq.command.DestinationInfo;
032    import org.apache.activemq.command.SessionId;
033    import org.apache.activemq.command.SessionInfo;
034    import org.apache.activemq.command.TransactionId;
035    
036    public class ConnectionState {
037    
038        ConnectionInfo info;
039        private final ConcurrentHashMap<TransactionId, TransactionState> transactions = new ConcurrentHashMap<TransactionId, TransactionState>();
040        private final ConcurrentHashMap<SessionId, SessionState> sessions = new ConcurrentHashMap<SessionId, SessionState>();
041        private final List<DestinationInfo> tempDestinations = Collections.synchronizedList(new ArrayList<DestinationInfo>());
042        private final AtomicBoolean shutdown = new AtomicBoolean(false);
043    
044        public ConnectionState(ConnectionInfo info) {
045            this.info = info;
046            // Add the default session id.
047            addSession(new SessionInfo(info, -1));
048        }
049    
050        public String toString() {
051            return info.toString();
052        }
053    
054        public void reset(ConnectionInfo info) {
055            this.info = info;
056            transactions.clear();
057            sessions.clear();
058            tempDestinations.clear();
059            shutdown.set(false);
060            // Add the default session id.
061            addSession(new SessionInfo(info, -1));
062        }
063    
064        public void addTempDestination(DestinationInfo info) {
065            checkShutdown();
066            tempDestinations.add(info);
067        }
068    
069        public void removeTempDestination(ActiveMQDestination destination) {
070            for (Iterator<DestinationInfo> iter = tempDestinations.iterator(); iter.hasNext();) {
071                DestinationInfo di = iter.next();
072                if (di.getDestination().equals(destination)) {
073                    iter.remove();
074                }
075            }
076        }
077    
078        public void addTransactionState(TransactionId id) {
079            checkShutdown();
080            transactions.put(id, new TransactionState(id));
081        }
082    
083        public TransactionState getTransactionState(TransactionId id) {
084            return transactions.get(id);
085        }
086    
087        public Collection<TransactionState> getTransactionStates() {
088            return transactions.values();
089        }
090    
091        public TransactionState removeTransactionState(TransactionId id) {
092            return transactions.remove(id);
093        }
094    
095        public void addSession(SessionInfo info) {
096            checkShutdown();
097            sessions.put(info.getSessionId(), new SessionState(info));
098        }
099    
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    }