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
    19  package org.codehaus.activemq.filter;
    20
    21  import javax.jms.Destination;
    22  import javax.jms.JMSException;
    23  import javax.jms.Message;
    24  import java.util.ArrayList;
    25  import java.util.List;
    26
    27  /***
    28   * Helper class for decomposing a Destination into a number of paths
    29   *
    30   * @version $Revision: 1.1 $
    31   */
    32  public class DestinationPath {
    33
    34      public static String[] getDestinationPaths(String subject) {
    35          List list = new ArrayList();
    36          int previous = 0;
    37          int lastIndex = subject.length() - 1;
    38          while (true) {
    39              int idx = subject.indexOf('.', previous);
    40              if (idx < 0) {
    41                  list.add(subject.substring(previous, lastIndex + 1));
    42                  break;
    43              }
    44              list.add(subject.substring(previous, idx));
    45              previous = idx + 1;
    46          }
    47          String[] answer = new String[list.size()];
    48          list.toArray(answer);
    49          return answer;
    50      }
    51
    52      public static String[] getDestinationPaths(Message message) throws JMSException {
    53          return getDestinationPaths(message.getJMSDestination());
    54      }
    55
    56      public static String[] getDestinationPaths(Destination destination) {
    57          return getDestinationPaths(destination.toString());
    58      }
    59
    60      /***
    61       * Converts the paths to a single String seperated by dots.
    62       *
    63       * @param paths
    64       * @return
    65       */
    66      public static String toString(String[] paths) {
    67          StringBuffer buffer = new StringBuffer();
    68          for (int i = 0; i < paths.length; i++) {
    69              if (i > 0) {
    70                  buffer.append(".");
    71              }
    72              String path = paths[i];
    73              if (path == null) {
    74                  buffer.append("*");
    75              }
    76              else {
    77                  buffer.append(path);
    78              }
    79          }
    80          return buffer.toString();
    81      }
    82  }