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.Iterator;
022    import java.util.List;
023    import java.util.Map;
024    import java.util.Map.Entry;
025    
026    import org.apache.activemq.command.ConnectionId;
027    import org.apache.activemq.command.ConsumerId;
028    import org.apache.activemq.command.ProducerId;
029    import org.apache.activemq.command.SessionId;
030    
031    /**
032     * 
033     */
034    
035    public class SingleTransportConnectionStateRegister  implements TransportConnectionStateRegister{
036    
037        private  TransportConnectionState connectionState;
038        private ConnectionId connectionId;
039    
040        public TransportConnectionState registerConnectionState(ConnectionId connectionId,
041                                                                   TransportConnectionState state) {
042            TransportConnectionState rc = connectionState;
043            connectionState = state;
044            this.connectionId = connectionId;
045            return rc;
046        }
047    
048        public synchronized TransportConnectionState unregisterConnectionState(ConnectionId connectionId) {
049            TransportConnectionState rc = null;
050            
051           
052            if (connectionId != null && connectionState != null && this.connectionId!=null){
053            if (this.connectionId.equals(connectionId)){
054                            rc = connectionState;
055                            connectionState = null;
056                            connectionId = null;
057                    }
058            }
059            return rc;
060        }
061    
062        public synchronized List<TransportConnectionState> listConnectionStates() {
063            List<TransportConnectionState> rc = new ArrayList<TransportConnectionState>();
064            if (connectionState != null) {
065                rc.add(connectionState);
066            }
067            return rc;
068        }
069    
070        public synchronized TransportConnectionState lookupConnectionState(String connectionId) {
071            TransportConnectionState cs = connectionState;
072            if (cs == null) {
073                throw new IllegalStateException(
074                                                "Cannot lookup a connectionId for a connection that had not been registered: "
075                                                    + connectionId);
076            }
077            return cs;
078        }
079    
080        public synchronized TransportConnectionState lookupConnectionState(ConsumerId id) {
081            TransportConnectionState cs = connectionState;
082            if (cs == null) {
083                throw new IllegalStateException(
084                                                "Cannot lookup a consumer from a connection that had not been registered: "
085                                                    + id.getParentId().getParentId());
086            }
087            return cs;
088        }
089    
090        public synchronized TransportConnectionState lookupConnectionState(ProducerId id) {
091            TransportConnectionState cs = connectionState;
092            if (cs == null) {
093                throw new IllegalStateException(
094                                                "Cannot lookup a producer from a connection that had not been registered: "
095                                                    + id.getParentId().getParentId());
096            }
097            return cs;
098        }
099    
100        public synchronized TransportConnectionState lookupConnectionState(SessionId id) {
101            TransportConnectionState cs = connectionState;
102            if (cs == null) {
103                throw new IllegalStateException(
104                                                "Cannot lookup a session from a connection that had not been registered: "
105                                                    + id.getParentId());
106            }
107            return cs;
108        }
109    
110        public synchronized TransportConnectionState lookupConnectionState(ConnectionId connectionId) {
111            TransportConnectionState cs = connectionState;
112            return cs;
113        }
114    
115            public synchronized boolean doesHandleMultipleConnectionStates() {
116                    return false;
117            }
118    
119            public synchronized boolean isEmpty() {
120                    return connectionState == null;
121            }
122    
123            public void intialize(TransportConnectionStateRegister other) {
124                    
125                    if (other.isEmpty()){
126                            clear();
127                    }else{
128                            Map map = other.mapStates();
129                            Iterator i = map.entrySet().iterator();
130                            Map.Entry<ConnectionId, TransportConnectionState> entry = (Entry<ConnectionId, TransportConnectionState>) i.next();
131                            connectionId = entry.getKey();
132                            connectionState =entry.getValue();
133                    }
134                    
135            }
136    
137            public Map<ConnectionId, TransportConnectionState> mapStates() {
138                    Map<ConnectionId, TransportConnectionState> map = new HashMap<ConnectionId, TransportConnectionState>();
139                    if (!isEmpty()) {
140                            map.put(connectionId, connectionState);
141                    }
142                    return map;
143            }
144    
145            public void clear() {
146                    connectionState=null;
147                    connectionId=null;
148                    
149            }
150    
151    }