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 java.io.IOException;
021
022import javax.jms.JMSException;
023import org.apache.activemq.command.ActiveMQDestination;
024import org.apache.activemq.util.JMSExceptionSupport;
025
026/**
027 * Represents a filter which only operates on Destinations
028 */
029public abstract class DestinationFilter implements BooleanExpression {
030
031    public static final String ANY_DESCENDENT = ">";
032    public static final String ANY_CHILD = "*";
033
034    public Object evaluate(MessageEvaluationContext message) throws JMSException {
035        return matches(message) ? Boolean.TRUE : Boolean.FALSE;
036    }
037
038    public boolean matches(MessageEvaluationContext message) throws JMSException {
039        try {
040            if (message.isDropped()) {
041                return false;
042            }
043            return matches(message.getMessage().getDestination());
044        } catch (IOException e) {
045            throw JMSExceptionSupport.create(e);
046        }
047    }
048
049    public abstract boolean matches(ActiveMQDestination destination);
050
051    public static DestinationFilter parseFilter(ActiveMQDestination destination) {
052        if (destination.isComposite()) {
053            return new CompositeDestinationFilter(destination);
054        }
055        String[] paths = DestinationPath.getDestinationPaths(destination);
056        int idx = paths.length - 1;
057        if (idx >= 0) {
058            String lastPath = paths[idx];
059            if (lastPath.equals(ANY_DESCENDENT)) {
060                return new PrefixDestinationFilter(paths, destination.getDestinationType());
061            } else {
062                while (idx >= 0) {
063                    lastPath = paths[idx--];
064                    if (lastPath.equals(ANY_CHILD)) {
065                        return new WildcardDestinationFilter(paths, destination.getDestinationType());
066                    }
067                }
068            }
069        }
070
071        // if none of the paths contain a wildcard then use equality
072        return new SimpleDestinationFilter(destination);
073    }
074
075    public abstract boolean isWildcard();
076}