View Javadoc

    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.transport;
    19
    20  import org.apache.commons.logging.Log;
    21  import org.apache.commons.logging.LogFactory;
    22  import org.codehaus.activemq.broker.BrokerContainer;
    23  import org.codehaus.activemq.service.Service;
    24
    25  import javax.jms.JMSException;
    26  import java.util.ArrayList;
    27  import java.util.Iterator;
    28  import java.util.List;
    29
    30  /***
    31   * Represents a connector to one or more remote brokers.
    32   * This class manages a number of {@link NetworkChannel} instances
    33   * which may or may not be connected to a
    34   * remote broker at any point in time.
    35   * <p/>
    36   * The implementation of this class could use a fixed number of
    37   * configured {@link NetworkChannel} instances or could use
    38   * discovery to find them.
    39   *
    40   * @version $Revision: 1.4 $
    41   */
    42  public class NetworkConnector implements Service {
    43      private static final Log log = LogFactory.getLog(NetworkConnector.class);
    44
    45      private BrokerContainer brokerContainer;
    46      private TransportChannelListener transportChannelListener;
    47      private List networkChannels = new ArrayList();
    48
    49      public NetworkConnector(BrokerContainer brokerContainer) {
    50          this.brokerContainer = brokerContainer;
    51      }
    52
    53      public void start() throws JMSException {
    54          for (Iterator iter = networkChannels.iterator(); iter.hasNext();) {
    55              NetworkChannel networkChannel = (NetworkChannel) iter.next();
    56              networkChannel.setBrokerContainer(getBrokerContainer());
    57              networkChannel.start();
    58          }
    59      }
    60
    61      public void stop() throws JMSException {
    62          for (Iterator iter = networkChannels.iterator(); iter.hasNext();) {
    63              NetworkChannel networkChannel = (NetworkChannel) iter.next();
    64              try {
    65                  networkChannel.stop();
    66              }
    67              catch (JMSException e) {
    68                  log.warn("Failed to stop network channel: " + e, e);
    69              }
    70          }
    71      }
    72
    73      public void setTransportChannelListener(TransportChannelListener listener) {
    74          this.transportChannelListener = listener;
    75      }
    76
    77
    78      // Properties
    79      //-------------------------------------------------------------------------
    80      public BrokerContainer getBrokerContainer() {
    81          return brokerContainer;
    82      }
    83
    84      public List getNetworkChannels() {
    85          return networkChannels;
    86      }
    87
    88      /***
    89       * Sets a list of {@link NetworkChannel} instances
    90       *
    91       * @param networkChannels
    92       */
    93      public void setNetworkChannels(List networkChannels) {
    94          this.networkChannels = networkChannels;
    95      }
    96
    97      /***
    98       * Adds a new network channel for the given URI
    99       *
    100      * @param uri
    101      * @return
    102      */
    103     public NetworkChannel addNetworkChannel(String uri) {
    104         NetworkChannel networkChannel = new NetworkChannel(brokerContainer, uri);
    105         addNetworkChannel(networkChannel);
    106         return networkChannel;
    107     }
    108
    109     /***
    110      * Adds a new network channel
    111      */
    112     public void addNetworkChannel(NetworkChannel networkChannel) {
    113         networkChannels.add(networkChannel);
    114     }
    115
    116     /***
    117      * Removes a network channel
    118      */
    119     public void removeNetworkChannel(NetworkChannel networkChannel) {
    120         networkChannels.remove(networkChannel);
    121     }
    122 }