/** 
 * 
 * Copyright 2004 Protique Ltd
 * 
 * Licensed 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.codehaus.activemq.transport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.activemq.broker.BrokerContainer;
import org.codehaus.activemq.service.Service;
import EDU.oswego.cs.dl.util.concurrent.BoundedBuffer;
import EDU.oswego.cs.dl.util.concurrent.Executor;
import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;

import javax.jms.JMSException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Represents a connector to one or more remote brokers.
 * This class manages a number of {@link NetworkChannel} instances
 * which may or may not be connected to a
 * remote broker at any point in time.
 * <p/>
 * The implementation of this class could use a fixed number of
 * configured {@link NetworkChannel} instances or could use
 * discovery to find them.
 *
 * @version $Revision: 1.6 $
 */
public class NetworkConnector implements Service {
    private static final Log log = LogFactory.getLog(NetworkConnector.class);

    private BrokerContainer brokerContainer;
    private TransportChannelListener transportChannelListener;
    private List networkChannels = new ArrayList();
    private Map localDetails = new HashMap();
    private String remoteUserName;
    private String remotePassword;
    protected PooledExecutor threadPool;
    
    
    public NetworkConnector(BrokerContainer brokerContainer) {
        this.brokerContainer = brokerContainer;
        this.threadPool = new PooledExecutor();
    }

    public void start() throws JMSException {
        for (Iterator iter = networkChannels.iterator(); iter.hasNext();) {
            NetworkChannel networkChannel = (NetworkChannel) iter.next();
            networkChannel.setBrokerContainer(getBrokerContainer());
            networkChannel.setThreadPool(threadPool);
            networkChannel.start();
        }
    }

    public void stop() throws JMSException {
        for (Iterator iter = networkChannels.iterator(); iter.hasNext();) {
            NetworkChannel networkChannel = (NetworkChannel) iter.next();
            try {
                networkChannel.stop();
            }
            catch (JMSException e) {
                log.warn("Failed to stop network channel: " + e, e);
            }
        }
    }

    public void setTransportChannelListener(TransportChannelListener listener) {
        this.transportChannelListener = listener;
    }
    
    


    // Properties
    //-------------------------------------------------------------------------
    
    public BrokerContainer getBrokerContainer() {
        return brokerContainer;
    }
    

    /**
     * @return Returns the threadPool.
     */
    public PooledExecutor getThreadPool() {
        return threadPool;
    }
   

    public List getNetworkChannels() {
        return networkChannels;
    }
    
//  Properties
    //-------------------------------------------------------------------------
    public Map getLocalDetails() {
        return localDetails;
    }

    public void setLocalDetails(Map localDetails) {
        this.localDetails = localDetails;
    }

    public String getRemotePassword() {
        return remotePassword;
    }

    public void setRemotePassword(String remotePassword) {
        this.remotePassword = remotePassword;
    }

    public String getRemoteUserName() {
        return remoteUserName;
    }

    public void setRemoteUserName(String remoteUserName) {
        this.remoteUserName = remoteUserName;
    }


    /**
     * Sets a list of {@link NetworkChannel} instances
     *
     * @param networkChannels
     */
    public void setNetworkChannels(List networkChannels) {
        this.networkChannels = networkChannels;
    }

    /**
     * Adds a new network channel for the given URI
     *
     * @param uri
     * @return
     */
    public NetworkChannel addNetworkChannel(String uri) {
        NetworkChannel networkChannel = createNetworkChannel(uri);
        addNetworkChannel(networkChannel);
        return networkChannel;
    }
    

    /**
     * Adds a new network channel
     */
    public void addNetworkChannel(NetworkChannel networkChannel) {
        networkChannels.add(networkChannel);
    }

    /**
     * Removes a network channel
     */
    public void removeNetworkChannel(NetworkChannel networkChannel) {
        networkChannels.remove(networkChannel);
    }
    
    /**
     * Create a channel from the url
     * @param url
     * @return
     */
    protected NetworkChannel createNetworkChannel(String url) {
        NetworkChannel answer = new NetworkChannel(this,getBrokerContainer(), url);
        answer.setRemoteUserName(getRemoteUserName());
        answer.setRemotePassword(getRemotePassword());
        return answer;
    }

}
