001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.activemq.filter; 019 020import org.apache.activemq.command.ActiveMQDestination; 021 022 023/** 024 * Matches messages which match a prefix like "A.B.>" 025 * 026 * 027 */ 028public class PrefixDestinationFilter extends DestinationFilter { 029 030 private String[] prefixes; 031 private byte destinationType; 032 033 /** 034 * An array of paths, the last path is '>' 035 * 036 * @param prefixes 037 */ 038 public PrefixDestinationFilter(String[] prefixes, byte destinationType) { 039 // collapse duplicate '>' at the end of the path 040 int lastIndex = prefixes.length - 1; 041 while (lastIndex >= 0 && ANY_DESCENDENT.equals(prefixes[lastIndex])) { 042 lastIndex--; 043 } 044 this.prefixes = new String[lastIndex + 2]; 045 System.arraycopy(prefixes, 0, this.prefixes, 0, this.prefixes.length); 046 this.destinationType = destinationType; 047 } 048 049 050 public boolean matches(ActiveMQDestination destination) { 051 if (destination.getDestinationType() != destinationType) return false; 052 String[] path = DestinationPath.getDestinationPaths(destination.getPhysicalName()); 053 054 055 int length = prefixes.length; 056 if (path.length >= length) { 057 int size = length - 1; 058 for (int i = 0; i < size; i++) { 059 if (!matches(prefixes[i],path[i])) { 060 return false; 061 } 062 } 063 return true; 064 }else{ 065 //want to look for the case where A matches A.> 066 boolean match = true; 067 for (int i = 0; (i < path.length && match); i++){ 068 match = matches(prefixes[i], path[i]); 069 } 070 //paths get compacted - e.g. A.*.> will be compacted to A.> and by definition - the last element on 071 //the prefix will be > 072 if (match && prefixes.length == (path.length + 1)) { 073 return true; 074 } 075 } 076 return false; 077 } 078 079 private boolean matches(String prefix,String path){ 080 return path.equals(ANY_CHILD) || prefix.equals(ANY_CHILD) || prefix.equals(path); 081 } 082 083 public String getText() { 084 return DestinationPath.toString(prefixes); 085 } 086 087 public String toString() { 088 return super.toString() + "[destination: " + getText() + "]"; 089 } 090 091 public boolean isWildcard() { 092 return true; 093 } 094}