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     * @version $Revision: 1.8 $
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            return rc;
047        }
048    
049        public List<TransportConnectionState> listConnectionStates() {
050            
051            List<TransportConnectionState> rc = new ArrayList<TransportConnectionState>();
052            rc.addAll(connectionStates.values());
053            return rc;
054        }
055    
056        public TransportConnectionState lookupConnectionState(String connectionId) {
057            return connectionStates.get(new ConnectionId(connectionId));
058        }
059    
060        public TransportConnectionState lookupConnectionState(ConsumerId id) {
061            TransportConnectionState cs = lookupConnectionState(id.getConnectionId());
062            if (cs == null) {
063                throw new IllegalStateException(
064                                                "Cannot lookup a consumer from a connection that had not been registered: "
065                                                    + id.getParentId().getParentId());
066            }
067            return cs;
068        }
069    
070        public TransportConnectionState lookupConnectionState(ProducerId id) {
071             TransportConnectionState cs = lookupConnectionState(id.getConnectionId());
072            if (cs == null) {
073                throw new IllegalStateException(
074                                                "Cannot lookup a producer from a connection that had not been registered: "
075                                                    + id.getParentId().getParentId());
076            }
077            return cs;
078        }
079    
080        public TransportConnectionState lookupConnectionState(SessionId id) {
081             TransportConnectionState cs = lookupConnectionState(id.getConnectionId());
082            if (cs == null) {
083                throw new IllegalStateException(
084                                                "Cannot lookup a session from a connection that had not been registered: "
085                                                    + id.getParentId());
086            }
087            return cs;
088        }
089    
090        public TransportConnectionState lookupConnectionState(ConnectionId connectionId) {
091            TransportConnectionState cs = connectionStates.get(connectionId);
092            if (cs == null) {
093                throw new IllegalStateException("Cannot lookup a connection that had not been registered: "
094                                                + connectionId);
095            }
096            return cs;
097        }
098    
099            
100    
101            public boolean doesHandleMultipleConnectionStates() {
102                    return true;
103            }
104    
105            public boolean isEmpty() {
106                    return connectionStates.isEmpty();
107            }
108    
109            public void clear() {
110                    connectionStates.clear();
111                    
112            }
113    
114            public void intialize(TransportConnectionStateRegister other) {
115                    connectionStates.clear();
116                    connectionStates.putAll(other.mapStates());
117                    
118            }
119    
120            public Map<ConnectionId, TransportConnectionState> mapStates() {
121                    HashMap<ConnectionId, TransportConnectionState> map = new HashMap<ConnectionId, TransportConnectionState>(connectionStates);
122                    return map;
123            }
124    
125    }