All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.activemq.broker.SingleTransportConnectionStateRegister Maven / Gradle / Ivy

There is a newer version: 6.1.3
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.activemq.broker;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.activemq.command.ConnectionId;
import org.apache.activemq.command.ConsumerId;
import org.apache.activemq.command.ProducerId;
import org.apache.activemq.command.SessionId;

/**
 * 
 */

public class SingleTransportConnectionStateRegister  implements TransportConnectionStateRegister{

    private  TransportConnectionState connectionState;
    private ConnectionId connectionId;

    public TransportConnectionState registerConnectionState(ConnectionId connectionId,
                                                               TransportConnectionState state) {
        TransportConnectionState rc = connectionState;
        connectionState = state;
        this.connectionId = connectionId;
        return rc;
    }

    public synchronized TransportConnectionState unregisterConnectionState(ConnectionId connectionId) {
        TransportConnectionState rc = null;
        
       
        if (connectionId != null && connectionState != null && this.connectionId!=null){
        if (this.connectionId.equals(connectionId)){
			rc = connectionState;
			connectionState = null;
			connectionId = null;
		}
        }
        return rc;
    }

    public synchronized List listConnectionStates() {
        List rc = new ArrayList();
        if (connectionState != null) {
            rc.add(connectionState);
        }
        return rc;
    }

    public synchronized TransportConnectionState lookupConnectionState(String connectionId) {
        TransportConnectionState cs = connectionState;
        if (cs == null) {
            throw new IllegalStateException(
                                            "Cannot lookup a connectionId for a connection that had not been registered: "
                                                + connectionId);
        }
        return cs;
    }

    public synchronized TransportConnectionState lookupConnectionState(ConsumerId id) {
        TransportConnectionState cs = connectionState;
        if (cs == null) {
            throw new IllegalStateException(
                                            "Cannot lookup a consumer from a connection that had not been registered: "
                                                + id.getParentId().getParentId());
        }
        return cs;
    }

    public synchronized TransportConnectionState lookupConnectionState(ProducerId id) {
        TransportConnectionState cs = connectionState;
        if (cs == null) {
            throw new IllegalStateException(
                                            "Cannot lookup a producer from a connection that had not been registered: "
                                                + id.getParentId().getParentId());
        }
        return cs;
    }

    public synchronized TransportConnectionState lookupConnectionState(SessionId id) {
        TransportConnectionState cs = connectionState;
        if (cs == null) {
            throw new IllegalStateException(
                                            "Cannot lookup a session from a connection that had not been registered: "
                                                + id.getParentId());
        }
        return cs;
    }

    public synchronized TransportConnectionState lookupConnectionState(ConnectionId connectionId) {
        TransportConnectionState cs = connectionState;
        return cs;
    }

	public synchronized boolean doesHandleMultipleConnectionStates() {
		return false;
	}

	public synchronized boolean isEmpty() {
		return connectionState == null;
	}

	public void intialize(TransportConnectionStateRegister other) {
		
		if (other.isEmpty()){
			clear();
		}else{
			Map map = other.mapStates();
			Iterator i = map.entrySet().iterator();
			Map.Entry entry = (Entry) i.next();
			connectionId = entry.getKey();
			connectionState =entry.getValue();
		}
		
	}

	public Map mapStates() {
		Map map = new HashMap();
		if (!isEmpty()) {
			map.put(connectionId, connectionState);
		}
		return map;
	}

	public void clear() {
		connectionState=null;
		connectionId=null;
		
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy