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    
018    package org.apache.activemq.filter;
019    
020    import org.apache.activemq.command.ActiveMQDestination;
021    
022    
023    /**
024     * Matches messages which contain wildcards like "A.B.*.*"
025     *
026     * 
027     */
028    public class WildcardDestinationFilter extends DestinationFilter {
029    
030        private String[] prefixes;
031        private byte destinationType;
032    
033        /**
034         * An array of paths containing * characters
035         *
036         * @param prefixes
037         */
038        public WildcardDestinationFilter(String[] prefixes, byte destinationType) {
039            this.prefixes = new String[prefixes.length];
040            for (int i = 0; i < prefixes.length; i++) {
041                String prefix = prefixes[i];
042                if (!prefix.equals("*")) {
043                    this.prefixes[i] = prefix;
044                }
045            }
046            this.destinationType = destinationType;
047        }
048    
049        public boolean matches(ActiveMQDestination destination) {
050            if (destination.getDestinationType() != destinationType) return false;
051            String[] path = DestinationPath.getDestinationPaths(destination);
052            int length = prefixes.length;
053            if (path.length == length) {
054                for (int i = 0; i < length; i++) {
055                    String prefix = prefixes[i];
056                    if (prefix != null && !prefix.equals(path[i])) {
057                        return false;
058                    }
059                }
060                return true;
061            }
062            return false;
063        }
064    
065    
066        public String getText() {
067            return DestinationPath.toString(prefixes);
068        }
069    
070        public String toString() {
071            return super.toString() + "[destination: " + getText() + "]";
072        }
073    
074        public boolean isWildcard() {
075            return true;
076        }
077    }