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 */ 017package org.apache.activemq.broker.region.virtual; 018 019import javax.jms.InvalidSelectorException; 020import javax.jms.JMSException; 021 022import org.apache.activemq.command.ActiveMQDestination; 023import org.apache.activemq.filter.BooleanExpression; 024import org.apache.activemq.filter.MessageEvaluationContext; 025import 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 */ 035public 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 095 @Override 096 public int hashCode() { 097 final int prime = 31; 098 int result = 1; 099 result = prime * result + ((destination == null) ? 0 : destination.hashCode()); 100 result = prime * result + ((selector == null) ? 0 : selector.hashCode()); 101 return result; 102 } 103 104 @Override 105 public boolean equals(Object obj) { 106 if (this == obj) 107 return true; 108 if (obj == null) 109 return false; 110 if (getClass() != obj.getClass()) 111 return false; 112 FilteredDestination other = (FilteredDestination) obj; 113 if (destination == null) { 114 if (other.destination != null) 115 return false; 116 } else if (!destination.equals(other.destination)) 117 return false; 118 if (selector == null) { 119 if (other.selector != null) 120 return false; 121 } else if (!selector.equals(other.selector)) 122 return false; 123 return true; 124 } 125}