/** 
 * 
 * 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.broker;

import org.codehaus.activemq.message.ActiveMQMessage;
import org.codehaus.activemq.transport.TransportChannel;

import javax.jms.JMSException;
import javax.security.auth.Subject;

import java.util.ArrayList;
import java.util.List;


/**
 * A mock dispatcher for testing
 *
 * @version $Revision: 1.2 $
 */
public class BrokerClientStub implements BrokerClient {

    private List messages = new ArrayList();
    private Object semaphore;
	private Subject subject;

    public BrokerClientStub() {
        this(new Object());
    }

    public BrokerClientStub(Object semaphore) {
        this.semaphore = semaphore;
    }

    /**
     * @return all the messages on the list so far, clearing the buffer
     */
    public synchronized List flushMessages() {
        List answer = new ArrayList(messages);
        messages.clear();
        return answer;
    }

    public synchronized void dispatch(ActiveMQMessage message) {
        messages.add(message);
        synchronized (semaphore) {
            semaphore.notifyAll();
        }
    }

    public void initialize(BrokerConnector brokerConnector, TransportChannel channel) {
    }

    public void waitForMessageToArrive() {
        System.out.println("Waiting for message to arrive");

        long start = System.currentTimeMillis();

        try {
            if (messages.isEmpty()) {
                synchronized (semaphore) {
                    semaphore.wait(4000);
                }
            }
        }
        catch (InterruptedException e) {
            System.out.println("Caught: " + e);
        }
        long end = System.currentTimeMillis() - start;

        System.out.println("End of wait for " + end + " millis");
    }

    public void start() throws JMSException {
    }

    public void stop() throws JMSException {
    }

    /**
     * @return true if the peer for this Client is itself another Broker
     */
    public boolean isBrokerConnection() {
        return false;
    }


    /**
     * Get the Capacity for in-progress messages at the peer (probably a JMSConnection)
     * Legimate values between 0-100. 100 representing that the peer cannot process
     * any more messages at the current time
     *
     * @return
     */
    public int getCapacity() {
        return 100;
    }

    /**
     * Get an indication if the peer should be considered as a slow consumer
     *
     * @return true id the peer should be considered as a slow consumer
     */
    public boolean isSlowConsumer() {
        return false;
    }

    /**
     * Update the peer Connection about the Broker's capacity for messages
     *
     * @param capacity
     */
    public void updateBrokerCapacity(int capacity) {

    }

    public String getClientID() {
        return "dummyClientID";
    }

    public void cleanUp() {
    }

    public TransportChannel getChannel() {
        return null;
    }

   
    public BrokerConnector getBrokerConnector() {
        return null;
    }

   
    public boolean isClusteredConnection() {
        return false;
    }

	public void setSubject(Subject subject) {
		this.subject = subject;
	}

	public Subject getSubject() {
		return subject;
	}
}
