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