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    package org.apache.activemq.broker.region.virtual;
018    
019    import javax.jms.InvalidSelectorException;
020    import javax.jms.JMSException;
021    
022    import org.apache.activemq.command.ActiveMQDestination;
023    import org.apache.activemq.filter.BooleanExpression;
024    import org.apache.activemq.filter.MessageEvaluationContext;
025    import org.apache.activemq.selector.SelectorParser;
026    
027    /**
028     * Represents a destination which is filtered using some predicate such as a selector
029     * so that messages are only dispatched to the destination if they match the filter.
030     *
031     * @org.apache.xbean.XBean
032     *
033     * 
034     */
035    public class FilteredDestination {
036        
037        private ActiveMQDestination destination;
038        private String selector;
039        private BooleanExpression filter;
040    
041        public boolean matches(MessageEvaluationContext context) throws JMSException {
042            BooleanExpression booleanExpression = getFilter();
043            if (booleanExpression == null) {
044                return false;
045            }
046            return booleanExpression.matches(context);
047        }
048    
049        public ActiveMQDestination getDestination() {
050            return destination;
051        }
052    
053        /**
054         * The destination to send messages to if they match the filter
055         */
056        public void setDestination(ActiveMQDestination destination) {
057            this.destination = destination;
058        }
059    
060        public String getSelector() {
061            return selector;
062        }
063    
064        /**
065         * Sets the JMS selector used to filter messages before forwarding them to this destination
066         */
067        public void setSelector(String selector) throws InvalidSelectorException {
068            this.selector = selector;
069            setFilter(SelectorParser.parse(selector));
070        }
071    
072        public BooleanExpression getFilter() {
073            return filter;
074        }
075    
076        public void setFilter(BooleanExpression filter) {
077            this.filter = filter;
078        }
079    
080    
081        /**
082         * Sets the destination property to the given queue name
083         */
084        public void setQueue(String queue) {
085            setDestination(ActiveMQDestination.createDestination(queue, ActiveMQDestination.QUEUE_TYPE));
086        }
087    
088        /**
089         * Sets the destination property to the given topic name
090         */
091        public void setTopic(String topic) {
092            setDestination(ActiveMQDestination.createDestination(topic, ActiveMQDestination.TOPIC_TYPE));
093        }
094    }