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    package org.apache.activemq.broker;
018    
019    import java.util.ArrayList;
020    import java.util.HashMap;
021    import java.util.List;
022    import java.util.Map;
023    import java.util.concurrent.ConcurrentHashMap;
024    
025    import org.apache.activemq.command.ConnectionId;
026    import org.apache.activemq.command.ConsumerId;
027    import org.apache.activemq.command.ProducerId;
028    import org.apache.activemq.command.SessionId;
029    
030    /**
031     * 
032     */
033    
034    public class MapTransportConnectionStateRegister  implements TransportConnectionStateRegister{
035    
036        private  Map <ConnectionId,TransportConnectionState>connectionStates = new ConcurrentHashMap<ConnectionId,TransportConnectionState>();
037    
038        public TransportConnectionState registerConnectionState(ConnectionId connectionId,
039                                                                   TransportConnectionState state) {
040            TransportConnectionState rc = connectionStates.put(connectionId, state);
041            return rc;
042        }
043    
044        public TransportConnectionState unregisterConnectionState(ConnectionId connectionId) {
045            TransportConnectionState rc = connectionStates.remove(connectionId);
046            if (rc.getReferenceCounter().get() > 1) {
047                rc.decrementReference();
048                connectionStates.put(connectionId, rc);
049            }
050            return rc;
051        }
052    
053        public List<TransportConnectionState> listConnectionStates() {
054            
055            List<TransportConnectionState> rc = new ArrayList<TransportConnectionState>();
056            rc.addAll(connectionStates.values());
057            return rc;
058        }
059    
060        public TransportConnectionState lookupConnectionState(String connectionId) {
061            return connectionStates.get(new ConnectionId(connectionId));
062        }
063    
064        public TransportConnectionState lookupConnectionState(ConsumerId id) {
065            TransportConnectionState cs = lookupConnectionState(id.getConnectionId());
066            if (cs == null) {
067                throw new IllegalStateException(
068                                                "Cannot lookup a consumer from a connection that had not been registered: "
069                                                    + id.getParentId().getParentId());
070            }
071            return cs;
072        }
073    
074        public TransportConnectionState lookupConnectionState(ProducerId id) {
075             TransportConnectionState cs = lookupConnectionState(id.getConnectionId());
076            if (cs == null) {
077                throw new IllegalStateException(
078                                                "Cannot lookup a producer from a connection that had not been registered: "
079                                                    + id.getParentId().getParentId());
080            }
081            return cs;
082        }
083    
084        public TransportConnectionState lookupConnectionState(SessionId id) {
085             TransportConnectionState cs = lookupConnectionState(id.getConnectionId());
086            if (cs == null) {
087                throw new IllegalStateException(
088                                                "Cannot lookup a session from a connection that had not been registered: "
089                                                    + id.getParentId());
090            }
091            return cs;
092        }
093    
094        public TransportConnectionState lookupConnectionState(ConnectionId connectionId) {
095            TransportConnectionState cs = connectionStates.get(connectionId);
096            if (cs == null) {
097                throw new IllegalStateException("Cannot lookup a connection that had not been registered: "
098                                                + connectionId);
099            }
100            return cs;
101        }
102    
103            
104    
105            public boolean doesHandleMultipleConnectionStates() {
106                    return true;
107            }
108    
109            public boolean isEmpty() {
110                    return connectionStates.isEmpty();
111            }
112    
113            public void clear() {
114                    connectionStates.clear();
115                    
116            }
117    
118            public void intialize(TransportConnectionStateRegister other) {
119                    connectionStates.clear();
120                    connectionStates.putAll(other.mapStates());
121                    
122            }
123    
124            public Map<ConnectionId, TransportConnectionState> mapStates() {
125                    HashMap<ConnectionId, TransportConnectionState> map = new HashMap<ConnectionId, TransportConnectionState>(connectionStates);
126                    return map;
127            }
128    
129    }