CompositeTransportChannelFactory xref

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.composite;
    19
    20  import org.codehaus.activemq.message.WireFormat;
    21  import org.codehaus.activemq.transport.TransportChannel;
    22  import org.codehaus.activemq.transport.TransportChannelFactory;
    23  import org.codehaus.activemq.util.JMSExceptionHelper;
    24
    25  import javax.jms.JMSException;
    26  import java.net.URI;
    27  import java.net.URISyntaxException;
    28  import java.util.ArrayList;
    29  import java.util.List;
    30  import java.util.StringTokenizer;
    31
    32  /***
    33   * A Composite implementation of a TransportChannelFactory
    34   *
    35   * @version $Revision: 1.5 $
    36   */
    37  public class CompositeTransportChannelFactory implements TransportChannelFactory {
    38      private String separator = ",";
    39
    40      public TransportChannel create(WireFormat wireFormat, URI remoteLocation) throws JMSException {
    41          try {
    42              return new CompositeTransportChannel(wireFormat, parseURIs(remoteLocation));
    43          }
    44          catch (URISyntaxException e) {
    45              throw JMSExceptionHelper.newJMSException("Can't parse list of URIs for: " + remoteLocation + ". Reason: " + e, e);
    46          }
    47      }
    48
    49      public TransportChannel create(WireFormat wireFormat, URI remoteLocation, URI localLocation) throws JMSException {
    50          return create(wireFormat, remoteLocation);
    51      }
    52
    53      public boolean requiresEmbeddedBroker() {
    54          return false;
    55      }
    56
    57      protected List parseURIs(URI uri) throws URISyntaxException {
    58          String text = uri.getSchemeSpecificPart();
    59          List uris = new ArrayList();
    60          StringTokenizer iter = new StringTokenizer(text, separator);
    61          while (iter.hasMoreTokens()) {
    62              String child = stripLeadingSlashes(iter.nextToken().trim());
    63              uris.add(new URI(child));
    64          }
    65          return randomizeURIs(uris);
    66      }
    67
    68      protected String stripLeadingSlashes(String text) {
    69          int idx = 0;
    70          while (text.charAt(idx) == '/') {
    71              idx++;
    72          }
    73          if (idx > 0) {
    74              return text.substring(idx);
    75          }
    76          return text;
    77      }
    78
    79
    80      protected List randomizeURIs(List uris) {
    81          return uris;
    82      }
    83  }