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        this.prefixes = prefixes;
040        this.destinationType = destinationType;
041    }
042
043    public boolean matches(ActiveMQDestination destination) {
044        if (destination.getDestinationType() != destinationType) return false;
045        String[] path = DestinationPath.getDestinationPaths(destination.getPhysicalName());
046        int length = prefixes.length;
047        if (path.length >= length) {
048            int size = length - 1;
049            for (int i = 0; i < size; i++) {
050                if (!path[i].equals(ANY_CHILD) && !prefixes[i].equals(ANY_CHILD) && !prefixes[i].equals(path[i])) {
051                    return false;
052                }
053            }
054            return true;
055        }
056        return false;
057    }
058
059    public String getText() {
060        return DestinationPath.toString(prefixes);
061    }
062
063    public String toString() {
064        return super.toString() + "[destination: " + getText() + "]";
065    }
066
067    public boolean isWildcard() {
068        return true;
069    }
070}