    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq.broker;
    19
    20  import org.codehaus.activemq.message.ActiveMQMessage;
    21  import org.codehaus.activemq.transport.TransportChannel;
    22
    23  import javax.jms.JMSException;
    24  import java.util.ArrayList;
    25  import java.util.List;
    26
    27
    28  /***
    29   * A mock dispatcher for testing
    30   *
    31   * @version $Revision: 1.17 $
    32   */
    33  public class MockBrokerClient implements BrokerClient {
    34
    35      private List messages = new ArrayList();
    36      private Object semaphore;
    37
    38      public MockBrokerClient() {
    39          this(new Object());
    40      }
    41
    42      public MockBrokerClient(Object semaphore) {
    43          this.semaphore = semaphore;
    44      }
    45
    46      /***
    47       * @return all the messages on the list so far, clearing the buffer
    48       */
    49      public synchronized List flushMessages() {
    50          List answer = new ArrayList(messages);
    51          messages.clear();
    52          return answer;
    53      }
    54
    55      public synchronized void dispatch(ActiveMQMessage message) {
    56          messages.add(message);
    57          synchronized (semaphore) {
    58              semaphore.notifyAll();
    59          }
    60      }
    61
    62      public void initialize(BrokerConnector brokerConnector, TransportChannel channel) {
    63      }
    64
    65      public void waitForMessageToArrive() {
    66          System.out.println("Waiting for message to arrive");
    67
    68          long start = System.currentTimeMillis();
    69
    70          try {
    71              if (messages.isEmpty()) {
    72                  synchronized (semaphore) {
    73                      semaphore.wait(4000);
    74                  }
    75              }
    76          }
    77          catch (InterruptedException e) {
    78              System.out.println("Caught: " + e);
    79          }
    80          long end = System.currentTimeMillis() - start;
    81
    82          System.out.println("End of wait for " + end + " millis");
    83      }
    84
    85      public void start() throws JMSException {
    86      }
    87
    88      public void stop() throws JMSException {
    89      }
    90
    91      /***
    92       * @return true if the peer for this Client is itself another Broker
    93       */
    94      public boolean isBrokerConnection() {
    95          return false;
    96      }
    97
    98
    99      /***
    100      * Get the Capacity for in-progress messages at the peer (probably a JMSConnection)
    101      * Legimate values between 0-100. 100 representing that the peer cannot process
    102      * any more messages at the current time
    103      *
    104      * @return
    105      */
    106     public int getCapacity() {
    107         return 100;
    108     }
    109
    110     /***
    111      * Get an indication if the peer should be considered as a slow consumer
    112      *
    113      * @return true id the peer should be considered as a slow consumer
    114      */
    115     public boolean isSlowConsumer() {
    116         return false;
    117     }
    118
    119     /***
    120      * Update the peer Connection about the Broker's capacity for messages
    121      *
    122      * @param capacity
    123      */
    124     public void updateBrokerCapacity(int capacity) {
    125
    126     }
    127
    128     public String getClientID() {
    129         return "dummyClientID";
    130     }
    131
    132     public void cleanUp() {
    133     }
    134
    135     public TransportChannel getChannel() {
    136         return null;
    137     }
    138 }