001    /** 
002     * 
003     * Copyright 2004 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * 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    
019    package org.activemq.filter;
020    
021    import org.activemq.message.ActiveMQDestination;
022    
023    import javax.jms.Destination;
024    import javax.jms.JMSException;
025    import javax.jms.Message;
026    
027    
028    /**
029     * Represents a filter which only operates on Destinations
030     *
031     * @version $Revision: 1.1.1.1 $
032     */
033    public abstract class DestinationFilter implements Filter {
034        public static final String ANY_DESCENDENT = ">";
035        public static final String ANY_CHILD = "*";
036    
037        public boolean matches(Message message) throws JMSException {
038            return matches(message.getJMSDestination());
039        }
040    
041        public abstract boolean matches(Destination destination);
042    
043        public static DestinationFilter parseFilter(Destination destination) {
044            if (destination instanceof ActiveMQDestination) {
045                ActiveMQDestination activeDestination = (ActiveMQDestination) destination;
046                if (activeDestination.isComposite()) {
047                    return new CompositeDestinationFilter(activeDestination);
048                }
049            }
050            String[] paths = DestinationPath.getDestinationPaths(destination);
051            int idx = paths.length - 1;
052            if (idx >= 0) {
053                String lastPath = paths[idx];
054                if (lastPath.equals(ANY_DESCENDENT)) {
055                    return new PrefixDestinationFilter(paths);
056                }
057                else {
058                    while (idx >= 0) {
059                        lastPath = paths[idx--];
060                        if (lastPath.equals(ANY_CHILD)) {
061                            return new WildcardDestinationFilter(paths);
062                        }
063                    }
064                }
065            }
066    
067            // if none of the paths contain a wildcard then use equality
068            return new SimpleDestinationFilter(destination);
069        }
070    }