KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > broker > region > virtual > FilteredDestination


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.broker.region.virtual;
19
20 import org.apache.activemq.command.ActiveMQDestination;
21 import org.apache.activemq.filter.BooleanExpression;
22 import org.apache.activemq.filter.MessageEvaluationContext;
23 import org.apache.activemq.selector.SelectorParser;
24
25 import javax.jms.InvalidSelectorException JavaDoc;
26 import javax.jms.JMSException JavaDoc;
27
28 /**
29  * Represents a destination which is filtered using some predicate such as a selector
30  * so that messages are only dispatched to the destination if they match the filter.
31  *
32  * @org.apache.xbean.XBean
33  *
34  * @version $Revision: 479694 $
35  */

36 public class FilteredDestination {
37     
38     private ActiveMQDestination destination;
39     private String JavaDoc selector;
40     private BooleanExpression filter;
41
42     public boolean matches(MessageEvaluationContext context) throws JMSException JavaDoc {
43         BooleanExpression booleanExpression = getFilter();
44         if (booleanExpression == null) {
45             return false;
46         }
47         return booleanExpression.matches(context);
48     }
49
50     public ActiveMQDestination getDestination() {
51         return destination;
52     }
53
54     /**
55      * The destination to send messages to if they match the filter
56      */

57     public void setDestination(ActiveMQDestination destination) {
58         this.destination = destination;
59     }
60
61     public String JavaDoc getSelector() {
62         return selector;
63     }
64
65     /**
66      * Sets the JMS selector used to filter messages before forwarding them to this destination
67      */

68     public void setSelector(String JavaDoc selector) throws InvalidSelectorException JavaDoc {
69         this.selector = selector;
70         setFilter(new SelectorParser().parse(selector));
71     }
72
73     public BooleanExpression getFilter() {
74         return filter;
75     }
76
77     public void setFilter(BooleanExpression filter) {
78         this.filter = filter;
79     }
80
81
82     /**
83      * Sets the destination property to the given queue name
84      */

85     public void setQueue(String JavaDoc queue) {
86         setDestination(ActiveMQDestination.createDestination(queue, ActiveMQDestination.QUEUE_TYPE));
87     }
88
89     /**
90      * Sets the destination property to the given topic name
91      */

92     public void setTopic(String JavaDoc topic) {
93         setDestination(ActiveMQDestination.createDestination(topic, ActiveMQDestination.TOPIC_TYPE));
94     }
95 }
96
Popular Tags