From a75276726c56e67087d7de909da69676bd93d40b Mon Sep 17 00:00:00 2001 From: Robert Godfrey Date: Thu, 2 Feb 2012 14:26:16 +0000 Subject: QPID-3801 : [Java] Move JMS Selector code into common git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@1239643 13f79535-47bb-0310-9956-ffa450edef68 --- java/broker/build.xml | 9 +- java/broker/src/main/grammar/SelectorParser.jj | 621 --------------------- .../apache/qpid/server/exchange/TopicExchange.java | 20 +- .../qpid/server/filter/ArithmeticExpression.java | 274 --------- .../qpid/server/filter/BinaryExpression.java | 90 --- .../qpid/server/filter/BooleanExpression.java | 38 -- .../qpid/server/filter/ComparisonExpression.java | 598 -------------------- .../qpid/server/filter/ConstantExpression.java | 209 ------- .../org/apache/qpid/server/filter/Expression.java | 36 -- .../qpid/server/filter/FilterManagerFactory.java | 21 +- .../qpid/server/filter/JMSSelectorFilter.java | 69 ++- .../apache/qpid/server/filter/LogicExpression.java | 120 ---- .../qpid/server/filter/PropertyExpression.java | 234 -------- .../apache/qpid/server/filter/UnaryExpression.java | 361 ------------ .../apache/qpid/server/filter/XPathExpression.java | 113 ---- .../qpid/server/filter/XQueryExpression.java | 55 -- .../qpid/server/filter/XalanXPathEvaluator.java | 89 --- .../org/apache/qpid/server/SelectorParserTest.java | 15 +- java/client/build.xml | 8 - java/client/src/main/grammar/SelectorParser.jj | 609 -------------------- .../java/org/apache/qpid/client/AMQSession.java | 2 +- .../org/apache/qpid/client/AMQSession_0_10.java | 2 +- .../org/apache/qpid/client/AMQSession_0_8.java | 2 +- .../apache/qpid/client/BasicMessageConsumer.java | 4 +- .../qpid/client/filter/JMSSelectorFilter.java | 213 +++++++ .../apache/qpid/client/filter/MessageFilter.java | 27 + .../apache/qpid/filter/ArithmeticExpression.java | 268 --------- .../org/apache/qpid/filter/BinaryExpression.java | 88 --- .../org/apache/qpid/filter/BooleanExpression.java | 33 -- .../apache/qpid/filter/ComparisonExpression.java | 588 ------------------- .../org/apache/qpid/filter/ConstantExpression.java | 204 ------- .../java/org/apache/qpid/filter/Expression.java | 35 -- .../org/apache/qpid/filter/JMSSelectorFilter.java | 71 --- .../org/apache/qpid/filter/LogicExpression.java | 108 ---- .../java/org/apache/qpid/filter/MessageFilter.java | 27 - .../org/apache/qpid/filter/PropertyExpression.java | 300 ---------- .../org/apache/qpid/filter/UnaryExpression.java | 316 ----------- .../apache/qpid/filter/JMSSelectorFilterTest.java | 2 + .../qpid/test/unit/message/TestAMQSession.java | 2 +- java/common/build.xml | 10 +- java/common/src/main/grammar/SelectorParser.jj | 608 ++++++++++++++++++++ .../apache/qpid/filter/ArithmeticExpression.java | 272 +++++++++ .../org/apache/qpid/filter/BinaryExpression.java | 90 +++ .../org/apache/qpid/filter/BooleanExpression.java | 36 ++ .../apache/qpid/filter/ComparisonExpression.java | 596 ++++++++++++++++++++ .../org/apache/qpid/filter/ConstantExpression.java | 207 +++++++ .../java/org/apache/qpid/filter/Expression.java | 34 ++ .../org/apache/qpid/filter/FilterableMessage.java | 41 ++ .../org/apache/qpid/filter/LogicExpression.java | 118 ++++ .../org/apache/qpid/filter/PropertyExpression.java | 234 ++++++++ .../qpid/filter/SelectorParsingException.java | 40 ++ .../org/apache/qpid/filter/UnaryExpression.java | 340 +++++++++++ java/lib/javacc.jar | Bin 378781 -> 298569 bytes 53 files changed, 2990 insertions(+), 5517 deletions(-) delete mode 100644 java/broker/src/main/grammar/SelectorParser.jj delete mode 100644 java/broker/src/main/java/org/apache/qpid/server/filter/ArithmeticExpression.java delete mode 100644 java/broker/src/main/java/org/apache/qpid/server/filter/BinaryExpression.java delete mode 100644 java/broker/src/main/java/org/apache/qpid/server/filter/BooleanExpression.java delete mode 100644 java/broker/src/main/java/org/apache/qpid/server/filter/ComparisonExpression.java delete mode 100644 java/broker/src/main/java/org/apache/qpid/server/filter/ConstantExpression.java delete mode 100644 java/broker/src/main/java/org/apache/qpid/server/filter/Expression.java delete mode 100644 java/broker/src/main/java/org/apache/qpid/server/filter/LogicExpression.java delete mode 100644 java/broker/src/main/java/org/apache/qpid/server/filter/PropertyExpression.java delete mode 100644 java/broker/src/main/java/org/apache/qpid/server/filter/UnaryExpression.java delete mode 100644 java/broker/src/main/java/org/apache/qpid/server/filter/XPathExpression.java delete mode 100644 java/broker/src/main/java/org/apache/qpid/server/filter/XQueryExpression.java delete mode 100644 java/broker/src/main/java/org/apache/qpid/server/filter/XalanXPathEvaluator.java delete mode 100644 java/client/src/main/grammar/SelectorParser.jj create mode 100644 java/client/src/main/java/org/apache/qpid/client/filter/JMSSelectorFilter.java create mode 100644 java/client/src/main/java/org/apache/qpid/client/filter/MessageFilter.java delete mode 100644 java/client/src/main/java/org/apache/qpid/filter/ArithmeticExpression.java delete mode 100644 java/client/src/main/java/org/apache/qpid/filter/BinaryExpression.java delete mode 100644 java/client/src/main/java/org/apache/qpid/filter/BooleanExpression.java delete mode 100644 java/client/src/main/java/org/apache/qpid/filter/ComparisonExpression.java delete mode 100644 java/client/src/main/java/org/apache/qpid/filter/ConstantExpression.java delete mode 100644 java/client/src/main/java/org/apache/qpid/filter/Expression.java delete mode 100644 java/client/src/main/java/org/apache/qpid/filter/JMSSelectorFilter.java delete mode 100644 java/client/src/main/java/org/apache/qpid/filter/LogicExpression.java delete mode 100644 java/client/src/main/java/org/apache/qpid/filter/MessageFilter.java delete mode 100644 java/client/src/main/java/org/apache/qpid/filter/PropertyExpression.java delete mode 100644 java/client/src/main/java/org/apache/qpid/filter/UnaryExpression.java create mode 100644 java/common/src/main/grammar/SelectorParser.jj create mode 100644 java/common/src/main/java/org/apache/qpid/filter/ArithmeticExpression.java create mode 100644 java/common/src/main/java/org/apache/qpid/filter/BinaryExpression.java create mode 100644 java/common/src/main/java/org/apache/qpid/filter/BooleanExpression.java create mode 100644 java/common/src/main/java/org/apache/qpid/filter/ComparisonExpression.java create mode 100644 java/common/src/main/java/org/apache/qpid/filter/ConstantExpression.java create mode 100644 java/common/src/main/java/org/apache/qpid/filter/Expression.java create mode 100644 java/common/src/main/java/org/apache/qpid/filter/FilterableMessage.java create mode 100644 java/common/src/main/java/org/apache/qpid/filter/LogicExpression.java create mode 100644 java/common/src/main/java/org/apache/qpid/filter/PropertyExpression.java create mode 100644 java/common/src/main/java/org/apache/qpid/filter/SelectorParsingException.java create mode 100644 java/common/src/main/java/org/apache/qpid/filter/UnaryExpression.java (limited to 'java') diff --git a/java/broker/build.xml b/java/broker/build.xml index 090df101f0..bb809583ee 100644 --- a/java/broker/build.xml +++ b/java/broker/build.xml @@ -34,14 +34,7 @@ - - - - - - + diff --git a/java/broker/src/main/grammar/SelectorParser.jj b/java/broker/src/main/grammar/SelectorParser.jj deleted file mode 100644 index c9e01cd01f..0000000000 --- a/java/broker/src/main/grammar/SelectorParser.jj +++ /dev/null @@ -1,621 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - - // - // Original File from r450141 of the Apache ActiveMQ project - // - -// ---------------------------------------------------------------------------- -// OPTIONS -// ---------------------------------------------------------------------------- -options { - STATIC = false; - UNICODE_INPUT = true; - - // some performance optimizations - OPTIMIZE_TOKEN_MANAGER = true; - ERROR_REPORTING = false; -} - -// ---------------------------------------------------------------------------- -// PARSER -// ---------------------------------------------------------------------------- - -PARSER_BEGIN(SelectorParser) -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.server.filter.jms.selector; - -import java.io.StringReader; -import java.util.ArrayList; - -import org.apache.qpid.AMQInvalidArgumentException; -import org.apache.qpid.server.filter.ArithmeticExpression; -import org.apache.qpid.server.filter.BooleanExpression; -import org.apache.qpid.server.filter.ComparisonExpression; -import org.apache.qpid.server.filter.ConstantExpression; -import org.apache.qpid.server.filter.Expression; -import org.apache.qpid.server.filter.LogicExpression; -import org.apache.qpid.server.filter.PropertyExpression; -import org.apache.qpid.server.filter.UnaryExpression; - -/** - * JMS Selector Parser generated by JavaCC - * - * Do not edit this .java file directly - it is autogenerated from SelectorParser.jj - */ -public class SelectorParser { - - public SelectorParser() { - this(new StringReader("")); - } - - public BooleanExpression parse(String sql) throws AMQInvalidArgumentException { - this.ReInit(new StringReader(sql)); - - try { - return this.JmsSelector(); - } - catch (Throwable e) { - throw (AMQInvalidArgumentException)new AMQInvalidArgumentException(sql,e); - } - - } - - private BooleanExpression asBooleanExpression(Expression value) throws ParseException { - if (value instanceof BooleanExpression) { - return (BooleanExpression) value; - } - if (value instanceof PropertyExpression) { - return UnaryExpression.createBooleanCast( value ); - } - throw new ParseException("Expression will not result in a boolean value: " + value); - } - - -} - -PARSER_END(SelectorParser) - -// ---------------------------------------------------------------------------- -// Tokens -// ---------------------------------------------------------------------------- - -/* White Space */ -SPECIAL_TOKEN : -{ - " " | "\t" | "\n" | "\r" | "\f" -} - -/* Comments */ -SKIP: -{ - -} - -SKIP: -{ - -} - -/* Reserved Words */ -TOKEN [IGNORE_CASE] : -{ - < NOT : "NOT"> - | < AND : "AND"> - | < OR : "OR"> - | < BETWEEN : "BETWEEN"> - | < LIKE : "LIKE"> - | < ESCAPE : "ESCAPE"> - | < IN : "IN"> - | < IS : "IS"> - | < TRUE : "TRUE" > - | < FALSE : "FALSE" > - | < NULL : "NULL" > - | < XPATH : "XPATH" > - | < XQUERY : "XQUERY" > -} - -/* Literals */ -TOKEN [IGNORE_CASE] : -{ - - < DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* (["l","L"])? > - | < HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ > - | < OCTAL_LITERAL: "0" (["0"-"7"])* > - | < FLOATING_POINT_LITERAL: - (["0"-"9"])+ "." (["0"-"9"])* ()? // matches: 5.5 or 5. or 5.5E10 or 5.E10 - | "." (["0"-"9"])+ ()? // matches: .5 or .5E10 - | (["0"-"9"])+ // matches: 5E10 - > - | < #EXPONENT: "E" (["+","-"])? (["0"-"9"])+ > - | < STRING_LITERAL: "'" ( ("''") | ~["'"] )* "'" > -} - -TOKEN [IGNORE_CASE] : -{ - < ID : ["a"-"z", "_", "$"] (["a"-"z","0"-"9","_", "$"])* > - | < QUOTED_ID : "\"" ( ("\"\"") | ~["\""] )* "\"" > -} - -// ---------------------------------------------------------------------------- -// Grammer -// ---------------------------------------------------------------------------- -BooleanExpression JmsSelector() : -{ - Expression left=null; -} -{ - ( - left = orExpression() - ) - { - return asBooleanExpression(left); - } - -} - -Expression orExpression() : -{ - Expression left; - Expression right; -} -{ - ( - left = andExpression() - ( - right = andExpression() - { - left = LogicExpression.createOR(asBooleanExpression(left), asBooleanExpression(right)); - } - )* - ) - { - return left; - } - -} - - -Expression andExpression() : -{ - Expression left; - Expression right; -} -{ - ( - left = equalityExpression() - ( - right = equalityExpression() - { - left = LogicExpression.createAND(asBooleanExpression(left), asBooleanExpression(right)); - } - )* - ) - { - return left; - } -} - -Expression equalityExpression() : -{ - Expression left; - Expression right; -} -{ - ( - left = comparisonExpression() - ( - - "=" right = comparisonExpression() - { - left = ComparisonExpression.createEqual(left, right); - } - | - "<>" right = comparisonExpression() - { - left = ComparisonExpression.createNotEqual(left, right); - } - | - LOOKAHEAD(2) - - { - left = ComparisonExpression.createIsNull(left); - } - | - - { - left = ComparisonExpression.createIsNotNull(left); - } - )* - ) - { - return left; - } -} - -Expression comparisonExpression() : -{ - Expression left; - Expression right; - Expression low; - Expression high; - String t, u; - boolean not; - ArrayList list; -} -{ - ( - left = addExpression() - ( - - ">" right = addExpression() - { - left = ComparisonExpression.createGreaterThan(left, right); - } - | - ">=" right = addExpression() - { - left = ComparisonExpression.createGreaterThanEqual(left, right); - } - | - "<" right = addExpression() - { - left = ComparisonExpression.createLessThan(left, right); - } - | - "<=" right = addExpression() - { - left = ComparisonExpression.createLessThanEqual(left, right); - } - | - { - u=null; - } - t = stringLitteral() - [ u = stringLitteral() ] - { - left = ComparisonExpression.createLike(left, t, u); - } - | - LOOKAHEAD(2) - { - u=null; - } - t = stringLitteral() [ u = stringLitteral() ] - { - left = ComparisonExpression.createNotLike(left, t, u); - } - | - low = addExpression() high = addExpression() - { - left = ComparisonExpression.createBetween(left, low, high); - } - | - LOOKAHEAD(2) - low = addExpression() high = addExpression() - { - left = ComparisonExpression.createNotBetween(left, low, high); - } - | - - "(" - t = stringLitteral() - { - list = new ArrayList(); - list.add( t ); - } - ( - "," - t = stringLitteral() - { - list.add( t ); - } - - )* - ")" - { - left = ComparisonExpression.createInFilter(left, list); - } - | - LOOKAHEAD(2) - - "(" - t = stringLitteral() - { - list = new ArrayList(); - list.add( t ); - } - ( - "," - t = stringLitteral() - { - list.add( t ); - } - - )* - ")" - { - left = ComparisonExpression.createNotInFilter(left, list); - } - - )* - ) - { - return left; - } -} - -Expression addExpression() : -{ - Expression left; - Expression right; -} -{ - left = multExpr() - ( - LOOKAHEAD( ("+"|"-") multExpr()) - ( - "+" right = multExpr() - { - left = ArithmeticExpression.createPlus(left, right); - } - | - "-" right = multExpr() - { - left = ArithmeticExpression.createMinus(left, right); - } - ) - - )* - { - return left; - } -} - -Expression multExpr() : -{ - Expression left; - Expression right; -} -{ - left = unaryExpr() - ( - "*" right = unaryExpr() - { - left = ArithmeticExpression.createMultiply(left, right); - } - | - "/" right = unaryExpr() - { - left = ArithmeticExpression.createDivide(left, right); - } - | - "%" right = unaryExpr() - { - left = ArithmeticExpression.createMod(left, right); - } - - )* - { - return left; - } -} - - -Expression unaryExpr() : -{ - String s=null; - Expression left=null; -} -{ - ( - LOOKAHEAD( "+" unaryExpr() ) - "+" left=unaryExpr() - | - "-" left=unaryExpr() - { - left = UnaryExpression.createNegate(left); - } - | - left=unaryExpr() - { - left = UnaryExpression.createNOT( asBooleanExpression(left) ); - } - | - s=stringLitteral() - { - left = UnaryExpression.createXPath( s ); - } - | - s=stringLitteral() - { - left = UnaryExpression.createXQuery( s ); - } - | - left = primaryExpr() - ) - { - return left; - } - -} - -Expression primaryExpr() : -{ - Expression left=null; -} -{ - ( - left = literal() - | - left = variable() - | - "(" left = orExpression() ")" - ) - { - return left; - } -} - - - -ConstantExpression literal() : -{ - Token t; - String s; - ConstantExpression left=null; -} -{ - ( - ( - s = stringLitteral() - { - left = new ConstantExpression(s); - } - ) - | - ( - t = - { - left = ConstantExpression.createFromDecimal(t.image); - } - ) - | - ( - t = - { - left = ConstantExpression.createFromHex(t.image); - } - ) - | - ( - t = - { - left = ConstantExpression.createFromOctal(t.image); - } - ) - | - ( - t = - { - left = ConstantExpression.createFloat(t.image); - } - ) - | - ( - - { - left = ConstantExpression.TRUE; - } - ) - | - ( - - { - left = ConstantExpression.FALSE; - } - ) - | - ( - - { - left = ConstantExpression.NULL; - } - ) - ) - { - return left; - } -} - -String stringLitteral() : -{ - Token t; - StringBuffer rc = new StringBuffer(); - boolean first=true; -} -{ - t = - { - // Decode the sting value. - String image = t.image; - for( int i=1; i < image.length()-1; i++ ) { - char c = image.charAt(i); - if( c == '\'' ) - i++; - rc.append(c); - } - return rc.toString(); - } -} - -PropertyExpression variable() : -{ - Token t; - StringBuffer rc = new StringBuffer(); - PropertyExpression left=null; -} -{ - ( - t = - { - left = new PropertyExpression(t.image); - } - | - t = - { - // Decode the sting value. - String image = t.image; - for( int i=1; i < image.length()-1; i++ ) { - char c = image.charAt(i); - if( c == '"' ) - i++; - rc.append(c); - } - return new PropertyExpression(rc.toString()); - } - - - ) - { - return left; - } -} diff --git a/java/broker/src/main/java/org/apache/qpid/server/exchange/TopicExchange.java b/java/broker/src/main/java/org/apache/qpid/server/exchange/TopicExchange.java index a1bf564a3d..6d7242a78d 100644 --- a/java/broker/src/main/java/org/apache/qpid/server/exchange/TopicExchange.java +++ b/java/broker/src/main/java/org/apache/qpid/server/exchange/TopicExchange.java @@ -26,6 +26,8 @@ import org.apache.qpid.AMQException; import org.apache.qpid.AMQInvalidArgumentException; import org.apache.qpid.common.AMQPFilterTypes; import org.apache.qpid.exchange.ExchangeDefaults; +import org.apache.qpid.filter.SelectorParsingException; +import org.apache.qpid.filter.selector.TokenMgrError; import org.apache.qpid.framing.AMQShortString; import org.apache.qpid.framing.FieldTable; import org.apache.qpid.server.binding.Binding; @@ -34,6 +36,7 @@ import org.apache.qpid.server.exchange.topic.TopicMatcherResult; import org.apache.qpid.server.exchange.topic.TopicNormalizer; import org.apache.qpid.server.exchange.topic.TopicParser; import org.apache.qpid.server.filter.JMSSelectorFilter; +import org.apache.qpid.filter.selector.ParseException; import org.apache.qpid.server.message.InboundMessage; import org.apache.qpid.server.queue.AMQQueue; import org.apache.qpid.server.queue.BaseQueue; @@ -193,7 +196,22 @@ public class TopicExchange extends AbstractExchange if(selectorRef == null || (selector = selectorRef.get())==null) { - selector = new JMSSelectorFilter(selectorString); + try + { + selector = new JMSSelectorFilter(selectorString); + } + catch (ParseException e) + { + throw new AMQInvalidArgumentException("Cannot parse JMS selector \"" + selectorString + "\"", e); + } + catch (SelectorParsingException e) + { + throw new AMQInvalidArgumentException("Cannot parse JMS selector \"" + selectorString + "\"", e); + } + catch (TokenMgrError e) + { + throw new AMQInvalidArgumentException("Cannot parse JMS selector \"" + selectorString + "\"", e); + } _selectorCache.put(selectorString, new WeakReference(selector)); } return selector; diff --git a/java/broker/src/main/java/org/apache/qpid/server/filter/ArithmeticExpression.java b/java/broker/src/main/java/org/apache/qpid/server/filter/ArithmeticExpression.java deleted file mode 100644 index 703f59dda6..0000000000 --- a/java/broker/src/main/java/org/apache/qpid/server/filter/ArithmeticExpression.java +++ /dev/null @@ -1,274 +0,0 @@ -/** - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.qpid.server.filter; -// -// Based on like named file from r450141 of the Apache ActiveMQ project -// - -import org.apache.qpid.server.queue.Filterable; - -/** - * An expression which performs an operation on two expression values - */ -public abstract class ArithmeticExpression extends BinaryExpression -{ - - protected static final int INTEGER = 1; - protected static final int LONG = 2; - protected static final int DOUBLE = 3; - - /** - * @param left - * @param right - */ - public ArithmeticExpression(Expression left, Expression right) - { - super(left, right); - } - - public static Expression createPlus(Expression left, Expression right) - { - return new ArithmeticExpression(left, right) - { - protected Object evaluate(Object lvalue, Object rvalue) - { - if (lvalue instanceof String) - { - String text = (String) lvalue; - String answer = text + rvalue; - - return answer; - } - else if (lvalue instanceof Number) - { - return plus((Number) lvalue, asNumber(rvalue)); - } - - throw new RuntimeException("Cannot call plus operation on: " + lvalue + " and: " + rvalue); - } - - public String getExpressionSymbol() - { - return "+"; - } - }; - } - - public static Expression createMinus(Expression left, Expression right) - { - return new ArithmeticExpression(left, right) - { - protected Object evaluate(Object lvalue, Object rvalue) - { - if (lvalue instanceof Number) - { - return minus((Number) lvalue, asNumber(rvalue)); - } - - throw new RuntimeException("Cannot call minus operation on: " + lvalue + " and: " + rvalue); - } - - public String getExpressionSymbol() - { - return "-"; - } - }; - } - - public static Expression createMultiply(Expression left, Expression right) - { - return new ArithmeticExpression(left, right) - { - - protected Object evaluate(Object lvalue, Object rvalue) - { - if (lvalue instanceof Number) - { - return multiply((Number) lvalue, asNumber(rvalue)); - } - - throw new RuntimeException("Cannot call multiply operation on: " + lvalue + " and: " + rvalue); - } - - public String getExpressionSymbol() - { - return "*"; - } - }; - } - - public static Expression createDivide(Expression left, Expression right) - { - return new ArithmeticExpression(left, right) - { - - protected Object evaluate(Object lvalue, Object rvalue) - { - if (lvalue instanceof Number) - { - return divide((Number) lvalue, asNumber(rvalue)); - } - - throw new RuntimeException("Cannot call divide operation on: " + lvalue + " and: " + rvalue); - } - - public String getExpressionSymbol() - { - return "/"; - } - }; - } - - public static Expression createMod(Expression left, Expression right) - { - return new ArithmeticExpression(left, right) - { - - protected Object evaluate(Object lvalue, Object rvalue) - { - if (lvalue instanceof Number) - { - return mod((Number) lvalue, asNumber(rvalue)); - } - - throw new RuntimeException("Cannot call mod operation on: " + lvalue + " and: " + rvalue); - } - - public String getExpressionSymbol() - { - return "%"; - } - }; - } - - protected Number plus(Number left, Number right) - { - switch (numberType(left, right)) - { - - case INTEGER: - return Integer.valueOf(left.intValue() + right.intValue()); - - case LONG: - return Long.valueOf(left.longValue() + right.longValue()); - - default: - return Double.valueOf(left.doubleValue() + right.doubleValue()); - } - } - - protected Number minus(Number left, Number right) - { - switch (numberType(left, right)) - { - - case INTEGER: - return Integer.valueOf(left.intValue() - right.intValue()); - - case LONG: - return Long.valueOf(left.longValue() - right.longValue()); - - default: - return Double.valueOf(left.doubleValue() - right.doubleValue()); - } - } - - protected Number multiply(Number left, Number right) - { - switch (numberType(left, right)) - { - - case INTEGER: - return Integer.valueOf(left.intValue() * right.intValue()); - - case LONG: - return Long.valueOf(left.longValue() * right.longValue()); - - default: - return Double.valueOf(left.doubleValue() * right.doubleValue()); - } - } - - protected Number divide(Number left, Number right) - { - return Double.valueOf(left.doubleValue() / right.doubleValue()); - } - - protected Number mod(Number left, Number right) - { - return Double.valueOf(left.doubleValue() % right.doubleValue()); - } - - private int numberType(Number left, Number right) - { - if (isDouble(left) || isDouble(right)) - { - return DOUBLE; - } - else if ((left instanceof Long) || (right instanceof Long)) - { - return LONG; - } - else - { - return INTEGER; - } - } - - private boolean isDouble(Number n) - { - return (n instanceof Float) || (n instanceof Double); - } - - protected Number asNumber(Object value) - { - if (value instanceof Number) - { - return (Number) value; - } - else - { - throw new RuntimeException("Cannot convert value: " + value + " into a number"); - } - } - - public Object evaluate(Filterable message) - { - Object lvalue = getLeft().evaluate(message); - if (lvalue == null) - { - return null; - } - - Object rvalue = getRight().evaluate(message); - if (rvalue == null) - { - return null; - } - - return evaluate(lvalue, rvalue); - } - - /** - * @param lvalue - * @param rvalue - * @return - */ - protected abstract Object evaluate(Object lvalue, Object rvalue); - -} diff --git a/java/broker/src/main/java/org/apache/qpid/server/filter/BinaryExpression.java b/java/broker/src/main/java/org/apache/qpid/server/filter/BinaryExpression.java deleted file mode 100644 index 355630bb51..0000000000 --- a/java/broker/src/main/java/org/apache/qpid/server/filter/BinaryExpression.java +++ /dev/null @@ -1,90 +0,0 @@ -/** - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.qpid.server.filter; -// -// Based on like named file from r450141 of the Apache ActiveMQ project -// - -/** - * An expression which performs an operation on two expression values. - */ -public abstract class BinaryExpression implements Expression -{ - private final Expression left; - private final Expression right; - - public BinaryExpression(Expression left, Expression right) - { - this.left = left; - this.right = right; - } - - public Expression getLeft() - { - return left; - } - - public Expression getRight() - { - return right; - } - - /** - * @see java.lang.Object#toString() - */ - public String toString() - { - return "(" + left.toString() + " " + getExpressionSymbol() + " " + right.toString() + ")"; - } - - /** - * TODO: more efficient hashCode() - * - * @see java.lang.Object#hashCode() - */ - public int hashCode() - { - return toString().hashCode(); - } - - /** - * TODO: more efficient hashCode() - * - * @see java.lang.Object#equals(java.lang.Object) - */ - public boolean equals(Object o) - { - - if ((o == null) || !this.getClass().equals(o.getClass())) - { - return false; - } - - return toString().equals(o.toString()); - - } - - /** - * Returns the symbol that represents this binary expression. For example, addition is - * represented by "+" - * - * @return - */ - public abstract String getExpressionSymbol(); - -} diff --git a/java/broker/src/main/java/org/apache/qpid/server/filter/BooleanExpression.java b/java/broker/src/main/java/org/apache/qpid/server/filter/BooleanExpression.java deleted file mode 100644 index 06e8664470..0000000000 --- a/java/broker/src/main/java/org/apache/qpid/server/filter/BooleanExpression.java +++ /dev/null @@ -1,38 +0,0 @@ -/** - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.qpid.server.filter; -// -// Based on like named file from r450141 of the Apache ActiveMQ project -// - -import org.apache.qpid.server.queue.Filterable; - -/** - * A BooleanExpression is an expression that always - * produces a Boolean result. - */ -public interface BooleanExpression extends Expression -{ - - /** - * @param message - * @return true if the expression evaluates to Boolean.TRUE. - */ - public boolean matches(Filterable message); - -} diff --git a/java/broker/src/main/java/org/apache/qpid/server/filter/ComparisonExpression.java b/java/broker/src/main/java/org/apache/qpid/server/filter/ComparisonExpression.java deleted file mode 100644 index b04197121c..0000000000 --- a/java/broker/src/main/java/org/apache/qpid/server/filter/ComparisonExpression.java +++ /dev/null @@ -1,598 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.server.filter; -// -// Based on like named file from r450141 of the Apache ActiveMQ project -// - -import org.apache.qpid.server.queue.Filterable; - -import java.util.HashSet; -import java.util.List; -import java.util.regex.Pattern; - -/** - * A filter performing a comparison of two objects - */ -public abstract class ComparisonExpression extends BinaryExpression implements BooleanExpression -{ - - public static BooleanExpression createBetween(Expression value, Expression left, Expression right) - { - return LogicExpression.createAND(createGreaterThanEqual(value, left), createLessThanEqual(value, right)); - } - - public static BooleanExpression createNotBetween(Expression value, Expression left, Expression right) - { - return LogicExpression.createOR(createLessThan(value, left), createGreaterThan(value, right)); - } - - private static final HashSet REGEXP_CONTROL_CHARS = new HashSet(); - - static - { - REGEXP_CONTROL_CHARS.add('.'); - REGEXP_CONTROL_CHARS.add('\\'); - REGEXP_CONTROL_CHARS.add('['); - REGEXP_CONTROL_CHARS.add(']'); - REGEXP_CONTROL_CHARS.add('^'); - REGEXP_CONTROL_CHARS.add('$'); - REGEXP_CONTROL_CHARS.add('?'); - REGEXP_CONTROL_CHARS.add('*'); - REGEXP_CONTROL_CHARS.add('+'); - REGEXP_CONTROL_CHARS.add('{'); - REGEXP_CONTROL_CHARS.add('}'); - REGEXP_CONTROL_CHARS.add('|'); - REGEXP_CONTROL_CHARS.add('('); - REGEXP_CONTROL_CHARS.add(')'); - REGEXP_CONTROL_CHARS.add(':'); - REGEXP_CONTROL_CHARS.add('&'); - REGEXP_CONTROL_CHARS.add('<'); - REGEXP_CONTROL_CHARS.add('>'); - REGEXP_CONTROL_CHARS.add('='); - REGEXP_CONTROL_CHARS.add('!'); - } - - static class LikeExpression extends UnaryExpression implements BooleanExpression - { - - private Pattern likePattern; - - /** - * @param right - */ - public LikeExpression(Expression right, String like, int escape) - { - super(right); - - StringBuffer regexp = new StringBuffer(like.length() * 2); - regexp.append("\\A"); // The beginning of the input - for (int i = 0; i < like.length(); i++) - { - char c = like.charAt(i); - if (escape == (0xFFFF & c)) - { - i++; - if (i >= like.length()) - { - // nothing left to escape... - break; - } - - char t = like.charAt(i); - regexp.append("\\x"); - regexp.append(Integer.toHexString(0xFFFF & t)); - } - else if (c == '%') - { - regexp.append(".*?"); // Do a non-greedy match - } - else if (c == '_') - { - regexp.append("."); // match one - } - else if (REGEXP_CONTROL_CHARS.contains(c)) - { - regexp.append("\\x"); - regexp.append(Integer.toHexString(0xFFFF & c)); - } - else - { - regexp.append(c); - } - } - - regexp.append("\\z"); // The end of the input - - likePattern = Pattern.compile(regexp.toString(), Pattern.DOTALL); - } - - /** - * org.apache.activemq.filter.UnaryExpression#getExpressionSymbol() - */ - public String getExpressionSymbol() - { - return "LIKE"; - } - - /** - * org.apache.activemq.filter.Expression#evaluate(MessageEvaluationContext) - */ - public Object evaluate(Filterable message) - { - - Object rv = this.getRight().evaluate(message); - - if (rv == null) - { - return null; - } - - if (!(rv instanceof String)) - { - return - Boolean.FALSE; - } - - return likePattern.matcher((String) rv).matches() ? Boolean.TRUE : Boolean.FALSE; - } - - public boolean matches(Filterable message) - { - Object object = evaluate(message); - - return (object != null) && (object == Boolean.TRUE); - } - } - - public static BooleanExpression createLike(Expression left, String right, String escape) - { - if ((escape != null) && (escape.length() != 1)) - { - throw new RuntimeException( - "The ESCAPE string litteral is invalid. It can only be one character. Litteral used: " + escape); - } - - int c = -1; - if (escape != null) - { - c = 0xFFFF & escape.charAt(0); - } - - return new LikeExpression(left, right, c); - } - - public static BooleanExpression createNotLike(Expression left, String right, String escape) - { - return UnaryExpression.createNOT(createLike(left, right, escape)); - } - - public static BooleanExpression createInFilter(Expression left, List elements) - { - - if (!(left instanceof PropertyExpression)) - { - throw new RuntimeException("Expected a property for In expression, got: " + left); - } - - return UnaryExpression.createInExpression((PropertyExpression) left, elements, false); - - } - - public static BooleanExpression createNotInFilter(Expression left, List elements) - { - - if (!(left instanceof PropertyExpression)) - { - throw new RuntimeException("Expected a property for In expression, got: " + left); - } - - return UnaryExpression.createInExpression((PropertyExpression) left, elements, true); - - } - - public static BooleanExpression createIsNull(Expression left) - { - return doCreateEqual(left, ConstantExpression.NULL); - } - - public static BooleanExpression createIsNotNull(Expression left) - { - return UnaryExpression.createNOT(doCreateEqual(left, ConstantExpression.NULL)); - } - - public static BooleanExpression createNotEqual(Expression left, Expression right) - { - return UnaryExpression.createNOT(createEqual(left, right)); - } - - public static BooleanExpression createEqual(Expression left, Expression right) - { - checkEqualOperand(left); - checkEqualOperand(right); - checkEqualOperandCompatability(left, right); - - return doCreateEqual(left, right); - } - - private static BooleanExpression doCreateEqual(Expression left, Expression right) - { - return new EqualExpression(left, right); - } - - public static BooleanExpression createGreaterThan(final Expression left, final Expression right) - { - checkLessThanOperand(left); - checkLessThanOperand(right); - - return new ComparisonExpression(left, right) - { - protected boolean asBoolean(int answer) - { - return answer > 0; - } - - public String getExpressionSymbol() - { - return ">"; - } - }; - } - - public static BooleanExpression createGreaterThanEqual(final Expression left, final Expression right) - { - checkLessThanOperand(left); - checkLessThanOperand(right); - - return new ComparisonExpression(left, right) - { - protected boolean asBoolean(int answer) - { - return answer >= 0; - } - - public String getExpressionSymbol() - { - return ">="; - } - }; - } - - public static BooleanExpression createLessThan(final Expression left, final Expression right) - { - checkLessThanOperand(left); - checkLessThanOperand(right); - - return new ComparisonExpression(left, right) - { - - protected boolean asBoolean(int answer) - { - return answer < 0; - } - - public String getExpressionSymbol() - { - return "<"; - } - - }; - } - - public static BooleanExpression createLessThanEqual(final Expression left, final Expression right) - { - checkLessThanOperand(left); - checkLessThanOperand(right); - - return new ComparisonExpression(left, right) - { - - protected boolean asBoolean(int answer) - { - return answer <= 0; - } - - public String getExpressionSymbol() - { - return "<="; - } - }; - } - - /** - * Only Numeric expressions can be used in >, >=, < or <= expressions.s - * - * @param expr - */ - public static void checkLessThanOperand(Expression expr) - { - if (expr instanceof ConstantExpression) - { - Object value = ((ConstantExpression) expr).getValue(); - if (value instanceof Number) - { - return; - } - - // Else it's boolean or a String.. - throw new RuntimeException("Value '" + expr + "' cannot be compared."); - } - - if (expr instanceof BooleanExpression) - { - throw new RuntimeException("Value '" + expr + "' cannot be compared."); - } - } - - /** - * Validates that the expression can be used in == or <> expression. - * Cannot not be NULL TRUE or FALSE litterals. - * - * @param expr - */ - public static void checkEqualOperand(Expression expr) - { - if (expr instanceof ConstantExpression) - { - Object value = ((ConstantExpression) expr).getValue(); - if (value == null) - { - throw new RuntimeException("'" + expr + "' cannot be compared."); - } - } - } - - /** - * - * @param left - * @param right - */ - private static void checkEqualOperandCompatability(Expression left, Expression right) - { - if ((left instanceof ConstantExpression) && (right instanceof ConstantExpression)) - { - if ((left instanceof BooleanExpression) && !(right instanceof BooleanExpression)) - { - throw new RuntimeException("'" + left + "' cannot be compared with '" + right + "'"); - } - } - } - - /** - * @param left - * @param right - */ - public ComparisonExpression(Expression left, Expression right) - { - super(left, right); - } - - public Object evaluate(Filterable message) - { - Comparable lv = (Comparable) getLeft().evaluate(message); - if (lv == null) - { - return null; - } - - Comparable rv = (Comparable) getRight().evaluate(message); - if (rv == null) - { - return null; - } - - return compare(lv, rv); - } - - protected Boolean compare(Comparable lv, Comparable rv) - { - Class lc = lv.getClass(); - Class rc = rv.getClass(); - // If the the objects are not of the same type, - // try to convert up to allow the comparison. - if (lc != rc) - { - if (lc == Byte.class) - { - if (rc == Short.class) - { - lv = ((Number) lv).shortValue(); - } - else if (rc == Integer.class) - { - lv = ((Number) lv).intValue(); - } - else if (rc == Long.class) - { - lv = ((Number) lv).longValue(); - } - else if (rc == Float.class) - { - lv = ((Number) lv).floatValue(); - } - else if (rc == Double.class) - { - lv = ((Number) lv).doubleValue(); - } - else - { - return Boolean.FALSE; - } - } - else if (lc == Short.class) - { - if (rc == Integer.class) - { - lv = ((Number) lv).intValue(); - } - else if (rc == Long.class) - { - lv = ((Number) lv).longValue(); - } - else if (rc == Float.class) - { - lv = ((Number) lv).floatValue(); - } - else if (rc == Double.class) - { - lv = ((Number) lv).doubleValue(); - } - else - { - return Boolean.FALSE; - } - } - else if (lc == Integer.class) - { - if (rc == Long.class) - { - lv = ((Number) lv).longValue(); - } - else if (rc == Float.class) - { - lv = ((Number) lv).floatValue(); - } - else if (rc == Double.class) - { - lv = ((Number) lv).doubleValue(); - } - else - { - return Boolean.FALSE; - } - } - else if (lc == Long.class) - { - if (rc == Integer.class) - { - rv = ((Number) rv).longValue(); - } - else if (rc == Float.class) - { - lv = ((Number) lv).floatValue(); - } - else if (rc == Double.class) - { - lv = ((Number) lv).doubleValue(); - } - else - { - return Boolean.FALSE; - } - } - else if (lc == Float.class) - { - if (rc == Integer.class) - { - rv = ((Number) rv).floatValue(); - } - else if (rc == Long.class) - { - rv = ((Number) rv).floatValue(); - } - else if (rc == Double.class) - { - lv = ((Number) lv).doubleValue(); - } - else - { - return Boolean.FALSE; - } - } - else if (lc == Double.class) - { - if (rc == Integer.class) - { - rv = ((Number) rv).doubleValue(); - } - else if (rc == Long.class) - { - rv = ((Number) rv).doubleValue(); - } - else if (rc == Float.class) - { - rv = ((Number) rv).doubleValue(); - } - else - { - return Boolean.FALSE; - } - } - else - { - return Boolean.FALSE; - } - } - - return asBoolean(lv.compareTo(rv)) ? Boolean.TRUE : Boolean.FALSE; - } - - protected abstract boolean asBoolean(int answer); - - public boolean matches(Filterable message) - { - Object object = evaluate(message); - - return (object != null) && (object == Boolean.TRUE); - } - - private static class EqualExpression extends ComparisonExpression - { - public EqualExpression(final Expression left, final Expression right) - { - super(left, right); - } - - public Object evaluate(Filterable message) - { - Object lv = getLeft().evaluate(message); - Object rv = getRight().evaluate(message); - - // Iff one of the values is null - if ((lv == null) ^ (rv == null)) - { - return Boolean.FALSE; - } - - if ((lv == rv) || lv.equals(rv)) - { - return Boolean.TRUE; - } - - if ((lv instanceof Comparable) && (rv instanceof Comparable)) - { - return compare((Comparable) lv, (Comparable) rv); - } - - return Boolean.FALSE; - } - - protected boolean asBoolean(int answer) - { - return answer == 0; - } - - public String getExpressionSymbol() - { - return "="; - } - } -} diff --git a/java/broker/src/main/java/org/apache/qpid/server/filter/ConstantExpression.java b/java/broker/src/main/java/org/apache/qpid/server/filter/ConstantExpression.java deleted file mode 100644 index 5956ba0aa0..0000000000 --- a/java/broker/src/main/java/org/apache/qpid/server/filter/ConstantExpression.java +++ /dev/null @@ -1,209 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.server.filter; -// -// Based on like named file from r450141 of the Apache ActiveMQ project -// - -import org.apache.qpid.server.queue.Filterable; - -import java.math.BigDecimal; - -/** - * Represents a constant expression - */ -public class ConstantExpression implements Expression -{ - - static class BooleanConstantExpression extends ConstantExpression implements BooleanExpression - { - public BooleanConstantExpression(Object value) - { - super(value); - } - - public boolean matches(Filterable message) - { - Object object = evaluate(message); - - return (object != null) && (object == Boolean.TRUE); - } - } - - public static final BooleanConstantExpression NULL = new BooleanConstantExpression(null); - public static final BooleanConstantExpression TRUE = new BooleanConstantExpression(Boolean.TRUE); - public static final BooleanConstantExpression FALSE = new BooleanConstantExpression(Boolean.FALSE); - - private Object value; - - public static ConstantExpression createFromDecimal(String text) - { - - // Strip off the 'l' or 'L' if needed. - if (text.endsWith("l") || text.endsWith("L")) - { - text = text.substring(0, text.length() - 1); - } - - Number value; - try - { - value = new Long(text); - } - catch (NumberFormatException e) - { - // The number may be too big to fit in a long. - value = new BigDecimal(text); - } - - long l = value.longValue(); - if ((Integer.MIN_VALUE <= l) && (l <= Integer.MAX_VALUE)) - { - value = value.intValue(); - } - - return new ConstantExpression(value); - } - - public static ConstantExpression createFromHex(String text) - { - Number value = Long.parseLong(text.substring(2), 16); - long l = value.longValue(); - if ((Integer.MIN_VALUE <= l) && (l <= Integer.MAX_VALUE)) - { - value = value.intValue(); - } - - return new ConstantExpression(value); - } - - public static ConstantExpression createFromOctal(String text) - { - Number value = Long.parseLong(text, 8); - long l = value.longValue(); - if ((Integer.MIN_VALUE <= l) && (l <= Integer.MAX_VALUE)) - { - value = value.intValue(); - } - - return new ConstantExpression(value); - } - - public static ConstantExpression createFloat(String text) - { - Number value = new Double(text); - - return new ConstantExpression(value); - } - - public ConstantExpression(Object value) - { - this.value = value; - } - - public Object evaluate(Filterable message) - { - return value; - } - - public Object getValue() - { - return value; - } - - /** - * @see java.lang.Object#toString() - */ - public String toString() - { - if (value == null) - { - return "NULL"; - } - - if (value instanceof Boolean) - { - return ((Boolean) value) ? "TRUE" : "FALSE"; - } - - if (value instanceof String) - { - return encodeString((String) value); - } - - return value.toString(); - } - - /** - * TODO: more efficient hashCode() - * - * @see java.lang.Object#hashCode() - */ - public int hashCode() - { - return toString().hashCode(); - } - - /** - * TODO: more efficient hashCode() - * - * @see java.lang.Object#equals(java.lang.Object) - */ - public boolean equals(Object o) - { - - if ((o == null) || !this.getClass().equals(o.getClass())) - { - return false; - } - - return toString().equals(o.toString()); - - } - - /** - * Encodes the value of string so that it looks like it would look like - * when it was provided in a selector. - * - * @param s - * @return - */ - public static String encodeString(String s) - { - StringBuffer b = new StringBuffer(); - b.append('\''); - for (int i = 0; i < s.length(); i++) - { - char c = s.charAt(i); - if (c == '\'') - { - b.append(c); - } - - b.append(c); - } - - b.append('\''); - - return b.toString(); - } - -} diff --git a/java/broker/src/main/java/org/apache/qpid/server/filter/Expression.java b/java/broker/src/main/java/org/apache/qpid/server/filter/Expression.java deleted file mode 100644 index 97e9915271..0000000000 --- a/java/broker/src/main/java/org/apache/qpid/server/filter/Expression.java +++ /dev/null @@ -1,36 +0,0 @@ -/** - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.qpid.server.filter; -// -// Based on like named file from r450141 of the Apache ActiveMQ project -// - -import org.apache.qpid.server.queue.Filterable; - -/** - * Represents an expression - */ -public interface Expression -{ - - /** - * @return the value of this expression - */ - public Object evaluate(Filterable message); - -} diff --git a/java/broker/src/main/java/org/apache/qpid/server/filter/FilterManagerFactory.java b/java/broker/src/main/java/org/apache/qpid/server/filter/FilterManagerFactory.java index 45cd3cb57e..56f0a6e08d 100644 --- a/java/broker/src/main/java/org/apache/qpid/server/filter/FilterManagerFactory.java +++ b/java/broker/src/main/java/org/apache/qpid/server/filter/FilterManagerFactory.java @@ -23,7 +23,11 @@ package org.apache.qpid.server.filter; import org.apache.log4j.Logger; import org.apache.qpid.AMQException; +import org.apache.qpid.AMQInvalidArgumentException; import org.apache.qpid.common.AMQPFilterTypes; +import org.apache.qpid.filter.SelectorParsingException; +import org.apache.qpid.filter.selector.ParseException; +import org.apache.qpid.filter.selector.TokenMgrError; import org.apache.qpid.framing.FieldTable; import java.util.Map; @@ -56,7 +60,22 @@ public class FilterManagerFactory if (selector != null && !selector.equals("")) { manager = new SimpleFilterManager(); - manager.add(new JMSSelectorFilter(selector)); + try + { + manager.add(new JMSSelectorFilter(selector)); + } + catch (ParseException e) + { + throw new AMQInvalidArgumentException("Cannot parse JMS selector \"" + selector + "\"", e); + } + catch (SelectorParsingException e) + { + throw new AMQInvalidArgumentException("Cannot parse JMS selector \"" + selector + "\"", e); + } + catch (TokenMgrError e) + { + throw new AMQInvalidArgumentException("Cannot parse JMS selector \"" + selector + "\"", e); + } } } diff --git a/java/broker/src/main/java/org/apache/qpid/server/filter/JMSSelectorFilter.java b/java/broker/src/main/java/org/apache/qpid/server/filter/JMSSelectorFilter.java index 95fe442271..47cacdc176 100644 --- a/java/broker/src/main/java/org/apache/qpid/server/filter/JMSSelectorFilter.java +++ b/java/broker/src/main/java/org/apache/qpid/server/filter/JMSSelectorFilter.java @@ -22,8 +22,12 @@ package org.apache.qpid.server.filter; import org.apache.log4j.Logger; -import org.apache.qpid.AMQInvalidArgumentException; -import org.apache.qpid.server.filter.jms.selector.SelectorParser; +import org.apache.qpid.filter.BooleanExpression; +import org.apache.qpid.filter.FilterableMessage; +import org.apache.qpid.filter.SelectorParsingException; +import org.apache.qpid.filter.selector.ParseException; +import org.apache.qpid.filter.selector.SelectorParser; +import org.apache.qpid.filter.selector.TokenMgrError; import org.apache.qpid.server.queue.Filterable; @@ -34,7 +38,7 @@ public class JMSSelectorFilter implements MessageFilter private String _selector; private BooleanExpression _matcher; - public JMSSelectorFilter(String selector) throws AMQInvalidArgumentException + public JMSSelectorFilter(String selector) throws ParseException, TokenMgrError, SelectorParsingException { _selector = selector; _matcher = new SelectorParser().parse(selector); @@ -42,7 +46,8 @@ public class JMSSelectorFilter implements MessageFilter public boolean matches(Filterable message) { - boolean match = _matcher.matches(message); + + boolean match = _matcher.matches(wrap(message)); if(_logger.isDebugEnabled()) { _logger.debug(message + " match(" + match + ") selector(" + System.identityHashCode(_selector) + "):" + _selector); @@ -50,6 +55,62 @@ public class JMSSelectorFilter implements MessageFilter return match; } + private FilterableMessage wrap(final Filterable message) + { + return new FilterableMessage() + { + public boolean isPersistent() + { + return message.isPersistent(); + } + + public boolean isRedelivered() + { + return message.isRedelivered(); + } + + public Object getHeader(String name) + { + return message.getMessageHeader().getHeader(name); + } + + public String getReplyTo() + { + return message.getMessageHeader().getReplyTo(); + } + + public String getType() + { + return message.getMessageHeader().getType(); + } + + public byte getPriority() + { + return message.getMessageHeader().getPriority(); + } + + public String getMessageId() + { + return message.getMessageHeader().getMessageId(); + } + + public long getTimestamp() + { + return message.getMessageHeader().getTimestamp(); + } + + public String getCorrelationId() + { + return message.getMessageHeader().getCorrelationId(); + } + + public long getExpiration() + { + return message.getMessageHeader().getExpiration(); + } + }; + } + public String getSelector() { return _selector; diff --git a/java/broker/src/main/java/org/apache/qpid/server/filter/LogicExpression.java b/java/broker/src/main/java/org/apache/qpid/server/filter/LogicExpression.java deleted file mode 100644 index f867b4d4c5..0000000000 --- a/java/broker/src/main/java/org/apache/qpid/server/filter/LogicExpression.java +++ /dev/null @@ -1,120 +0,0 @@ -/** - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.qpid.server.filter; -// -// Based on like named file from r450141 of the Apache ActiveMQ project -// - -import org.apache.qpid.server.queue.Filterable; - -/** - * A filter performing a comparison of two objects - */ -public abstract class LogicExpression extends BinaryExpression implements BooleanExpression -{ - - public static BooleanExpression createOR(BooleanExpression lvalue, BooleanExpression rvalue) - { - return new OrExpression(lvalue, rvalue); - } - - public static BooleanExpression createAND(BooleanExpression lvalue, BooleanExpression rvalue) - { - return new AndExpression(lvalue, rvalue); - } - - /** - * @param left - * @param right - */ - public LogicExpression(BooleanExpression left, BooleanExpression right) - { - super(left, right); - } - - public abstract Object evaluate(Filterable message); - - public boolean matches(Filterable message) - { - Object object = evaluate(message); - - return (object != null) && (object == Boolean.TRUE); - } - - private static class OrExpression extends LogicExpression - { - public OrExpression(final BooleanExpression lvalue, final BooleanExpression rvalue) - { - super(lvalue, rvalue); - } - - public Object evaluate(Filterable message) - { - - Boolean lv = (Boolean) getLeft().evaluate(message); - // Can we do an OR shortcut?? - if ((lv != null) && lv.booleanValue()) - { - return Boolean.TRUE; - } - - Boolean rv = (Boolean) getRight().evaluate(message); - - return (rv == null) ? null : rv; - } - - public String getExpressionSymbol() - { - return "OR"; - } - } - - private static class AndExpression extends LogicExpression - { - public AndExpression(final BooleanExpression lvalue, final BooleanExpression rvalue) - { - super(lvalue, rvalue); - } - - public Object evaluate(Filterable message) - { - - Boolean lv = (Boolean) getLeft().evaluate(message); - - // Can we do an AND shortcut?? - if (lv == null) - { - return null; - } - - if (!lv.booleanValue()) - { - return Boolean.FALSE; - } - - Boolean rv = (Boolean) getRight().evaluate(message); - - return (rv == null) ? null : rv; - } - - public String getExpressionSymbol() - { - return "AND"; - } - } -} diff --git a/java/broker/src/main/java/org/apache/qpid/server/filter/PropertyExpression.java b/java/broker/src/main/java/org/apache/qpid/server/filter/PropertyExpression.java deleted file mode 100644 index d6a77be78b..0000000000 --- a/java/broker/src/main/java/org/apache/qpid/server/filter/PropertyExpression.java +++ /dev/null @@ -1,234 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.server.filter; -// -// Based on like named file from r450141 of the Apache ActiveMQ project -// - -import org.apache.log4j.Logger; - -import org.apache.qpid.server.queue.Filterable; - -import java.util.HashMap; - -/** - * Represents a property expression - */ -public class PropertyExpression implements Expression -{ - // Constants - defined the same as JMS - private static enum JMSDeliveryMode { NON_PERSISTENT, PERSISTENT } - - private static final int DEFAULT_PRIORITY = 4; - - private static final Logger _logger = org.apache.log4j.Logger.getLogger(PropertyExpression.class); - - private static final HashMap JMS_PROPERTY_EXPRESSIONS = new HashMap(); - - { - JMS_PROPERTY_EXPRESSIONS.put("JMSDestination", new Expression() - { - public Object evaluate(Filterable message) - { - //TODO - return null; - } - }); - JMS_PROPERTY_EXPRESSIONS.put("JMSReplyTo", new ReplyToExpression()); - - JMS_PROPERTY_EXPRESSIONS.put("JMSType", new TypeExpression()); - - JMS_PROPERTY_EXPRESSIONS.put("JMSDeliveryMode", new DeliveryModeExpression()); - - JMS_PROPERTY_EXPRESSIONS.put("JMSPriority", new PriorityExpression()); - - JMS_PROPERTY_EXPRESSIONS.put("JMSMessageID", new MessageIDExpression()); - - JMS_PROPERTY_EXPRESSIONS.put("AMQMessageID", new MessageIDExpression()); - - JMS_PROPERTY_EXPRESSIONS.put("JMSTimestamp", new TimestampExpression()); - - JMS_PROPERTY_EXPRESSIONS.put("JMSCorrelationID", new CorrelationIdExpression()); - - JMS_PROPERTY_EXPRESSIONS.put("JMSExpiration", new ExpirationExpression()); - - JMS_PROPERTY_EXPRESSIONS.put("JMSRedelivered", new Expression() - { - public Object evaluate(Filterable message) - { - return message.isRedelivered(); - } - }); - } - - private final String name; - private final Expression jmsPropertyExpression; - - public boolean outerTest() - { - return false; - } - - public PropertyExpression(String name) - { - this.name = name; - - - - jmsPropertyExpression = (Expression) JMS_PROPERTY_EXPRESSIONS.get(name); - } - - public Object evaluate(Filterable message) - { - - if (jmsPropertyExpression != null) - { - return jmsPropertyExpression.evaluate(message); - } - else - { - return message.getMessageHeader().getHeader(name); - } - } - - public String getName() - { - return name; - } - - /** - * @see java.lang.Object#toString() - */ - public String toString() - { - return name; - } - - /** - * @see java.lang.Object#hashCode() - */ - public int hashCode() - { - return name.hashCode(); - } - - /** - * @see java.lang.Object#equals(java.lang.Object) - */ - public boolean equals(Object o) - { - - if ((o == null) || !this.getClass().equals(o.getClass())) - { - return false; - } - - return name.equals(((PropertyExpression) o).name); - - } - - private static class ReplyToExpression implements Expression - { - public Object evaluate(Filterable message) - { - String replyTo = message.getMessageHeader().getReplyTo(); - return replyTo; - } - - } - - private static class TypeExpression implements Expression - { - public Object evaluate(Filterable message) - { - - String type = message.getMessageHeader().getType(); - return type; - - } - } - - private static class DeliveryModeExpression implements Expression - { - public Object evaluate(Filterable message) - { - JMSDeliveryMode mode = message.isPersistent() ? JMSDeliveryMode.PERSISTENT : - JMSDeliveryMode.NON_PERSISTENT; - if (_logger.isDebugEnabled()) - { - _logger.debug("JMSDeliveryMode is :" + mode); - } - - return mode.toString(); - } - } - - private static class PriorityExpression implements Expression - { - public Object evaluate(Filterable message) - { - byte priority = message.getMessageHeader().getPriority(); - return (int) priority; - } - } - - private static class MessageIDExpression implements Expression - { - public Object evaluate(Filterable message) - { - - String messageId = message.getMessageHeader().getMessageId(); - - return messageId; - - } - } - - private static class TimestampExpression implements Expression - { - public Object evaluate(Filterable message) - { - long timestamp = message.getMessageHeader().getTimestamp(); - return timestamp; - } - } - - private static class CorrelationIdExpression implements Expression - { - public Object evaluate(Filterable message) - { - - String correlationId = message.getMessageHeader().getCorrelationId(); - - return correlationId; - } - } - - private static class ExpirationExpression implements Expression - { - public Object evaluate(Filterable message) - { - long expiration = message.getMessageHeader().getExpiration(); - return expiration; - - } - } -} diff --git a/java/broker/src/main/java/org/apache/qpid/server/filter/UnaryExpression.java b/java/broker/src/main/java/org/apache/qpid/server/filter/UnaryExpression.java deleted file mode 100644 index fdd651a046..0000000000 --- a/java/broker/src/main/java/org/apache/qpid/server/filter/UnaryExpression.java +++ /dev/null @@ -1,361 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.server.filter; -// -// Based on like named file from r450141 of the Apache ActiveMQ project -// - -import org.apache.qpid.server.queue.Filterable; - -import java.math.BigDecimal; -import java.util.Collection; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; - -/** - * An expression which performs an operation on two expression values - */ -public abstract class UnaryExpression implements Expression -{ - - private static final BigDecimal BD_LONG_MIN_VALUE = BigDecimal.valueOf(Long.MIN_VALUE); - private Expression right; - - public static Expression createNegate(Expression left) - { - return new NegativeExpression(left); - } - - public static BooleanExpression createInExpression(PropertyExpression right, List elements, final boolean not) - { - - // Use a HashSet if there are many elements. - Collection t; - if (elements.size() == 0) - { - t = null; - } - else if (elements.size() < 5) - { - t = elements; - } - else - { - t = new HashSet(elements); - } - - final Collection inList = t; - - return new InExpression(right, inList, not); - } - - abstract static class BooleanUnaryExpression extends UnaryExpression implements BooleanExpression - { - public BooleanUnaryExpression(Expression left) - { - super(left); - } - - public boolean matches(Filterable message) - { - Object object = evaluate(message); - - return (object != null) && (object == Boolean.TRUE); - } - } - ; - - public static BooleanExpression createNOT(BooleanExpression left) - { - return new NotExpression(left); - } - - public static BooleanExpression createXPath(final String xpath) - { - return new XPathExpression(xpath); - } - - public static BooleanExpression createXQuery(final String xpath) - { - return new XQueryExpression(xpath); - } - - public static BooleanExpression createBooleanCast(Expression left) - { - return new BooleanCastExpression(left); - } - - private static Number negate(Number left) - { - Class clazz = left.getClass(); - if (clazz == Integer.class) - { - return -left.intValue(); - } - else if (clazz == Long.class) - { - return -left.longValue(); - } - else if (clazz == Float.class) - { - return -left.floatValue(); - } - else if (clazz == Double.class) - { - return -left.doubleValue(); - } - else if (clazz == BigDecimal.class) - { - // We ussually get a big deciamal when we have Long.MIN_VALUE constant in the - // Selector. Long.MIN_VALUE is too big to store in a Long as a positive so we store it - // as a Big decimal. But it gets Negated right away.. to here we try to covert it back - // to a Long. - BigDecimal bd = (BigDecimal) left; - bd = bd.negate(); - - if (BD_LONG_MIN_VALUE.compareTo(bd) == 0) - { - return Long.MIN_VALUE; - } - - return bd; - } - else - { - throw new RuntimeException("Don't know how to negate: " + left); - } - } - - public UnaryExpression(Expression left) - { - this.right = left; - } - - public Expression getRight() - { - return right; - } - - public void setRight(Expression expression) - { - right = expression; - } - - /** - * @see java.lang.Object#toString() - */ - public String toString() - { - return "(" + getExpressionSymbol() + " " + right.toString() + ")"; - } - - /** - * TODO: more efficient hashCode() - * - * @see java.lang.Object#hashCode() - */ - public int hashCode() - { - return toString().hashCode(); - } - - /** - * TODO: more efficient hashCode() - * - * @see java.lang.Object#equals(java.lang.Object) - */ - public boolean equals(Object o) - { - return ((o != null) && this.getClass().equals(o.getClass())) && toString().equals(o.toString()); - } - - /** - * Returns the symbol that represents this binary expression. For example, addition is - * represented by "+" - * - * @return - */ - public abstract String getExpressionSymbol(); - - private static class NegativeExpression extends UnaryExpression - { - public NegativeExpression(final Expression left) - { - super(left); - } - - public Object evaluate(Filterable message) - { - Object rvalue = getRight().evaluate(message); - if (rvalue == null) - { - return null; - } - - if (rvalue instanceof Number) - { - return negate((Number) rvalue); - } - - return null; - } - - public String getExpressionSymbol() - { - return "-"; - } - } - - private static class InExpression extends BooleanUnaryExpression - { - private final Collection _inList; - private final boolean _not; - - public InExpression(final PropertyExpression right, final Collection inList, final boolean not) - { - super(right); - _inList = inList; - _not = not; - } - - public Object evaluate(Filterable message) - { - - Object rvalue = getRight().evaluate(message); - if (rvalue == null) - { - return null; - } - - if (rvalue.getClass() != String.class) - { - return null; - } - - if (((_inList != null) && _inList.contains(rvalue)) ^ _not) - { - return Boolean.TRUE; - } - else - { - return Boolean.FALSE; - } - - } - - public String toString() - { - StringBuffer answer = new StringBuffer(); - answer.append(getRight()); - answer.append(" "); - answer.append(getExpressionSymbol()); - answer.append(" ( "); - - int count = 0; - for (Iterator i = _inList.iterator(); i.hasNext();) - { - Object o = (Object) i.next(); - if (count != 0) - { - answer.append(", "); - } - - answer.append(o); - count++; - } - - answer.append(" )"); - - return answer.toString(); - } - - public String getExpressionSymbol() - { - if (_not) - { - return "NOT IN"; - } - else - { - return "IN"; - } - } - } - - private static class NotExpression extends BooleanUnaryExpression - { - public NotExpression(final BooleanExpression left) - { - super(left); - } - - public Object evaluate(Filterable message) - { - Boolean lvalue = (Boolean) getRight().evaluate(message); - if (lvalue == null) - { - return null; - } - - return lvalue.booleanValue() ? Boolean.FALSE : Boolean.TRUE; - } - - public String getExpressionSymbol() - { - return "NOT"; - } - } - - private static class BooleanCastExpression extends BooleanUnaryExpression - { - public BooleanCastExpression(final Expression left) - { - super(left); - } - - public Object evaluate(Filterable message) - { - Object rvalue = getRight().evaluate(message); - if (rvalue == null) - { - return null; - } - - if (!rvalue.getClass().equals(Boolean.class)) - { - return Boolean.FALSE; - } - - return ((Boolean) rvalue).booleanValue() ? Boolean.TRUE : Boolean.FALSE; - } - - public String toString() - { - return getRight().toString(); - } - - public String getExpressionSymbol() - { - return ""; - } - } -} diff --git a/java/broker/src/main/java/org/apache/qpid/server/filter/XPathExpression.java b/java/broker/src/main/java/org/apache/qpid/server/filter/XPathExpression.java deleted file mode 100644 index ead385d1ba..0000000000 --- a/java/broker/src/main/java/org/apache/qpid/server/filter/XPathExpression.java +++ /dev/null @@ -1,113 +0,0 @@ -/** - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.qpid.server.filter; -// -// Based on like named file from r450141 of the Apache ActiveMQ project -// - -import org.apache.log4j.Logger; - -import org.apache.qpid.server.queue.Filterable; - -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; - -/** - * Used to evaluate an XPath Expression in a JMS selector. - */ -public final class XPathExpression implements BooleanExpression { - - private static final Logger log = Logger.getLogger(XPathExpression.class); - private static final String EVALUATOR_SYSTEM_PROPERTY = "org.apache.qpid.server.filter.XPathEvaluatorClassName"; - private static final String DEFAULT_EVALUATOR_CLASS_NAME=XalanXPathEvaluator.class.getName(); - - private static final Constructor EVALUATOR_CONSTRUCTOR; - - static { - String cn = System.getProperty(EVALUATOR_SYSTEM_PROPERTY, DEFAULT_EVALUATOR_CLASS_NAME); - Constructor m = null; - try { - try { - m = getXPathEvaluatorConstructor(cn); - } catch (Throwable e) { - log.warn("Invalid "+XPathEvaluator.class.getName()+" implementation: "+cn+", reason: "+e,e); - cn = DEFAULT_EVALUATOR_CLASS_NAME; - try { - m = getXPathEvaluatorConstructor(cn); - } catch (Throwable e2) { - log.error("Default XPath evaluator could not be loaded",e); - } - } - } finally { - EVALUATOR_CONSTRUCTOR = m; - } - } - - private static Constructor getXPathEvaluatorConstructor(String cn) throws ClassNotFoundException, SecurityException, NoSuchMethodException { - Class c = XPathExpression.class.getClassLoader().loadClass(cn); - if( !XPathEvaluator.class.isAssignableFrom(c) ) { - throw new ClassCastException(""+c+" is not an instance of "+XPathEvaluator.class); - } - return c.getConstructor(new Class[]{String.class}); - } - - private final String xpath; - private final XPathEvaluator evaluator; - - static public interface XPathEvaluator { - public boolean evaluate(Filterable message); - } - - XPathExpression(String xpath) { - this.xpath = xpath; - this.evaluator = createEvaluator(xpath); - } - - private XPathEvaluator createEvaluator(String xpath2) { - try { - return (XPathEvaluator)EVALUATOR_CONSTRUCTOR.newInstance(new Object[]{xpath}); - } catch (InvocationTargetException e) { - Throwable cause = e.getCause(); - if( cause instanceof RuntimeException ) { - throw (RuntimeException)cause; - } - throw new RuntimeException("Invalid XPath Expression: "+xpath+" reason: "+e.getMessage(), e); - } catch (Throwable e) { - throw new RuntimeException("Invalid XPath Expression: "+xpath+" reason: "+e.getMessage(), e); - } - } - - public Object evaluate(Filterable message) { - return evaluator.evaluate(message) ? Boolean.TRUE : Boolean.FALSE; - } - - public String toString() { - return "XPATH "+ConstantExpression.encodeString(xpath); - } - - /** - * @param message - * @return true if the expression evaluates to Boolean.TRUE. - */ - public boolean matches(Filterable message) - { - Object object = evaluate(message); - return object!=null && object==Boolean.TRUE; - } - -} diff --git a/java/broker/src/main/java/org/apache/qpid/server/filter/XQueryExpression.java b/java/broker/src/main/java/org/apache/qpid/server/filter/XQueryExpression.java deleted file mode 100644 index c16684ce64..0000000000 --- a/java/broker/src/main/java/org/apache/qpid/server/filter/XQueryExpression.java +++ /dev/null @@ -1,55 +0,0 @@ -/** - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.qpid.server.filter; - -import org.apache.qpid.server.queue.Filterable; - -// -// Based on like named file from r450141 of the Apache ActiveMQ project -// - -/** - * Used to evaluate an XQuery Expression in a JMS selector. - */ -public final class XQueryExpression implements BooleanExpression { - private final String xpath; - - XQueryExpression(String xpath) { - super(); - this.xpath = xpath; - } - - public Object evaluate(Filterable message) { - return Boolean.FALSE; - } - - public String toString() { - return "XQUERY "+ConstantExpression.encodeString(xpath); - } - - /** - * @param message - * @return true if the expression evaluates to Boolean.TRUE. - */ - public boolean matches(Filterable message) - { - Object object = evaluate(message); - return object!=null && object==Boolean.TRUE; - } - -} diff --git a/java/broker/src/main/java/org/apache/qpid/server/filter/XalanXPathEvaluator.java b/java/broker/src/main/java/org/apache/qpid/server/filter/XalanXPathEvaluator.java deleted file mode 100644 index ab85cb1b55..0000000000 --- a/java/broker/src/main/java/org/apache/qpid/server/filter/XalanXPathEvaluator.java +++ /dev/null @@ -1,89 +0,0 @@ -/** - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.qpid.server.filter; -// -// Based on like named file from r450141 of the Apache ActiveMQ project -// - -import org.apache.xpath.CachedXPathAPI; -import org.w3c.dom.Document; -import org.w3c.dom.traversal.NodeIterator; -import org.xml.sax.InputSource; - -import org.apache.qpid.server.queue.Filterable; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import java.io.ByteArrayInputStream; -import java.io.StringReader; - -public class XalanXPathEvaluator implements XPathExpression.XPathEvaluator { - - private final String xpath; - - public XalanXPathEvaluator(String xpath) { - this.xpath = xpath; - } - - public boolean evaluate(Filterable m) - { - // TODO - we would have to check the content type and then evaluate the content - // here... is this really a feature we wish to implement? - RobG - return false; - - } - - private boolean evaluate(byte[] data) { - try { - - InputSource inputSource = new InputSource(new ByteArrayInputStream(data)); - - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - factory.setNamespaceAware(true); - DocumentBuilder dbuilder = factory.newDocumentBuilder(); - Document doc = dbuilder.parse(inputSource); - - CachedXPathAPI cachedXPathAPI = new CachedXPathAPI(); - NodeIterator iterator = cachedXPathAPI.selectNodeIterator(doc,xpath); - return iterator.nextNode()!=null; - - } catch (Throwable e) { - return false; - } - } - - private boolean evaluate(String text) { - try { - InputSource inputSource = new InputSource(new StringReader(text)); - - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - factory.setNamespaceAware(true); - DocumentBuilder dbuilder = factory.newDocumentBuilder(); - Document doc = dbuilder.parse(inputSource); - - // We should associated the cachedXPathAPI object with the message being evaluated - // since that should speedup subsequent xpath expressions. - CachedXPathAPI cachedXPathAPI = new CachedXPathAPI(); - NodeIterator iterator = cachedXPathAPI.selectNodeIterator(doc,xpath); - return iterator.nextNode()!=null; - } catch (Throwable e) { - return false; - } - } -} diff --git a/java/broker/src/test/java/org/apache/qpid/server/SelectorParserTest.java b/java/broker/src/test/java/org/apache/qpid/server/SelectorParserTest.java index 3ba4f1c620..3e0e217eee 100644 --- a/java/broker/src/test/java/org/apache/qpid/server/SelectorParserTest.java +++ b/java/broker/src/test/java/org/apache/qpid/server/SelectorParserTest.java @@ -23,7 +23,8 @@ package org.apache.qpid.server; import junit.framework.TestCase; -import org.apache.qpid.AMQException; +import org.apache.qpid.filter.SelectorParsingException; +import org.apache.qpid.filter.selector.ParseException; import org.apache.qpid.server.filter.JMSSelectorFilter; public class SelectorParserTest extends TestCase @@ -109,7 +110,11 @@ public class SelectorParserTest extends TestCase { new JMSSelectorFilter(selector); } - catch (AMQException e) + catch (ParseException e) + { + fail("Selector '" + selector + "' was not parsed :" + e.getMessage()); + } + catch (SelectorParsingException e) { fail("Selector '" + selector + "' was not parsed :" + e.getMessage()); } @@ -122,7 +127,11 @@ public class SelectorParserTest extends TestCase new JMSSelectorFilter(selector); fail("Selector '" + selector + "' was parsed "); } - catch (AMQException e) + catch (ParseException e) + { + //normal path + } + catch (SelectorParsingException e) { //normal path } diff --git a/java/client/build.xml b/java/client/build.xml index d52de8dca6..5a72d67b68 100644 --- a/java/client/build.xml +++ b/java/client/build.xml @@ -32,14 +32,6 @@ - - - - - - diff --git a/java/client/src/main/grammar/SelectorParser.jj b/java/client/src/main/grammar/SelectorParser.jj deleted file mode 100644 index 4bf9a968d7..0000000000 --- a/java/client/src/main/grammar/SelectorParser.jj +++ /dev/null @@ -1,609 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - - // - // Original File from r450141 of the Apache ActiveMQ project - // - -// ---------------------------------------------------------------------------- -// OPTIONS -// ---------------------------------------------------------------------------- -options { - STATIC = false; - UNICODE_INPUT = true; - - // some performance optimizations - OPTIMIZE_TOKEN_MANAGER = true; - ERROR_REPORTING = false; -} - -// ---------------------------------------------------------------------------- -// PARSER -// ---------------------------------------------------------------------------- - -PARSER_BEGIN(SelectorParser) -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.filter.selector; - -import java.io.StringReader; -import java.util.ArrayList; - -import org.apache.qpid.AMQInternalException; -import org.apache.qpid.filter.ArithmeticExpression; -import org.apache.qpid.filter.BooleanExpression; -import org.apache.qpid.filter.ComparisonExpression; -import org.apache.qpid.filter.ConstantExpression; -import org.apache.qpid.filter.Expression; -import org.apache.qpid.filter.LogicExpression; -import org.apache.qpid.filter.PropertyExpression; -import org.apache.qpid.filter.UnaryExpression; - -/** - * JMS Selector Parser generated by JavaCC - * - * Do not edit this .java file directly - it is autogenerated from SelectorParser.jj - */ -public class SelectorParser { - - public SelectorParser() { - this(new StringReader("")); - } - - public BooleanExpression parse(String sql) throws AMQInternalException { - this.ReInit(new StringReader(sql)); - - try { - return this.JmsSelector(); - } - catch (Throwable e) { - throw new AMQInternalException(sql,e); - } - - } - - private BooleanExpression asBooleanExpression(Expression value) throws ParseException { - if (value instanceof BooleanExpression) { - return (BooleanExpression) value; - } - if (value instanceof PropertyExpression) { - return UnaryExpression.createBooleanCast( value ); - } - throw new ParseException("Expression will not result in a boolean value: " + value); - } - - -} - -PARSER_END(SelectorParser) - -// ---------------------------------------------------------------------------- -// Tokens -// ---------------------------------------------------------------------------- - -/* White Space */ -SPECIAL_TOKEN : -{ - " " | "\t" | "\n" | "\r" | "\f" -} - -/* Comments */ -SKIP: -{ - -} - -SKIP: -{ - -} - -/* Reserved Words */ -TOKEN [IGNORE_CASE] : -{ - < NOT : "NOT"> - | < AND : "AND"> - | < OR : "OR"> - | < BETWEEN : "BETWEEN"> - | < LIKE : "LIKE"> - | < ESCAPE : "ESCAPE"> - | < IN : "IN"> - | < IS : "IS"> - | < TRUE : "TRUE" > - | < FALSE : "FALSE" > - | < NULL : "NULL" > -} - -/* Literals */ -TOKEN [IGNORE_CASE] : -{ - - < DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* (["l","L"])? > - | < HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ > - | < OCTAL_LITERAL: "0" (["0"-"7"])* > - | < FLOATING_POINT_LITERAL: - (["0"-"9"])+ "." (["0"-"9"])* ()? // matches: 5.5 or 5. or 5.5E10 or 5.E10 - | "." (["0"-"9"])+ ()? // matches: .5 or .5E10 - | (["0"-"9"])+ // matches: 5E10 - > - | < #EXPONENT: "E" (["+","-"])? (["0"-"9"])+ > - | < STRING_LITERAL: "'" ( ("''") | ~["'"] )* "'" > -} - -TOKEN [IGNORE_CASE] : -{ - < ID : ["a"-"z", "_", "$"] (["a"-"z","0"-"9","_", "$"])* > - | < QUOTED_ID : "\"" ( ("\"\"") | ~["\""] )* "\"" > - -} - -// ---------------------------------------------------------------------------- -// Grammer -// ---------------------------------------------------------------------------- -BooleanExpression JmsSelector() : -{ - Expression left=null; -} -{ - ( - left = orExpression() - ) - { - return asBooleanExpression(left); - } - -} - -Expression orExpression() : -{ - Expression left; - Expression right; -} -{ - ( - left = andExpression() - ( - right = andExpression() - { - left = LogicExpression.createOR(asBooleanExpression(left), asBooleanExpression(right)); - } - )* - ) - { - return left; - } - -} - - -Expression andExpression() : -{ - Expression left; - Expression right; -} -{ - ( - left = equalityExpression() - ( - right = equalityExpression() - { - left = LogicExpression.createAND(asBooleanExpression(left), asBooleanExpression(right)); - } - )* - ) - { - return left; - } -} - -Expression equalityExpression() : -{ - Expression left; - Expression right; -} -{ - ( - left = comparisonExpression() - ( - - "=" right = comparisonExpression() - { - left = ComparisonExpression.createEqual(left, right); - } - | - "<>" right = comparisonExpression() - { - left = ComparisonExpression.createNotEqual(left, right); - } - | - LOOKAHEAD(2) - - { - left = ComparisonExpression.createIsNull(left); - } - | - - { - left = ComparisonExpression.createIsNotNull(left); - } - )* - ) - { - return left; - } -} - -Expression comparisonExpression() : -{ - Expression left; - Expression right; - Expression low; - Expression high; - String t, u; - boolean not; - ArrayList list; -} -{ - ( - left = addExpression() - ( - - ">" right = addExpression() - { - left = ComparisonExpression.createGreaterThan(left, right); - } - | - ">=" right = addExpression() - { - left = ComparisonExpression.createGreaterThanEqual(left, right); - } - | - "<" right = addExpression() - { - left = ComparisonExpression.createLessThan(left, right); - } - | - "<=" right = addExpression() - { - left = ComparisonExpression.createLessThanEqual(left, right); - } - | - { - u=null; - } - t = stringLitteral() - [ u = stringLitteral() ] - { - left = ComparisonExpression.createLike(left, t, u); - } - | - LOOKAHEAD(2) - { - u=null; - } - t = stringLitteral() [ u = stringLitteral() ] - { - left = ComparisonExpression.createNotLike(left, t, u); - } - | - low = addExpression() high = addExpression() - { - left = ComparisonExpression.createBetween(left, low, high); - } - | - LOOKAHEAD(2) - low = addExpression() high = addExpression() - { - left = ComparisonExpression.createNotBetween(left, low, high); - } - | - - "(" - t = stringLitteral() - { - list = new ArrayList(); - list.add( t ); - } - ( - "," - t = stringLitteral() - { - list.add( t ); - } - - )* - ")" - { - left = ComparisonExpression.createInFilter(left, list); - } - | - LOOKAHEAD(2) - - "(" - t = stringLitteral() - { - list = new ArrayList(); - list.add( t ); - } - ( - "," - t = stringLitteral() - { - list.add( t ); - } - - )* - ")" - { - left = ComparisonExpression.createNotInFilter(left, list); - } - - )* - ) - { - return left; - } -} - -Expression addExpression() : -{ - Expression left; - Expression right; -} -{ - left = multExpr() - ( - LOOKAHEAD( ("+"|"-") multExpr()) - ( - "+" right = multExpr() - { - left = ArithmeticExpression.createPlus(left, right); - } - | - "-" right = multExpr() - { - left = ArithmeticExpression.createMinus(left, right); - } - ) - - )* - { - return left; - } -} - -Expression multExpr() : -{ - Expression left; - Expression right; -} -{ - left = unaryExpr() - ( - "*" right = unaryExpr() - { - left = ArithmeticExpression.createMultiply(left, right); - } - | - "/" right = unaryExpr() - { - left = ArithmeticExpression.createDivide(left, right); - } - | - "%" right = unaryExpr() - { - left = ArithmeticExpression.createMod(left, right); - } - - )* - { - return left; - } -} - - -Expression unaryExpr() : -{ - String s=null; - Expression left=null; -} -{ - ( - LOOKAHEAD( "+" unaryExpr() ) - "+" left=unaryExpr() - | - "-" left=unaryExpr() - { - left = UnaryExpression.createNegate(left); - } - | - left=unaryExpr() - { - left = UnaryExpression.createNOT( asBooleanExpression(left) ); - } - | - left = primaryExpr() - ) - { - return left; - } - -} - -Expression primaryExpr() : -{ - Expression left=null; -} -{ - ( - left = literal() - | - left = variable() - | - "(" left = orExpression() ")" - ) - { - return left; - } -} - - - -ConstantExpression literal() : -{ - Token t; - String s; - ConstantExpression left=null; -} -{ - ( - ( - s = stringLitteral() - { - left = new ConstantExpression(s); - } - ) - | - ( - t = - { - left = ConstantExpression.createFromDecimal(t.image); - } - ) - | - ( - t = - { - left = ConstantExpression.createFromHex(t.image); - } - ) - | - ( - t = - { - left = ConstantExpression.createFromOctal(t.image); - } - ) - | - ( - t = - { - left = ConstantExpression.createFloat(t.image); - } - ) - | - ( - - { - left = ConstantExpression.TRUE; - } - ) - | - ( - - { - left = ConstantExpression.FALSE; - } - ) - | - ( - - { - left = ConstantExpression.NULL; - } - ) - ) - { - return left; - } -} - -String stringLitteral() : -{ - Token t; - StringBuffer rc = new StringBuffer(); - boolean first=true; -} -{ - t = - { - // Decode the sting value. - String image = t.image; - for( int i=1; i < image.length()-1; i++ ) { - char c = image.charAt(i); - if( c == '\'' ) - i++; - rc.append(c); - } - return rc.toString(); - } -} - -PropertyExpression variable() : -{ - Token t; - StringBuffer rc = new StringBuffer(); - PropertyExpression left=null; -} -{ - ( - t = - { - left = new PropertyExpression(t.image); - } - | - t = - { - // Decode the sting value. - String image = t.image; - for( int i=1; i < image.length()-1; i++ ) { - char c = image.charAt(i); - if( c == '"' ) - i++; - rc.append(c); - } - return new PropertyExpression(rc.toString()); - } - - ) - { - return left; - } -} diff --git a/java/client/src/main/java/org/apache/qpid/client/AMQSession.java b/java/client/src/main/java/org/apache/qpid/client/AMQSession.java index 55707be14d..eef5bc0b57 100644 --- a/java/client/src/main/java/org/apache/qpid/client/AMQSession.java +++ b/java/client/src/main/java/org/apache/qpid/client/AMQSession.java @@ -47,8 +47,8 @@ import org.apache.qpid.client.message.UnprocessedMessage; import org.apache.qpid.client.protocol.AMQProtocolHandler; import org.apache.qpid.client.util.FlowControllingBlockingQueue; import org.apache.qpid.common.AMQPFilterTypes; +import org.apache.qpid.client.filter.MessageFilter; import org.apache.qpid.configuration.ClientProperties; -import org.apache.qpid.filter.MessageFilter; import org.apache.qpid.framing.AMQShortString; import org.apache.qpid.framing.FieldTable; import org.apache.qpid.framing.MethodRegistry; diff --git a/java/client/src/main/java/org/apache/qpid/client/AMQSession_0_10.java b/java/client/src/main/java/org/apache/qpid/client/AMQSession_0_10.java index c092fa6ccb..e1138c5356 100644 --- a/java/client/src/main/java/org/apache/qpid/client/AMQSession_0_10.java +++ b/java/client/src/main/java/org/apache/qpid/client/AMQSession_0_10.java @@ -27,6 +27,7 @@ import org.apache.qpid.client.AMQDestination.DestSyntax; import org.apache.qpid.client.failover.FailoverException; import org.apache.qpid.client.failover.FailoverNoopSupport; import org.apache.qpid.client.failover.FailoverProtectedOperation; +import org.apache.qpid.client.filter.MessageFilter; import org.apache.qpid.client.message.AMQMessageDelegateFactory; import org.apache.qpid.client.message.FieldTableSupport; import org.apache.qpid.client.message.MessageFactoryRegistry; @@ -37,7 +38,6 @@ import org.apache.qpid.client.messaging.address.Node.ExchangeNode; import org.apache.qpid.client.messaging.address.Node.QueueNode; import org.apache.qpid.client.protocol.AMQProtocolHandler; import org.apache.qpid.exchange.ExchangeDefaults; -import org.apache.qpid.filter.MessageFilter; import org.apache.qpid.framing.AMQShortString; import org.apache.qpid.framing.FieldTable; import org.apache.qpid.protocol.AMQConstant; diff --git a/java/client/src/main/java/org/apache/qpid/client/AMQSession_0_8.java b/java/client/src/main/java/org/apache/qpid/client/AMQSession_0_8.java index 24d73c583e..c86fb6529a 100644 --- a/java/client/src/main/java/org/apache/qpid/client/AMQSession_0_8.java +++ b/java/client/src/main/java/org/apache/qpid/client/AMQSession_0_8.java @@ -38,7 +38,7 @@ import org.apache.qpid.client.protocol.AMQProtocolHandler; import org.apache.qpid.client.state.AMQState; import org.apache.qpid.client.state.AMQStateManager; import org.apache.qpid.client.state.listener.SpecificMethodFrameListener; -import org.apache.qpid.filter.MessageFilter; +import org.apache.qpid.client.filter.MessageFilter; import org.apache.qpid.framing.*; import org.apache.qpid.framing.amqp_0_9.MethodRegistry_0_9; import org.apache.qpid.framing.amqp_0_91.MethodRegistry_0_91; diff --git a/java/client/src/main/java/org/apache/qpid/client/BasicMessageConsumer.java b/java/client/src/main/java/org/apache/qpid/client/BasicMessageConsumer.java index 1def0c4237..8e9b1fb90f 100644 --- a/java/client/src/main/java/org/apache/qpid/client/BasicMessageConsumer.java +++ b/java/client/src/main/java/org/apache/qpid/client/BasicMessageConsumer.java @@ -26,14 +26,14 @@ import org.slf4j.LoggerFactory; import org.apache.qpid.AMQException; import org.apache.qpid.AMQInternalException; import org.apache.qpid.client.failover.FailoverException; +import org.apache.qpid.client.filter.MessageFilter; import org.apache.qpid.client.message.AMQMessageDelegateFactory; import org.apache.qpid.client.message.AbstractJMSMessage; import org.apache.qpid.client.message.CloseConsumerMessage; import org.apache.qpid.client.message.MessageFactoryRegistry; import org.apache.qpid.client.protocol.AMQProtocolHandler; import org.apache.qpid.common.AMQPFilterTypes; -import org.apache.qpid.filter.JMSSelectorFilter; -import org.apache.qpid.filter.MessageFilter; +import org.apache.qpid.client.filter.JMSSelectorFilter; import org.apache.qpid.framing.AMQShortString; import org.apache.qpid.framing.FieldTable; import org.apache.qpid.framing.FieldTableFactory; diff --git a/java/client/src/main/java/org/apache/qpid/client/filter/JMSSelectorFilter.java b/java/client/src/main/java/org/apache/qpid/client/filter/JMSSelectorFilter.java new file mode 100644 index 0000000000..bab518b0ec --- /dev/null +++ b/java/client/src/main/java/org/apache/qpid/client/filter/JMSSelectorFilter.java @@ -0,0 +1,213 @@ +/* Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.qpid.client.filter; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.qpid.AMQInternalException; +import org.apache.qpid.client.message.AbstractJMSMessage; +import org.apache.qpid.filter.FilterableMessage; +import org.apache.qpid.filter.SelectorParsingException; +import org.apache.qpid.filter.selector.ParseException; +import org.apache.qpid.filter.selector.SelectorParser; +import org.apache.qpid.filter.BooleanExpression; +import org.apache.qpid.filter.selector.TokenMgrError; + +import javax.jms.DeliveryMode; +import javax.jms.JMSException; + + +public class JMSSelectorFilter implements MessageFilter +{ + private static final Logger _logger = LoggerFactory.getLogger(JMSSelectorFilter.class); + + private final String _selector; + private final BooleanExpression _matcher; + + public JMSSelectorFilter(String selector) throws AMQInternalException + { + if (selector == null || "".equals(selector)) + { + throw new IllegalArgumentException("Cannot create a JMSSelectorFilter with a null or empty selector string"); + } + _selector = selector; + if (_logger.isDebugEnabled()) + { + _logger.debug("Created JMSSelectorFilter with selector:" + _selector); + } + try + { + _matcher = new SelectorParser().parse(selector); + } + catch (ParseException e) + { + throw new AMQInternalException("Unable to parse selector \""+selector+"\"", e); + } + catch (SelectorParsingException e) + { + throw new AMQInternalException("Unable to parse selector \""+selector+"\"", e); + } + catch (TokenMgrError e) + { + throw new AMQInternalException("Unable to parse selector \""+selector+"\"", e); + } + } + + public boolean matches(AbstractJMSMessage message) + { + try + { + boolean match = _matcher.matches(wrap(message)); + if (_logger.isDebugEnabled()) + { + _logger.debug(message + " match(" + match + ") selector(" + _selector + "): " + _selector); + } + return match; + } + catch (SelectorParsingException e) + { + _logger.warn("Caught exception when evaluating message selector for message " + message, e); + } + return false; + } + + private FilterableMessage wrap(final AbstractJMSMessage message) + { + return new FilterableMessage() + { + public boolean isPersistent() + { + try + { + return message.getJMSDeliveryMode() == DeliveryMode.PERSISTENT; + } + catch (JMSException e) + { + throw new SelectorParsingException(e); + } + } + + public boolean isRedelivered() + { + try + { + return message.getJMSRedelivered(); + } + catch (JMSException e) + { + throw new SelectorParsingException(e); + } + } + + public Object getHeader(String name) + { + try + { + return message.getObjectProperty(name); + } + catch (JMSException e) + { + throw new SelectorParsingException(e); + } + } + + public String getReplyTo() + { + return message.getReplyToString(); + } + + public String getType() + { + try + { + return message.getJMSType(); + } + catch (JMSException e) + { + throw new SelectorParsingException(e); + } + } + + public byte getPriority() + { + try + { + return (byte) message.getJMSPriority(); + } + catch (JMSException e) + { + throw new SelectorParsingException(e); + } + } + + public String getMessageId() + { + try + { + return message.getJMSMessageID(); + } + catch (JMSException e) + { + throw new SelectorParsingException(e); + } + } + + public long getTimestamp() + { + try + { + return message.getJMSTimestamp(); + } + catch (JMSException e) + { + throw new SelectorParsingException(e); + } + } + + public String getCorrelationId() + { + try + { + return message.getJMSCorrelationID(); + } + catch (JMSException e) + { + throw new SelectorParsingException(e); + } + } + + public long getExpiration() + { + try + { + return message.getJMSExpiration(); + } + catch (JMSException e) + { + throw new SelectorParsingException(e); + } + } + }; + } + + public String getSelector() + { + return _selector; + } +} diff --git a/java/client/src/main/java/org/apache/qpid/client/filter/MessageFilter.java b/java/client/src/main/java/org/apache/qpid/client/filter/MessageFilter.java new file mode 100644 index 0000000000..fec78d6ba5 --- /dev/null +++ b/java/client/src/main/java/org/apache/qpid/client/filter/MessageFilter.java @@ -0,0 +1,27 @@ +/* Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.qpid.client.filter; + +import org.apache.qpid.client.message.AbstractJMSMessage; + + +public interface MessageFilter +{ + boolean matches(AbstractJMSMessage message); + String getSelector(); +} diff --git a/java/client/src/main/java/org/apache/qpid/filter/ArithmeticExpression.java b/java/client/src/main/java/org/apache/qpid/filter/ArithmeticExpression.java deleted file mode 100644 index df5e2acd66..0000000000 --- a/java/client/src/main/java/org/apache/qpid/filter/ArithmeticExpression.java +++ /dev/null @@ -1,268 +0,0 @@ -/* Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.qpid.filter; - -import org.apache.qpid.AMQInternalException; -import org.apache.qpid.client.message.AbstractJMSMessage; - - -/** - * An expression which performs an operation on two expression values - */ -public abstract class ArithmeticExpression extends BinaryExpression -{ - - protected static final int INTEGER = 1; - protected static final int LONG = 2; - protected static final int DOUBLE = 3; - - - public ArithmeticExpression(Expression left, Expression right) - { - super(left, right); - } - - public static Expression createPlus(Expression left, Expression right) - { - return new ArithmeticExpression(left, right) - { - protected Object evaluate(Object lvalue, Object rvalue) - { - if (lvalue instanceof String) - { - String text = (String) lvalue; - return text + rvalue; - } - else if (lvalue instanceof Number) - { - return plus((Number) lvalue, asNumber(rvalue)); - } - - throw new RuntimeException("Cannot call plus operation on: " + lvalue + " and: " + rvalue); - } - - public String getExpressionSymbol() - { - return "+"; - } - }; - } - - public static Expression createMinus(Expression left, Expression right) - { - return new ArithmeticExpression(left, right) - { - protected Object evaluate(Object lvalue, Object rvalue) - { - if (lvalue instanceof Number) - { - return minus((Number) lvalue, asNumber(rvalue)); - } - - throw new RuntimeException("Cannot call minus operation on: " + lvalue + " and: " + rvalue); - } - - public String getExpressionSymbol() - { - return "-"; - } - }; - } - - public static Expression createMultiply(Expression left, Expression right) - { - return new ArithmeticExpression(left, right) - { - - protected Object evaluate(Object lvalue, Object rvalue) - { - if (lvalue instanceof Number) - { - return multiply((Number) lvalue, asNumber(rvalue)); - } - - throw new RuntimeException("Cannot call multiply operation on: " + lvalue + " and: " + rvalue); - } - - public String getExpressionSymbol() - { - return "*"; - } - }; - } - - public static Expression createDivide(Expression left, Expression right) - { - return new ArithmeticExpression(left, right) - { - - protected Object evaluate(Object lvalue, Object rvalue) - { - if (lvalue instanceof Number) - { - return divide((Number) lvalue, asNumber(rvalue)); - } - - throw new RuntimeException("Cannot call divide operation on: " + lvalue + " and: " + rvalue); - } - - public String getExpressionSymbol() - { - return "/"; - } - }; - } - - public static Expression createMod(Expression left, Expression right) - { - return new ArithmeticExpression(left, right) - { - - protected Object evaluate(Object lvalue, Object rvalue) - { - if (lvalue instanceof Number) - { - return mod((Number) lvalue, asNumber(rvalue)); - } - - throw new RuntimeException("Cannot call mod operation on: " + lvalue + " and: " + rvalue); - } - - public String getExpressionSymbol() - { - return "%"; - } - }; - } - - protected Number plus(Number left, Number right) - { - switch (numberType(left, right)) - { - - case ArithmeticExpression.INTEGER: - return new Integer(left.intValue() + right.intValue()); - - case ArithmeticExpression.LONG: - return new Long(left.longValue() + right.longValue()); - - default: - return new Double(left.doubleValue() + right.doubleValue()); - } - } - - protected Number minus(Number left, Number right) - { - switch (numberType(left, right)) - { - - case ArithmeticExpression.INTEGER: - return new Integer(left.intValue() - right.intValue()); - - case ArithmeticExpression.LONG: - return new Long(left.longValue() - right.longValue()); - - default: - return new Double(left.doubleValue() - right.doubleValue()); - } - } - - protected Number multiply(Number left, Number right) - { - switch (numberType(left, right)) - { - - case ArithmeticExpression.INTEGER: - return new Integer(left.intValue() * right.intValue()); - - case ArithmeticExpression.LONG: - return new Long(left.longValue() * right.longValue()); - - default: - return new Double(left.doubleValue() * right.doubleValue()); - } - } - - protected Number divide(Number left, Number right) - { - return new Double(left.doubleValue() / right.doubleValue()); - } - - protected Number mod(Number left, Number right) - { - return new Double(left.doubleValue() % right.doubleValue()); - } - - private int numberType(Number left, Number right) - { - if (isDouble(left) || isDouble(right)) - { - return ArithmeticExpression.DOUBLE; - } - else if ((left instanceof Long) || (right instanceof Long)) - { - return ArithmeticExpression.LONG; - } - else - { - return ArithmeticExpression.INTEGER; - } - } - - private boolean isDouble(Number n) - { - return (n instanceof Float) || (n instanceof Double); - } - - protected Number asNumber(Object value) - { - if (value instanceof Number) - { - return (Number) value; - } - else - { - throw new RuntimeException("Cannot convert value: " + value + " into a number"); - } - } - - public Object evaluate(AbstractJMSMessage message) throws AMQInternalException - { - Object lvalue = getLeft().evaluate(message); - if (lvalue == null) - { - return null; - } - - Object rvalue = getRight().evaluate(message); - if (rvalue == null) - { - return null; - } - - return evaluate(lvalue, rvalue); - } - - /** - * @param lvalue - * @param rvalue - * @return - */ - protected abstract Object evaluate(Object lvalue, Object rvalue); - -} diff --git a/java/client/src/main/java/org/apache/qpid/filter/BinaryExpression.java b/java/client/src/main/java/org/apache/qpid/filter/BinaryExpression.java deleted file mode 100644 index a08a6cc094..0000000000 --- a/java/client/src/main/java/org/apache/qpid/filter/BinaryExpression.java +++ /dev/null @@ -1,88 +0,0 @@ -/* Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.qpid.filter; - -/** - * An expression which performs an operation on two expression values. - */ -public abstract class BinaryExpression implements Expression -{ - private final Expression left; - private final Expression right; - - public BinaryExpression(Expression left, Expression right) - { - this.left = left; - this.right = right; - } - - public Expression getLeft() - { - return left; - } - - public Expression getRight() - { - return right; - } - - /** - * @see Object#toString() - */ - public String toString() - { - return "(" + left.toString() + " " + getExpressionSymbol() + " " + right.toString() + ")"; - } - - /** - * TODO: more efficient hashCode() - * - * @see Object#hashCode() - */ - public int hashCode() - { - return toString().hashCode(); - } - - /** - * TODO: more efficient hashCode() - * - * @see Object#equals(Object) - */ - public boolean equals(Object o) - { - - if ((o == null) || !this.getClass().equals(o.getClass())) - { - return false; - } - - return toString().equals(o.toString()); - - } - - /** - * Returns the symbol that represents this binary expression. For example, addition is - * represented by "+" - * - * @return - */ - public abstract String getExpressionSymbol(); - - -} diff --git a/java/client/src/main/java/org/apache/qpid/filter/BooleanExpression.java b/java/client/src/main/java/org/apache/qpid/filter/BooleanExpression.java deleted file mode 100644 index 14a5c7ea87..0000000000 --- a/java/client/src/main/java/org/apache/qpid/filter/BooleanExpression.java +++ /dev/null @@ -1,33 +0,0 @@ -/* Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.qpid.filter; - -import org.apache.qpid.AMQInternalException; -import org.apache.qpid.client.message.AbstractJMSMessage; - - -/** - * A BooleanExpression is an expression that always - * produces a Boolean result. - */ -public interface BooleanExpression extends Expression -{ - - public boolean matches(AbstractJMSMessage message) throws AMQInternalException; - -} diff --git a/java/client/src/main/java/org/apache/qpid/filter/ComparisonExpression.java b/java/client/src/main/java/org/apache/qpid/filter/ComparisonExpression.java deleted file mode 100644 index bf5220f695..0000000000 --- a/java/client/src/main/java/org/apache/qpid/filter/ComparisonExpression.java +++ /dev/null @@ -1,588 +0,0 @@ -/* Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.qpid.filter; - -import org.apache.qpid.AMQInternalException; -import org.apache.qpid.client.message.AbstractJMSMessage; - -import java.util.HashSet; -import java.util.List; -import java.util.regex.Pattern; - -/** - * A filter performing a comparison of two objects - */ -public abstract class ComparisonExpression extends BinaryExpression implements BooleanExpression -{ - - public static BooleanExpression createBetween(Expression value, Expression left, Expression right) - { - return LogicExpression.createAND(ComparisonExpression.createGreaterThanEqual(value, left), ComparisonExpression.createLessThanEqual(value, right)); - } - - public static BooleanExpression createNotBetween(Expression value, Expression left, Expression right) - { - return LogicExpression.createOR(ComparisonExpression.createLessThan(value, left), ComparisonExpression.createGreaterThan(value, right)); - } - - private static final HashSet REGEXP_CONTROL_CHARS = new HashSet(); - - static - { - ComparisonExpression.REGEXP_CONTROL_CHARS.add(new Character('.')); - ComparisonExpression.REGEXP_CONTROL_CHARS.add(new Character('\\')); - ComparisonExpression.REGEXP_CONTROL_CHARS.add(new Character('[')); - ComparisonExpression.REGEXP_CONTROL_CHARS.add(new Character(']')); - ComparisonExpression.REGEXP_CONTROL_CHARS.add(new Character('^')); - ComparisonExpression.REGEXP_CONTROL_CHARS.add(new Character('$')); - ComparisonExpression.REGEXP_CONTROL_CHARS.add(new Character('?')); - ComparisonExpression.REGEXP_CONTROL_CHARS.add(new Character('*')); - ComparisonExpression.REGEXP_CONTROL_CHARS.add(new Character('+')); - ComparisonExpression.REGEXP_CONTROL_CHARS.add(new Character('{')); - ComparisonExpression.REGEXP_CONTROL_CHARS.add(new Character('}')); - ComparisonExpression.REGEXP_CONTROL_CHARS.add(new Character('|')); - ComparisonExpression.REGEXP_CONTROL_CHARS.add(new Character('(')); - ComparisonExpression.REGEXP_CONTROL_CHARS.add(new Character(')')); - ComparisonExpression.REGEXP_CONTROL_CHARS.add(new Character(':')); - ComparisonExpression.REGEXP_CONTROL_CHARS.add(new Character('&')); - ComparisonExpression.REGEXP_CONTROL_CHARS.add(new Character('<')); - ComparisonExpression.REGEXP_CONTROL_CHARS.add(new Character('>')); - ComparisonExpression.REGEXP_CONTROL_CHARS.add(new Character('=')); - ComparisonExpression.REGEXP_CONTROL_CHARS.add(new Character('!')); - } - - static class LikeExpression extends UnaryExpression implements BooleanExpression - { - - private Pattern likePattern; - - /** - * @param right - */ - public LikeExpression(Expression right, String like, int escape) - { - super(right); - - StringBuffer regexp = new StringBuffer(like.length() * 2); - regexp.append("\\A"); // The beginning of the input - for (int i = 0; i < like.length(); i++) - { - char c = like.charAt(i); - if (escape == (0xFFFF & c)) - { - i++; - if (i >= like.length()) - { - // nothing left to escape... - break; - } - - char t = like.charAt(i); - regexp.append("\\x"); - regexp.append(Integer.toHexString(0xFFFF & t)); - } - else if (c == '%') - { - regexp.append(".*?"); // Do a non-greedy match - } - else if (c == '_') - { - regexp.append("."); // match one - } - else if (ComparisonExpression.REGEXP_CONTROL_CHARS.contains(new Character(c))) - { - regexp.append("\\x"); - regexp.append(Integer.toHexString(0xFFFF & c)); - } - else - { - regexp.append(c); - } - } - - regexp.append("\\z"); // The end of the input - - likePattern = Pattern.compile(regexp.toString(), Pattern.DOTALL); - } - - /** - * org.apache.activemq.filter.UnaryExpression#getExpressionSymbol() - */ - public String getExpressionSymbol() - { - return "LIKE"; - } - - /** - * org.apache.activemq.filter.Expression#evaluate(MessageEvaluationContext) - */ - public Object evaluate(AbstractJMSMessage message) throws AMQInternalException - { - - Object rv = this.getRight().evaluate(message); - - if (rv == null) - { - return null; - } - - if (!(rv instanceof String)) - { - return - Boolean.FALSE; - } - - return likePattern.matcher((String) rv).matches() ? Boolean.TRUE : Boolean.FALSE; - } - - public boolean matches(AbstractJMSMessage message) throws AMQInternalException - { - Object object = evaluate(message); - - return (object != null) && (object == Boolean.TRUE); - } - } - - public static BooleanExpression createLike(Expression left, String right, String escape) - { - if ((escape != null) && (escape.length() != 1)) - { - throw new RuntimeException( - "The ESCAPE string litteral is invalid. It can only be one character. Litteral used: " + escape); - } - - int c = -1; - if (escape != null) - { - c = 0xFFFF & escape.charAt(0); - } - - return new LikeExpression(left, right, c); - } - - public static BooleanExpression createNotLike(Expression left, String right, String escape) - { - return UnaryExpression.createNOT(ComparisonExpression.createLike(left, right, escape)); - } - - public static BooleanExpression createInFilter(Expression left, List elements) - { - - if (!(left instanceof PropertyExpression)) - { - throw new RuntimeException("Expected a property for In expression, got: " + left); - } - - return UnaryExpression.createInExpression((PropertyExpression) left, elements, false); - - } - - public static BooleanExpression createNotInFilter(Expression left, List elements) - { - - if (!(left instanceof PropertyExpression)) - { - throw new RuntimeException("Expected a property for In expression, got: " + left); - } - - return UnaryExpression.createInExpression((PropertyExpression) left, elements, true); - - } - - public static BooleanExpression createIsNull(Expression left) - { - return ComparisonExpression.doCreateEqual(left, ConstantExpression.NULL); - } - - public static BooleanExpression createIsNotNull(Expression left) - { - return UnaryExpression.createNOT(ComparisonExpression.doCreateEqual(left, ConstantExpression.NULL)); - } - - public static BooleanExpression createNotEqual(Expression left, Expression right) - { - return UnaryExpression.createNOT(ComparisonExpression.createEqual(left, right)); - } - - public static BooleanExpression createEqual(Expression left, Expression right) - { - ComparisonExpression.checkEqualOperand(left); - ComparisonExpression.checkEqualOperand(right); - ComparisonExpression.checkEqualOperandCompatability(left, right); - - return ComparisonExpression.doCreateEqual(left, right); - } - - private static BooleanExpression doCreateEqual(Expression left, Expression right) - { - return new ComparisonExpression(left, right) - { - - public Object evaluate(AbstractJMSMessage message) throws AMQInternalException - { - Object lv = getLeft().evaluate(message); - Object rv = getRight().evaluate(message); - - // Iff one of the values is null - if ((lv == null) ^ (rv == null)) - { - return Boolean.FALSE; - } - - if ((lv == rv) || lv.equals(rv)) - { - return Boolean.TRUE; - } - - if ((lv instanceof Comparable) && (rv instanceof Comparable)) - { - return compare((Comparable) lv, (Comparable) rv); - } - - return Boolean.FALSE; - } - - protected boolean asBoolean(int answer) - { - return answer == 0; - } - - public String getExpressionSymbol() - { - return "="; - } - }; - } - - public static BooleanExpression createGreaterThan(final Expression left, final Expression right) - { - ComparisonExpression.checkLessThanOperand(left); - ComparisonExpression.checkLessThanOperand(right); - - return new ComparisonExpression(left, right) - { - protected boolean asBoolean(int answer) - { - return answer > 0; - } - - public String getExpressionSymbol() - { - return ">"; - } - }; - } - - public static BooleanExpression createGreaterThanEqual(final Expression left, final Expression right) - { - ComparisonExpression.checkLessThanOperand(left); - ComparisonExpression.checkLessThanOperand(right); - - return new ComparisonExpression(left, right) - { - protected boolean asBoolean(int answer) - { - return answer >= 0; - } - - public String getExpressionSymbol() - { - return ">="; - } - }; - } - - public static BooleanExpression createLessThan(final Expression left, final Expression right) - { - ComparisonExpression.checkLessThanOperand(left); - ComparisonExpression.checkLessThanOperand(right); - - return new ComparisonExpression(left, right) - { - - protected boolean asBoolean(int answer) - { - return answer < 0; - } - - public String getExpressionSymbol() - { - return "<"; - } - - }; - } - - public static BooleanExpression createLessThanEqual(final Expression left, final Expression right) - { - ComparisonExpression.checkLessThanOperand(left); - ComparisonExpression.checkLessThanOperand(right); - - return new ComparisonExpression(left, right) - { - - protected boolean asBoolean(int answer) - { - return answer <= 0; - } - - public String getExpressionSymbol() - { - return "<="; - } - }; - } - - /** - * Only Numeric expressions can be used in >, >=, < or <= expressions.s - * - * @param expr - */ - public static void checkLessThanOperand(Expression expr) - { - if (expr instanceof ConstantExpression) - { - Object value = ((ConstantExpression) expr).getValue(); - if (value instanceof Number) - { - return; - } - - // Else it's boolean or a String.. - throw new RuntimeException("Value '" + expr + "' cannot be compared."); - } - - if (expr instanceof BooleanExpression) - { - throw new RuntimeException("Value '" + expr + "' cannot be compared."); - } - } - - /** - * Validates that the expression can be used in == or <> expression. - * Cannot not be NULL TRUE or FALSE litterals. - * - * @param expr - */ - public static void checkEqualOperand(Expression expr) - { - if (expr instanceof ConstantExpression) - { - Object value = ((ConstantExpression) expr).getValue(); - if (value == null) - { - throw new RuntimeException("'" + expr + "' cannot be compared."); - } - } - } - - /** - * - * @param left - * @param right - */ - private static void checkEqualOperandCompatability(Expression left, Expression right) - { - if ((left instanceof ConstantExpression) && (right instanceof ConstantExpression)) - { - if ((left instanceof BooleanExpression) && !(right instanceof BooleanExpression)) - { - throw new RuntimeException("'" + left + "' cannot be compared with '" + right + "'"); - } - } - } - - /** - * @param left - * @param right - */ - public ComparisonExpression(Expression left, Expression right) - { - super(left, right); - } - - public Object evaluate(AbstractJMSMessage message) throws AMQInternalException - { - Comparable lv = (Comparable) getLeft().evaluate(message); - if (lv == null) - { - return null; - } - - Comparable rv = (Comparable) getRight().evaluate(message); - if (rv == null) - { - return null; - } - - return compare(lv, rv); - } - - protected Boolean compare(Comparable lv, Comparable rv) - { - Class lc = lv.getClass(); - Class rc = rv.getClass(); - // If the the objects are not of the same type, - // try to convert up to allow the comparison. - if (lc != rc) - { - if (lc == Byte.class) - { - if (rc == Short.class) - { - lv = new Short(((Number) lv).shortValue()); - } - else if (rc == Integer.class) - { - lv = new Integer(((Number) lv).intValue()); - } - else if (rc == Long.class) - { - lv = new Long(((Number) lv).longValue()); - } - else if (rc == Float.class) - { - lv = new Float(((Number) lv).floatValue()); - } - else if (rc == Double.class) - { - lv = new Double(((Number) lv).doubleValue()); - } - else - { - return Boolean.FALSE; - } - } - else if (lc == Short.class) - { - if (rc == Integer.class) - { - lv = new Integer(((Number) lv).intValue()); - } - else if (rc == Long.class) - { - lv = new Long(((Number) lv).longValue()); - } - else if (rc == Float.class) - { - lv = new Float(((Number) lv).floatValue()); - } - else if (rc == Double.class) - { - lv = new Double(((Number) lv).doubleValue()); - } - else - { - return Boolean.FALSE; - } - } - else if (lc == Integer.class) - { - if (rc == Long.class) - { - lv = new Long(((Number) lv).longValue()); - } - else if (rc == Float.class) - { - lv = new Float(((Number) lv).floatValue()); - } - else if (rc == Double.class) - { - lv = new Double(((Number) lv).doubleValue()); - } - else - { - return Boolean.FALSE; - } - } - else if (lc == Long.class) - { - if (rc == Integer.class) - { - rv = new Long(((Number) rv).longValue()); - } - else if (rc == Float.class) - { - lv = new Float(((Number) lv).floatValue()); - } - else if (rc == Double.class) - { - lv = new Double(((Number) lv).doubleValue()); - } - else - { - return Boolean.FALSE; - } - } - else if (lc == Float.class) - { - if (rc == Integer.class) - { - rv = new Float(((Number) rv).floatValue()); - } - else if (rc == Long.class) - { - rv = new Float(((Number) rv).floatValue()); - } - else if (rc == Double.class) - { - lv = new Double(((Number) lv).doubleValue()); - } - else - { - return Boolean.FALSE; - } - } - else if (lc == Double.class) - { - if (rc == Integer.class) - { - rv = new Double(((Number) rv).doubleValue()); - } - else if (rc == Long.class) - { - rv = new Double(((Number) rv).doubleValue()); - } - else if (rc == Float.class) - { - rv = new Float(((Number) rv).doubleValue()); - } - else - { - return Boolean.FALSE; - } - } - else - { - return Boolean.FALSE; - } - } - - return asBoolean(lv.compareTo(rv)) ? Boolean.TRUE : Boolean.FALSE; - } - - protected abstract boolean asBoolean(int answer); - - public boolean matches(AbstractJMSMessage message) throws AMQInternalException - { - Object object = evaluate(message); - - return (object != null) && (object == Boolean.TRUE); - } - -} diff --git a/java/client/src/main/java/org/apache/qpid/filter/ConstantExpression.java b/java/client/src/main/java/org/apache/qpid/filter/ConstantExpression.java deleted file mode 100644 index d208e228d0..0000000000 --- a/java/client/src/main/java/org/apache/qpid/filter/ConstantExpression.java +++ /dev/null @@ -1,204 +0,0 @@ -/* Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.qpid.filter; - -import org.apache.qpid.AMQInternalException; -import org.apache.qpid.client.message.AbstractJMSMessage; - -import java.math.BigDecimal; - -/** - * Represents a constant expression - */ -public class ConstantExpression implements Expression -{ - - static class BooleanConstantExpression extends ConstantExpression implements BooleanExpression - { - public BooleanConstantExpression(Object value) - { - super(value); - } - - public boolean matches(AbstractJMSMessage message) throws AMQInternalException - { - Object object = evaluate(message); - - return (object != null) && (object == Boolean.TRUE); - } - } - - public static final BooleanConstantExpression NULL = new BooleanConstantExpression(null); - public static final BooleanConstantExpression TRUE = new BooleanConstantExpression(Boolean.TRUE); - public static final BooleanConstantExpression FALSE = new BooleanConstantExpression(Boolean.FALSE); - - private Object value; - - public static ConstantExpression createFromDecimal(String text) - { - - // Strip off the 'l' or 'L' if needed. - if (text.endsWith("l") || text.endsWith("L")) - { - text = text.substring(0, text.length() - 1); - } - - Number value; - try - { - value = new Long(text); - } - catch (NumberFormatException e) - { - // The number may be too big to fit in a long. - value = new BigDecimal(text); - } - - long l = value.longValue(); - if ((Integer.MIN_VALUE <= l) && (l <= Integer.MAX_VALUE)) - { - value = new Integer(value.intValue()); - } - - return new ConstantExpression(value); - } - - public static ConstantExpression createFromHex(String text) - { - Number value = new Long(Long.parseLong(text.substring(2), 16)); - long l = value.longValue(); - if ((Integer.MIN_VALUE <= l) && (l <= Integer.MAX_VALUE)) - { - value = new Integer(value.intValue()); - } - - return new ConstantExpression(value); - } - - public static ConstantExpression createFromOctal(String text) - { - Number value = new Long(Long.parseLong(text, 8)); - long l = value.longValue(); - if ((Integer.MIN_VALUE <= l) && (l <= Integer.MAX_VALUE)) - { - value = new Integer(value.intValue()); - } - - return new ConstantExpression(value); - } - - public static ConstantExpression createFloat(String text) - { - Number value = new Double(text); - - return new ConstantExpression(value); - } - - public ConstantExpression(Object value) - { - this.value = value; - } - - public Object evaluate(AbstractJMSMessage message) throws AMQInternalException - { - return value; - } - - public Object getValue() - { - return value; - } - - /** - * @see Object#toString() - */ - public String toString() - { - if (value == null) - { - return "NULL"; - } - - if (value instanceof Boolean) - { - return ((Boolean) value).booleanValue() ? "TRUE" : "FALSE"; - } - - if (value instanceof String) - { - return ConstantExpression.encodeString((String) value); - } - - return value.toString(); - } - - /** - * TODO: more efficient hashCode() - * - * @see Object#hashCode() - */ - public int hashCode() - { - return toString().hashCode(); - } - - /** - * TODO: more efficient hashCode() - * - * @see Object#equals(Object) - */ - public boolean equals(Object o) - { - - if ((o == null) || !this.getClass().equals(o.getClass())) - { - return false; - } - - return toString().equals(o.toString()); - - } - - /** - * Encodes the value of string so that it looks like it would look like - * when it was provided in a selector. - * - * @param s - * @return - */ - public static String encodeString(String s) - { - StringBuffer b = new StringBuffer(); - b.append('\''); - for (int i = 0; i < s.length(); i++) - { - char c = s.charAt(i); - if (c == '\'') - { - b.append(c); - } - - b.append(c); - } - - b.append('\''); - - return b.toString(); - } - -} diff --git a/java/client/src/main/java/org/apache/qpid/filter/Expression.java b/java/client/src/main/java/org/apache/qpid/filter/Expression.java deleted file mode 100644 index 8208f49688..0000000000 --- a/java/client/src/main/java/org/apache/qpid/filter/Expression.java +++ /dev/null @@ -1,35 +0,0 @@ -/* Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.qpid.filter; - -import org.apache.qpid.AMQInternalException; -import org.apache.qpid.client.message.AbstractJMSMessage; - - -/** - * Represents an expression - */ -public interface Expression -{ - /** - * @param message The message to evaluate - * @return the value of this expression - * @throws AMQInternalException - */ - public Object evaluate(AbstractJMSMessage message) throws AMQInternalException; -} diff --git a/java/client/src/main/java/org/apache/qpid/filter/JMSSelectorFilter.java b/java/client/src/main/java/org/apache/qpid/filter/JMSSelectorFilter.java deleted file mode 100644 index 2a8a3dc558..0000000000 --- a/java/client/src/main/java/org/apache/qpid/filter/JMSSelectorFilter.java +++ /dev/null @@ -1,71 +0,0 @@ -/* Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.qpid.filter; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import org.apache.qpid.AMQInternalException; -import org.apache.qpid.client.message.AbstractJMSMessage; -import org.apache.qpid.filter.selector.SelectorParser; - - -public class JMSSelectorFilter implements MessageFilter -{ - private static final Logger _logger = LoggerFactory.getLogger(JMSSelectorFilter.class); - - private final String _selector; - private final BooleanExpression _matcher; - - public JMSSelectorFilter(String selector) throws AMQInternalException - { - if (selector == null || "".equals(selector)) - { - throw new IllegalArgumentException("Cannot create a JMSSelectorFilter with a null or empty selector string"); - } - _selector = selector; - if (_logger.isDebugEnabled()) - { - _logger.debug("Created JMSSelectorFilter with selector:" + _selector); - } - _matcher = new SelectorParser().parse(selector); - } - - public boolean matches(AbstractJMSMessage message) - { - try - { - boolean match = _matcher.matches(message); - if (_logger.isDebugEnabled()) - { - _logger.debug(message + " match(" + match + ") selector(" + _selector + "): " + _selector); - } - return match; - } - catch (AMQInternalException e) - { - _logger.warn("Caught exception when evaluating message selector for message " + message, e); - } - return false; - } - - public String getSelector() - { - return _selector; - } -} diff --git a/java/client/src/main/java/org/apache/qpid/filter/LogicExpression.java b/java/client/src/main/java/org/apache/qpid/filter/LogicExpression.java deleted file mode 100644 index b08b93228f..0000000000 --- a/java/client/src/main/java/org/apache/qpid/filter/LogicExpression.java +++ /dev/null @@ -1,108 +0,0 @@ -/* Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.qpid.filter; - -import org.apache.qpid.AMQInternalException; -import org.apache.qpid.client.message.AbstractJMSMessage; - - -/** - * A filter performing a comparison of two objects - */ -public abstract class LogicExpression extends BinaryExpression implements BooleanExpression -{ - - public static BooleanExpression createOR(BooleanExpression lvalue, BooleanExpression rvalue) - { - return new LogicExpression(lvalue, rvalue) - { - - public Object evaluate(AbstractJMSMessage message) throws AMQInternalException - { - - Boolean lv = (Boolean) getLeft().evaluate(message); - // Can we do an OR shortcut?? - if ((lv != null) && lv.booleanValue()) - { - return Boolean.TRUE; - } - - Boolean rv = (Boolean) getRight().evaluate(message); - - return (rv == null) ? null : rv; - } - - public String getExpressionSymbol() - { - return "OR"; - } - }; - } - - public static BooleanExpression createAND(BooleanExpression lvalue, BooleanExpression rvalue) - { - return new LogicExpression(lvalue, rvalue) - { - - public Object evaluate(AbstractJMSMessage message) throws AMQInternalException - { - - Boolean lv = (Boolean) getLeft().evaluate(message); - - // Can we do an AND shortcut?? - if (lv == null) - { - return null; - } - - if (!lv.booleanValue()) - { - return Boolean.FALSE; - } - - Boolean rv = (Boolean) getRight().evaluate(message); - - return (rv == null) ? null : rv; - } - - public String getExpressionSymbol() - { - return "AND"; - } - }; - } - - /** - * @param left - * @param right - */ - public LogicExpression(BooleanExpression left, BooleanExpression right) - { - super(left, right); - } - - public abstract Object evaluate(AbstractJMSMessage message) throws AMQInternalException; - - public boolean matches(AbstractJMSMessage message) throws AMQInternalException - { - Object object = evaluate(message); - - return (object != null) && (object == Boolean.TRUE); - } - -} diff --git a/java/client/src/main/java/org/apache/qpid/filter/MessageFilter.java b/java/client/src/main/java/org/apache/qpid/filter/MessageFilter.java deleted file mode 100644 index ec0e8ea4c0..0000000000 --- a/java/client/src/main/java/org/apache/qpid/filter/MessageFilter.java +++ /dev/null @@ -1,27 +0,0 @@ -/* Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.qpid.filter; - -import org.apache.qpid.client.message.AbstractJMSMessage; - - -public interface MessageFilter -{ - boolean matches(AbstractJMSMessage message); - String getSelector(); -} diff --git a/java/client/src/main/java/org/apache/qpid/filter/PropertyExpression.java b/java/client/src/main/java/org/apache/qpid/filter/PropertyExpression.java deleted file mode 100644 index 3f229952b4..0000000000 --- a/java/client/src/main/java/org/apache/qpid/filter/PropertyExpression.java +++ /dev/null @@ -1,300 +0,0 @@ -/* Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.qpid.filter; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import org.apache.qpid.AMQInternalException; -import org.apache.qpid.client.message.AbstractJMSMessage; - -import javax.jms.DeliveryMode; -import javax.jms.JMSException; -import java.util.HashMap; - -/** - * Represents a property expression - */ -public class PropertyExpression implements Expression -{ - // Constants - defined the same as JMS - private static enum JMSDeliveryMode { NON_PERSISTENT, PERSISTENT } - private static final int DEFAULT_PRIORITY = 4; - - private static final Logger _logger = LoggerFactory.getLogger(PropertyExpression.class); - - private static final HashMap JMS_PROPERTY_EXPRESSIONS = new HashMap(); - - static - { - JMS_PROPERTY_EXPRESSIONS.put("JMSDestination", new Expression() - { - public Object evaluate(AbstractJMSMessage message) - { - //TODO - return null; - } - }); - JMS_PROPERTY_EXPRESSIONS.put("JMSReplyTo", new Expression() - { - public Object evaluate(AbstractJMSMessage message) - { - return message.getReplyToString(); - } - }); - - JMS_PROPERTY_EXPRESSIONS.put("JMSType", new Expression() - { - public Object evaluate(AbstractJMSMessage message) - { - try - { - return message.getJMSType(); - } - catch (JMSException e) - { - _logger.warn("Error evaluating property", e); - - return null; - } - - } - }); - - JMS_PROPERTY_EXPRESSIONS.put("JMSDeliveryMode", new Expression() - { - public Object evaluate(AbstractJMSMessage message) - { - - JMSDeliveryMode mode = JMSDeliveryMode.NON_PERSISTENT; - try - { - mode = message.getJMSDeliveryMode() == DeliveryMode.PERSISTENT ? - JMSDeliveryMode.PERSISTENT : JMSDeliveryMode.NON_PERSISTENT; - - if (_logger.isDebugEnabled()) - { - _logger.debug("JMSDeliveryMode is :" + mode); - } - } - catch (JMSException e) - { - _logger.warn("Error evaluating property",e); - } - - return mode.toString(); - } - }); - - JMS_PROPERTY_EXPRESSIONS.put("JMSPriority", new Expression() - { - public Object evaluate(AbstractJMSMessage message) - { - try - { - return message.getJMSPriority(); - } - catch (Exception e) - { - _logger.warn("Error evaluating property",e); - } - - return DEFAULT_PRIORITY; - } - }); - - JMS_PROPERTY_EXPRESSIONS.put("AMQMessageID", new Expression() - { - public Object evaluate(AbstractJMSMessage message) - { - - try - { - return message.getJMSMessageID(); - } - catch (JMSException e) - { - _logger.warn("Error evaluating property",e); - - return null; - } - - } - }); - - JMS_PROPERTY_EXPRESSIONS.put("JMSTimestamp", new Expression() - { - public Object evaluate(AbstractJMSMessage message) - { - try - { - return message.getJMSTimestamp(); - } - catch (Exception e) - { - _logger.warn("Error evaluating property",e); - - return null; - } - - } - }); - - JMS_PROPERTY_EXPRESSIONS.put("JMSCorrelationID", new Expression() - { - public Object evaluate(AbstractJMSMessage message) - { - - try - { - return message.getJMSCorrelationID(); - } - catch (JMSException e) - { - _logger.warn("Error evaluating property",e); - - return null; - } - - } - }); - - JMS_PROPERTY_EXPRESSIONS.put("JMSExpiration", new Expression() - { - public Object evaluate(AbstractJMSMessage message) - { - - try - { - return message.getJMSExpiration(); - } - catch (JMSException e) - { - _logger.warn("Error evaluating property",e); - return null; - } - - } - }); - - JMS_PROPERTY_EXPRESSIONS.put("JMSRedelivered", new Expression() - { - public Object evaluate(AbstractJMSMessage message) - { - try - { - return message.getJMSRedelivered(); - } - catch (JMSException e) - { - _logger.warn("Error evaluating property",e); - return null; - } - } - }); - - JMS_PROPERTY_EXPRESSIONS.put("JMSMessageID", new Expression() - { - public Object evaluate(AbstractJMSMessage message) - { - try - { - return message.getJMSMessageID(); - } - catch (Exception e) - { - _logger.warn("Error evaluating property",e); - - return null; - } - - } - }); - - } - - private final String name; - private final Expression jmsPropertyExpression; - - public PropertyExpression(String name) - { - this.name = name; - jmsPropertyExpression = JMS_PROPERTY_EXPRESSIONS.get(name); - } - - public Object evaluate(AbstractJMSMessage message) throws AMQInternalException - { - - if (jmsPropertyExpression != null) - { - return jmsPropertyExpression.evaluate(message); - } - else - { - - try - { - - if (_logger.isDebugEnabled()) - { - _logger.debug("Looking up property:" + name); - _logger.debug("Properties are:" + message.getPropertyNames()); - } - return message.getObjectProperty(name); - } - catch(JMSException e) - { - throw new AMQInternalException("Exception evaluating properties for filter", e); - } - } - } - - public String getName() - { - return name; - } - - /** - * @see java.lang.Object#toString() - */ - public String toString() - { - return name; - } - - /** - * @see java.lang.Object#hashCode() - */ - public int hashCode() - { - return name.hashCode(); - } - - /** - * @see java.lang.Object#equals(java.lang.Object) - */ - public boolean equals(Object o) - { - if ((o == null) || !this.getClass().equals(o.getClass())) - { - return false; - } - return name.equals(((PropertyExpression) o).name); - } - -} diff --git a/java/client/src/main/java/org/apache/qpid/filter/UnaryExpression.java b/java/client/src/main/java/org/apache/qpid/filter/UnaryExpression.java deleted file mode 100644 index 55fb044839..0000000000 --- a/java/client/src/main/java/org/apache/qpid/filter/UnaryExpression.java +++ /dev/null @@ -1,316 +0,0 @@ -/* Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.qpid.filter; - -import org.apache.qpid.AMQInternalException; -import org.apache.qpid.client.message.AbstractJMSMessage; - -import java.math.BigDecimal; -import java.util.Collection; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; - -/** - * An expression which performs an operation on two expression values - */ -public abstract class UnaryExpression implements Expression -{ - - private static final BigDecimal BD_LONG_MIN_VALUE = BigDecimal.valueOf(Long.MIN_VALUE); - private final Expression right; - - public static Expression createNegate(Expression left) - { - return new UnaryExpression(left) - { - public Object evaluate(AbstractJMSMessage message) throws AMQInternalException - { - Object rvalue = getRight().evaluate(message); - if (rvalue == null) - { - return null; - } - - if (rvalue instanceof Number) - { - return UnaryExpression.negate((Number) rvalue); - } - - return null; - } - - public String getExpressionSymbol() - { - return "-"; - } - }; - } - - public static BooleanExpression createInExpression(PropertyExpression right, List elements, final boolean not) - { - - // Use a HashSet if there are many elements. - Collection t; - if (elements.size() == 0) - { - t = null; - } - else if (elements.size() < 5) - { - t = elements; - } - else - { - t = new HashSet(elements); - } - - final Collection inList = t; - - return new BooleanUnaryExpression(right) - { - public Object evaluate(AbstractJMSMessage message) throws AMQInternalException - { - - Object rvalue = getRight().evaluate(message); - if (rvalue == null) - { - return null; - } - - if (rvalue.getClass() != String.class) - { - return null; - } - - if (((inList != null) && inList.contains(rvalue)) ^ not) - { - return Boolean.TRUE; - } - else - { - return Boolean.FALSE; - } - - } - - public String toString() - { - StringBuffer answer = new StringBuffer(); - answer.append(getRight()); - answer.append(" "); - answer.append(getExpressionSymbol()); - answer.append(" ( "); - - int count = 0; - for (Iterator i = inList.iterator(); i.hasNext();) - { - Object o = (Object) i.next(); - if (count != 0) - { - answer.append(", "); - } - - answer.append(o); - count++; - } - - answer.append(" )"); - - return answer.toString(); - } - - public String getExpressionSymbol() - { - if (not) - { - return "NOT IN"; - } - else - { - return "IN"; - } - } - }; - } - - abstract static class BooleanUnaryExpression extends UnaryExpression implements BooleanExpression - { - public BooleanUnaryExpression(Expression left) - { - super(left); - } - - public boolean matches(AbstractJMSMessage message) throws AMQInternalException - { - Object object = evaluate(message); - - return (object != null) && (object == Boolean.TRUE); - } - } - - ; - - public static BooleanExpression createNOT(BooleanExpression left) - { - return new BooleanUnaryExpression(left) - { - public Object evaluate(AbstractJMSMessage message) throws AMQInternalException - { - Boolean lvalue = (Boolean) getRight().evaluate(message); - if (lvalue == null) - { - return null; - } - - return lvalue.booleanValue() ? Boolean.FALSE : Boolean.TRUE; - } - - public String getExpressionSymbol() - { - return "NOT"; - } - }; - } - public static BooleanExpression createBooleanCast(Expression left) - { - return new BooleanUnaryExpression(left) - { - public Object evaluate(AbstractJMSMessage message) throws AMQInternalException - { - Object rvalue = getRight().evaluate(message); - if (rvalue == null) - { - return null; - } - - if (!rvalue.getClass().equals(Boolean.class)) - { - return Boolean.FALSE; - } - - return ((Boolean) rvalue).booleanValue() ? Boolean.TRUE : Boolean.FALSE; - } - - public String toString() - { - return getRight().toString(); - } - - public String getExpressionSymbol() - { - return ""; - } - }; - } - - private static Number negate(Number left) - { - Class clazz = left.getClass(); - if (clazz == Integer.class) - { - return new Integer(-left.intValue()); - } - else if (clazz == Long.class) - { - return new Long(-left.longValue()); - } - else if (clazz == Float.class) - { - return new Float(-left.floatValue()); - } - else if (clazz == Double.class) - { - return new Double(-left.doubleValue()); - } - else if (clazz == BigDecimal.class) - { - // We ussually get a big deciamal when we have Long.MIN_VALUE constant in the - // Selector. Long.MIN_VALUE is too big to store in a Long as a positive so we store it - // as a Big decimal. But it gets Negated right away.. to here we try to covert it back - // to a Long. - BigDecimal bd = (BigDecimal) left; - bd = bd.negate(); - - if (UnaryExpression.BD_LONG_MIN_VALUE.compareTo(bd) == 0) - { - return new Long(Long.MIN_VALUE); - } - - return bd; - } - else - { - throw new RuntimeException("Don't know how to negate: " + left); - } - } - - public UnaryExpression(Expression left) - { - this.right = left; - } - - public Expression getRight() - { - return right; - } - - /** - * @see Object#toString() - */ - public String toString() - { - return "(" + getExpressionSymbol() + " " + getRight().toString() + ")"; - } - - /** - * TODO: more efficient hashCode() - * - * @see Object#hashCode() - */ - public int hashCode() - { - return toString().hashCode(); - } - - /** - * TODO: more efficient hashCode() - * - * @see Object#equals(Object) - */ - public boolean equals(Object o) - { - - if ((o == null) || !this.getClass().equals(o.getClass())) - { - return false; - } - - return toString().equals(o.toString()); - - } - - /** - * Returns the symbol that represents this binary expression. For example, addition is - * represented by "+" - * - * @return - */ - public abstract String getExpressionSymbol(); - -} diff --git a/java/client/src/test/java/org/apache/qpid/filter/JMSSelectorFilterTest.java b/java/client/src/test/java/org/apache/qpid/filter/JMSSelectorFilterTest.java index d4d8ea4350..8782b92ee9 100644 --- a/java/client/src/test/java/org/apache/qpid/filter/JMSSelectorFilterTest.java +++ b/java/client/src/test/java/org/apache/qpid/filter/JMSSelectorFilterTest.java @@ -23,6 +23,8 @@ package org.apache.qpid.filter; import junit.framework.TestCase; import org.apache.qpid.AMQInternalException; +import org.apache.qpid.client.filter.JMSSelectorFilter; +import org.apache.qpid.client.filter.MessageFilter; import org.apache.qpid.client.message.JMSTextMessage; import org.apache.qpid.client.message.TestMessageHelper; diff --git a/java/client/src/test/java/org/apache/qpid/test/unit/message/TestAMQSession.java b/java/client/src/test/java/org/apache/qpid/test/unit/message/TestAMQSession.java index c11f0ee35d..ecd8eb99d1 100644 --- a/java/client/src/test/java/org/apache/qpid/test/unit/message/TestAMQSession.java +++ b/java/client/src/test/java/org/apache/qpid/test/unit/message/TestAMQSession.java @@ -29,7 +29,7 @@ import org.apache.qpid.client.BasicMessageProducer_0_8; import org.apache.qpid.client.failover.FailoverException; import org.apache.qpid.client.message.AMQMessageDelegateFactory; import org.apache.qpid.client.protocol.AMQProtocolHandler; -import org.apache.qpid.filter.MessageFilter; +import org.apache.qpid.client.filter.MessageFilter; import org.apache.qpid.framing.AMQShortString; import org.apache.qpid.framing.FieldTable; diff --git a/java/common/build.xml b/java/common/build.xml index f2f85fc634..fe96f384d5 100644 --- a/java/common/build.xml +++ b/java/common/build.xml @@ -32,6 +32,7 @@ + @@ -96,7 +97,14 @@ public class QpidBuildVersion - + + + + + + diff --git a/java/common/src/main/grammar/SelectorParser.jj b/java/common/src/main/grammar/SelectorParser.jj new file mode 100644 index 0000000000..32d8b61cc8 --- /dev/null +++ b/java/common/src/main/grammar/SelectorParser.jj @@ -0,0 +1,608 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + + // + // Original File from r450141 of the Apache ActiveMQ project + // + +// ---------------------------------------------------------------------------- +// OPTIONS +// ---------------------------------------------------------------------------- +options { + STATIC = false; + UNICODE_INPUT = true; + + // some performance optimizations + ERROR_REPORTING = false; +} + +// ---------------------------------------------------------------------------- +// PARSER +// ---------------------------------------------------------------------------- + +PARSER_BEGIN(SelectorParser) +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.filter.selector; + +import java.io.StringReader; +import java.util.ArrayList; + +import org.apache.qpid.filter.ArithmeticExpression; +import org.apache.qpid.filter.BooleanExpression; +import org.apache.qpid.filter.ComparisonExpression; +import org.apache.qpid.filter.ConstantExpression; +import org.apache.qpid.filter.Expression; +import org.apache.qpid.filter.LogicExpression; +import org.apache.qpid.filter.PropertyExpression; +import org.apache.qpid.filter.UnaryExpression; + +/** + * JMS Selector Parser generated by JavaCC + * + * Do not edit this .java file directly - it is autogenerated from SelectorParser.jj + */ +public class SelectorParser +{ + + public SelectorParser() + { + this(new StringReader("")); + } + + public BooleanExpression parse(String sql) throws ParseException + { + this.ReInit(new StringReader(sql)); + + return this.JmsSelector(); + + } + + private BooleanExpression asBooleanExpression(Expression value) throws ParseException + { + if (value instanceof BooleanExpression) + { + return (BooleanExpression) value; + } + if (value instanceof PropertyExpression) + { + return UnaryExpression.createBooleanCast( value ); + } + throw new ParseException("Expression will not result in a boolean value: " + value); + } + + +} + +PARSER_END(SelectorParser) + +// ---------------------------------------------------------------------------- +// Tokens +// ---------------------------------------------------------------------------- + +/* White Space */ +SPECIAL_TOKEN : +{ + " " | "\t" | "\n" | "\r" | "\f" +} + +/* Comments */ +SKIP: +{ + +} + +SKIP: +{ + +} + +/* Reserved Words */ +TOKEN [IGNORE_CASE] : +{ + < NOT : "NOT"> + | < AND : "AND"> + | < OR : "OR"> + | < BETWEEN : "BETWEEN"> + | < LIKE : "LIKE"> + | < ESCAPE : "ESCAPE"> + | < IN : "IN"> + | < IS : "IS"> + | < TRUE : "TRUE" > + | < FALSE : "FALSE" > + | < NULL : "NULL" > +} + +/* Literals */ +TOKEN [IGNORE_CASE] : +{ + + < DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* (["l","L"])? > + | < HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ > + | < OCTAL_LITERAL: "0" (["0"-"7"])* > + | < FLOATING_POINT_LITERAL: + (["0"-"9"])+ "." (["0"-"9"])* ()? // matches: 5.5 or 5. or 5.5E10 or 5.E10 + | "." (["0"-"9"])+ ()? // matches: .5 or .5E10 + | (["0"-"9"])+ // matches: 5E10 + > + | < #EXPONENT: "E" (["+","-"])? (["0"-"9"])+ > + | < STRING_LITERAL: "'" ( ("''") | ~["'"] )* "'" > +} + +TOKEN [IGNORE_CASE] : +{ + < ID : ["a"-"z", "_", "$"] (["a"-"z","0"-"9","_", "$"])* > + | < QUOTED_ID : "\"" ( ("\"\"") | ~["\""] )* "\"" > +} + +// ---------------------------------------------------------------------------- +// Grammer +// ---------------------------------------------------------------------------- +BooleanExpression JmsSelector() : +{ + Expression left=null; +} +{ + ( + left = orExpression() + ) + { + return asBooleanExpression(left); + } + +} + +Expression orExpression() : +{ + Expression left; + Expression right; +} +{ + ( + left = andExpression() + ( + right = andExpression() + { + left = LogicExpression.createOR(asBooleanExpression(left), asBooleanExpression(right)); + } + )* + ) + { + return left; + } + +} + + +Expression andExpression() : +{ + Expression left; + Expression right; +} +{ + ( + left = equalityExpression() + ( + right = equalityExpression() + { + left = LogicExpression.createAND(asBooleanExpression(left), asBooleanExpression(right)); + } + )* + ) + { + return left; + } +} + +Expression equalityExpression() : +{ + Expression left; + Expression right; +} +{ + ( + left = comparisonExpression() + ( + + "=" right = comparisonExpression() + { + left = ComparisonExpression.createEqual(left, right); + } + | + "<>" right = comparisonExpression() + { + left = ComparisonExpression.createNotEqual(left, right); + } + | + LOOKAHEAD(2) + + { + left = ComparisonExpression.createIsNull(left); + } + | + + { + left = ComparisonExpression.createIsNotNull(left); + } + )* + ) + { + return left; + } +} + +Expression comparisonExpression() : +{ + Expression left; + Expression right; + Expression low; + Expression high; + String t, u; + boolean not; + ArrayList list; +} +{ + ( + left = addExpression() + ( + + ">" right = addExpression() + { + left = ComparisonExpression.createGreaterThan(left, right); + } + | + ">=" right = addExpression() + { + left = ComparisonExpression.createGreaterThanEqual(left, right); + } + | + "<" right = addExpression() + { + left = ComparisonExpression.createLessThan(left, right); + } + | + "<=" right = addExpression() + { + left = ComparisonExpression.createLessThanEqual(left, right); + } + | + { + u=null; + } + t = stringLiteral() + [ u = stringLiteral() ] + { + left = ComparisonExpression.createLike(left, t, u); + } + | + LOOKAHEAD(2) + { + u=null; + } + t = stringLiteral() [ u = stringLiteral() ] + { + left = ComparisonExpression.createNotLike(left, t, u); + } + | + low = addExpression() high = addExpression() + { + left = ComparisonExpression.createBetween(left, low, high); + } + | + LOOKAHEAD(2) + low = addExpression() high = addExpression() + { + left = ComparisonExpression.createNotBetween(left, low, high); + } + | + + "(" + t = stringLiteral() + { + list = new ArrayList(); + list.add( t ); + } + ( + "," + t = stringLiteral() + { + list.add( t ); + } + + )* + ")" + { + left = ComparisonExpression.createInFilter(left, list); + } + | + LOOKAHEAD(2) + + "(" + t = stringLiteral() + { + list = new ArrayList(); + list.add( t ); + } + ( + "," + t = stringLiteral() + { + list.add( t ); + } + + )* + ")" + { + left = ComparisonExpression.createNotInFilter(left, list); + } + + )* + ) + { + return left; + } +} + +Expression addExpression() : +{ + Expression left; + Expression right; +} +{ + left = multExpr() + ( + LOOKAHEAD( ("+"|"-") multExpr()) + ( + "+" right = multExpr() + { + left = ArithmeticExpression.createPlus(left, right); + } + | + "-" right = multExpr() + { + left = ArithmeticExpression.createMinus(left, right); + } + ) + + )* + { + return left; + } +} + +Expression multExpr() : +{ + Expression left; + Expression right; +} +{ + left = unaryExpr() + ( + "*" right = unaryExpr() + { + left = ArithmeticExpression.createMultiply(left, right); + } + | + "/" right = unaryExpr() + { + left = ArithmeticExpression.createDivide(left, right); + } + | + "%" right = unaryExpr() + { + left = ArithmeticExpression.createMod(left, right); + } + + )* + { + return left; + } +} + + +Expression unaryExpr() : +{ + String s=null; + Expression left=null; +} +{ + ( + LOOKAHEAD( "+" unaryExpr() ) + "+" left=unaryExpr() + | + "-" left=unaryExpr() + { + left = UnaryExpression.createNegate(left); + } + | + left=unaryExpr() + { + left = UnaryExpression.createNOT( asBooleanExpression(left) ); + } + | + left = primaryExpr() + ) + { + return left; + } + +} + +Expression primaryExpr() : +{ + Expression left=null; +} +{ + ( + left = literal() + | + left = variable() + | + "(" left = orExpression() ")" + ) + { + return left; + } +} + + + +ConstantExpression literal() : +{ + Token t; + String s; + ConstantExpression left=null; +} +{ + ( + ( + s = stringLiteral() + { + left = new ConstantExpression(s); + } + ) + | + ( + t = + { + left = ConstantExpression.createFromDecimal(t.image); + } + ) + | + ( + t = + { + left = ConstantExpression.createFromHex(t.image); + } + ) + | + ( + t = + { + left = ConstantExpression.createFromOctal(t.image); + } + ) + | + ( + t = + { + left = ConstantExpression.createFloat(t.image); + } + ) + | + ( + + { + left = ConstantExpression.TRUE; + } + ) + | + ( + + { + left = ConstantExpression.FALSE; + } + ) + | + ( + + { + left = ConstantExpression.NULL; + } + ) + ) + { + return left; + } +} + +String stringLiteral() : +{ + Token t; + StringBuffer rc = new StringBuffer(); + boolean first=true; +} +{ + t = + { + // Decode the sting value. + String image = t.image; + for( int i=1; i < image.length()-1; i++ ) { + char c = image.charAt(i); + if( c == '\'' ) + i++; + rc.append(c); + } + return rc.toString(); + } +} + +PropertyExpression variable() : +{ + Token t; + StringBuffer rc = new StringBuffer(); + PropertyExpression left=null; +} +{ + ( + t = + { + left = new PropertyExpression(t.image); + } + | + t = + { + // Decode the sting value. + String image = t.image; + for( int i=1; i < image.length()-1; i++ ) { + char c = image.charAt(i); + if( c == '"' ) + i++; + rc.append(c); + } + return new PropertyExpression(rc.toString()); + } + + + ) + { + return left; + } +} diff --git a/java/common/src/main/java/org/apache/qpid/filter/ArithmeticExpression.java b/java/common/src/main/java/org/apache/qpid/filter/ArithmeticExpression.java new file mode 100644 index 0000000000..47d970cfbd --- /dev/null +++ b/java/common/src/main/java/org/apache/qpid/filter/ArithmeticExpression.java @@ -0,0 +1,272 @@ +/** + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.qpid.filter; +// +// Based on like named file from r450141 of the Apache ActiveMQ project +// + +/** + * An expression which performs an operation on two expression values + */ +public abstract class ArithmeticExpression extends BinaryExpression +{ + + protected static final int INTEGER = 1; + protected static final int LONG = 2; + protected static final int DOUBLE = 3; + + /** + * @param left + * @param right + */ + public ArithmeticExpression(Expression left, Expression right) + { + super(left, right); + } + + public static Expression createPlus(Expression left, Expression right) + { + return new ArithmeticExpression(left, right) + { + protected Object evaluate(Object lvalue, Object rvalue) + { + if (lvalue instanceof String) + { + String text = (String) lvalue; + String answer = text + rvalue; + + return answer; + } + else if (lvalue instanceof Number) + { + return plus((Number) lvalue, asNumber(rvalue)); + } + + throw new SelectorParsingException("Cannot call plus operation on: " + lvalue + " and: " + rvalue); + } + + public String getExpressionSymbol() + { + return "+"; + } + }; + } + + public static Expression createMinus(Expression left, Expression right) + { + return new ArithmeticExpression(left, right) + { + protected Object evaluate(Object lvalue, Object rvalue) + { + if (lvalue instanceof Number) + { + return minus((Number) lvalue, asNumber(rvalue)); + } + + throw new SelectorParsingException("Cannot call minus operation on: " + lvalue + " and: " + rvalue); + } + + public String getExpressionSymbol() + { + return "-"; + } + }; + } + + public static Expression createMultiply(Expression left, Expression right) + { + return new ArithmeticExpression(left, right) + { + + protected Object evaluate(Object lvalue, Object rvalue) + { + if (lvalue instanceof Number) + { + return multiply((Number) lvalue, asNumber(rvalue)); + } + + throw new SelectorParsingException("Cannot call multiply operation on: " + lvalue + " and: " + rvalue); + } + + public String getExpressionSymbol() + { + return "*"; + } + }; + } + + public static Expression createDivide(Expression left, Expression right) + { + return new ArithmeticExpression(left, right) + { + + protected Object evaluate(Object lvalue, Object rvalue) + { + if (lvalue instanceof Number) + { + return divide((Number) lvalue, asNumber(rvalue)); + } + + throw new SelectorParsingException("Cannot call divide operation on: " + lvalue + " and: " + rvalue); + } + + public String getExpressionSymbol() + { + return "/"; + } + }; + } + + public static Expression createMod(Expression left, Expression right) + { + return new ArithmeticExpression(left, right) + { + + protected Object evaluate(Object lvalue, Object rvalue) + { + if (lvalue instanceof Number) + { + return mod((Number) lvalue, asNumber(rvalue)); + } + + throw new SelectorParsingException("Cannot call mod operation on: " + lvalue + " and: " + rvalue); + } + + public String getExpressionSymbol() + { + return "%"; + } + }; + } + + protected Number plus(Number left, Number right) + { + switch (numberType(left, right)) + { + + case INTEGER: + return Integer.valueOf(left.intValue() + right.intValue()); + + case LONG: + return Long.valueOf(left.longValue() + right.longValue()); + + default: + return Double.valueOf(left.doubleValue() + right.doubleValue()); + } + } + + protected Number minus(Number left, Number right) + { + switch (numberType(left, right)) + { + + case INTEGER: + return Integer.valueOf(left.intValue() - right.intValue()); + + case LONG: + return Long.valueOf(left.longValue() - right.longValue()); + + default: + return Double.valueOf(left.doubleValue() - right.doubleValue()); + } + } + + protected Number multiply(Number left, Number right) + { + switch (numberType(left, right)) + { + + case INTEGER: + return Integer.valueOf(left.intValue() * right.intValue()); + + case LONG: + return Long.valueOf(left.longValue() * right.longValue()); + + default: + return Double.valueOf(left.doubleValue() * right.doubleValue()); + } + } + + protected Number divide(Number left, Number right) + { + return Double.valueOf(left.doubleValue() / right.doubleValue()); + } + + protected Number mod(Number left, Number right) + { + return Double.valueOf(left.doubleValue() % right.doubleValue()); + } + + private int numberType(Number left, Number right) + { + if (isDouble(left) || isDouble(right)) + { + return DOUBLE; + } + else if ((left instanceof Long) || (right instanceof Long)) + { + return LONG; + } + else + { + return INTEGER; + } + } + + private boolean isDouble(Number n) + { + return (n instanceof Float) || (n instanceof Double); + } + + protected Number asNumber(Object value) + { + if (value instanceof Number) + { + return (Number) value; + } + else + { + throw new SelectorParsingException("Cannot convert value: " + value + " into a number"); + } + } + + public Object evaluate(FilterableMessage message) + { + Object lvalue = getLeft().evaluate(message); + if (lvalue == null) + { + return null; + } + + Object rvalue = getRight().evaluate(message); + if (rvalue == null) + { + return null; + } + + return evaluate(lvalue, rvalue); + } + + /** + * @param lvalue + * @param rvalue + * @return + */ + protected abstract Object evaluate(Object lvalue, Object rvalue); + +} diff --git a/java/common/src/main/java/org/apache/qpid/filter/BinaryExpression.java b/java/common/src/main/java/org/apache/qpid/filter/BinaryExpression.java new file mode 100644 index 0000000000..6467bbbe1f --- /dev/null +++ b/java/common/src/main/java/org/apache/qpid/filter/BinaryExpression.java @@ -0,0 +1,90 @@ +/** + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.qpid.filter; +// +// Based on like named file from r450141 of the Apache ActiveMQ project +// + +/** + * An expression which performs an operation on two expression values. + */ +public abstract class BinaryExpression implements Expression +{ + private final Expression left; + private final Expression right; + + public BinaryExpression(Expression left, Expression right) + { + this.left = left; + this.right = right; + } + + public Expression getLeft() + { + return left; + } + + public Expression getRight() + { + return right; + } + + /** + * @see java.lang.Object#toString() + */ + public String toString() + { + return "(" + left.toString() + " " + getExpressionSymbol() + " " + right.toString() + ")"; + } + + /** + * TODO: more efficient hashCode() + * + * @see java.lang.Object#hashCode() + */ + public int hashCode() + { + return toString().hashCode(); + } + + /** + * TODO: more efficient hashCode() + * + * @see java.lang.Object#equals(java.lang.Object) + */ + public boolean equals(Object o) + { + + if ((o == null) || !this.getClass().equals(o.getClass())) + { + return false; + } + + return toString().equals(o.toString()); + + } + + /** + * Returns the symbol that represents this binary expression. For example, addition is + * represented by "+" + * + * @return + */ + public abstract String getExpressionSymbol(); + +} diff --git a/java/common/src/main/java/org/apache/qpid/filter/BooleanExpression.java b/java/common/src/main/java/org/apache/qpid/filter/BooleanExpression.java new file mode 100644 index 0000000000..13e1604d5f --- /dev/null +++ b/java/common/src/main/java/org/apache/qpid/filter/BooleanExpression.java @@ -0,0 +1,36 @@ +/** + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.qpid.filter; +// +// Based on like named file from r450141 of the Apache ActiveMQ project +// + +/** + * A BooleanExpression is an expression that always + * produces a Boolean result. + */ +public interface BooleanExpression extends Expression +{ + + /** + * @param message + * @return true if the expression evaluates to Boolean.TRUE. + */ + public boolean matches(FilterableMessage message); + +} diff --git a/java/common/src/main/java/org/apache/qpid/filter/ComparisonExpression.java b/java/common/src/main/java/org/apache/qpid/filter/ComparisonExpression.java new file mode 100644 index 0000000000..2cfb97dc6c --- /dev/null +++ b/java/common/src/main/java/org/apache/qpid/filter/ComparisonExpression.java @@ -0,0 +1,596 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.filter; +// +// Based on like named file from r450141 of the Apache ActiveMQ project +// + +import java.util.HashSet; +import java.util.List; +import java.util.regex.Pattern; + +/** + * A filter performing a comparison of two objects + */ +public abstract class ComparisonExpression extends BinaryExpression implements BooleanExpression +{ + + public static BooleanExpression createBetween(Expression value, Expression left, Expression right) + { + return LogicExpression.createAND(createGreaterThanEqual(value, left), createLessThanEqual(value, right)); + } + + public static BooleanExpression createNotBetween(Expression value, Expression left, Expression right) + { + return LogicExpression.createOR(createLessThan(value, left), createGreaterThan(value, right)); + } + + private static final HashSet REGEXP_CONTROL_CHARS = new HashSet(); + + static + { + REGEXP_CONTROL_CHARS.add('.'); + REGEXP_CONTROL_CHARS.add('\\'); + REGEXP_CONTROL_CHARS.add('['); + REGEXP_CONTROL_CHARS.add(']'); + REGEXP_CONTROL_CHARS.add('^'); + REGEXP_CONTROL_CHARS.add('$'); + REGEXP_CONTROL_CHARS.add('?'); + REGEXP_CONTROL_CHARS.add('*'); + REGEXP_CONTROL_CHARS.add('+'); + REGEXP_CONTROL_CHARS.add('{'); + REGEXP_CONTROL_CHARS.add('}'); + REGEXP_CONTROL_CHARS.add('|'); + REGEXP_CONTROL_CHARS.add('('); + REGEXP_CONTROL_CHARS.add(')'); + REGEXP_CONTROL_CHARS.add(':'); + REGEXP_CONTROL_CHARS.add('&'); + REGEXP_CONTROL_CHARS.add('<'); + REGEXP_CONTROL_CHARS.add('>'); + REGEXP_CONTROL_CHARS.add('='); + REGEXP_CONTROL_CHARS.add('!'); + } + + static class LikeExpression extends UnaryExpression implements BooleanExpression + { + + private Pattern likePattern; + + /** + * @param right + */ + public LikeExpression(Expression right, String like, int escape) + { + super(right); + + StringBuffer regexp = new StringBuffer(like.length() * 2); + regexp.append("\\A"); // The beginning of the input + for (int i = 0; i < like.length(); i++) + { + char c = like.charAt(i); + if (escape == (0xFFFF & c)) + { + i++; + if (i >= like.length()) + { + // nothing left to escape... + break; + } + + char t = like.charAt(i); + regexp.append("\\x"); + regexp.append(Integer.toHexString(0xFFFF & t)); + } + else if (c == '%') + { + regexp.append(".*?"); // Do a non-greedy match + } + else if (c == '_') + { + regexp.append("."); // match one + } + else if (REGEXP_CONTROL_CHARS.contains(c)) + { + regexp.append("\\x"); + regexp.append(Integer.toHexString(0xFFFF & c)); + } + else + { + regexp.append(c); + } + } + + regexp.append("\\z"); // The end of the input + + likePattern = Pattern.compile(regexp.toString(), Pattern.DOTALL); + } + + /** + * org.apache.activemq.filter.UnaryExpression#getExpressionSymbol() + */ + public String getExpressionSymbol() + { + return "LIKE"; + } + + /** + * org.apache.activemq.filter.Expression#evaluate(MessageEvaluationContext) + */ + public Object evaluate(FilterableMessage message) + { + + Object rv = this.getRight().evaluate(message); + + if (rv == null) + { + return null; + } + + if (!(rv instanceof String)) + { + return + Boolean.FALSE; + } + + return likePattern.matcher((String) rv).matches() ? Boolean.TRUE : Boolean.FALSE; + } + + public boolean matches(FilterableMessage message) + { + Object object = evaluate(message); + + return (object != null) && (object == Boolean.TRUE); + } + } + + public static BooleanExpression createLike(Expression left, String right, String escape) + { + if ((escape != null) && (escape.length() != 1)) + { + throw new SelectorParsingException( + "The ESCAPE string literal is invalid. It can only be one character. Litteral used: " + escape); + } + + int c = -1; + if (escape != null) + { + c = 0xFFFF & escape.charAt(0); + } + + return new LikeExpression(left, right, c); + } + + public static BooleanExpression createNotLike(Expression left, String right, String escape) + { + return UnaryExpression.createNOT(createLike(left, right, escape)); + } + + public static BooleanExpression createInFilter(Expression left, List elements) + { + + if (!(left instanceof PropertyExpression)) + { + throw new SelectorParsingException("Expected a property for In expression, got: " + left); + } + + return UnaryExpression.createInExpression((PropertyExpression) left, elements, false); + + } + + public static BooleanExpression createNotInFilter(Expression left, List elements) + { + + if (!(left instanceof PropertyExpression)) + { + throw new SelectorParsingException("Expected a property for In expression, got: " + left); + } + + return UnaryExpression.createInExpression((PropertyExpression) left, elements, true); + + } + + public static BooleanExpression createIsNull(Expression left) + { + return doCreateEqual(left, ConstantExpression.NULL); + } + + public static BooleanExpression createIsNotNull(Expression left) + { + return UnaryExpression.createNOT(doCreateEqual(left, ConstantExpression.NULL)); + } + + public static BooleanExpression createNotEqual(Expression left, Expression right) + { + return UnaryExpression.createNOT(createEqual(left, right)); + } + + public static BooleanExpression createEqual(Expression left, Expression right) + { + checkEqualOperand(left); + checkEqualOperand(right); + checkEqualOperandCompatability(left, right); + + return doCreateEqual(left, right); + } + + private static BooleanExpression doCreateEqual(Expression left, Expression right) + { + return new EqualExpression(left, right); + } + + public static BooleanExpression createGreaterThan(final Expression left, final Expression right) + { + checkLessThanOperand(left); + checkLessThanOperand(right); + + return new ComparisonExpression(left, right) + { + protected boolean asBoolean(int answer) + { + return answer > 0; + } + + public String getExpressionSymbol() + { + return ">"; + } + }; + } + + public static BooleanExpression createGreaterThanEqual(final Expression left, final Expression right) + { + checkLessThanOperand(left); + checkLessThanOperand(right); + + return new ComparisonExpression(left, right) + { + protected boolean asBoolean(int answer) + { + return answer >= 0; + } + + public String getExpressionSymbol() + { + return ">="; + } + }; + } + + public static BooleanExpression createLessThan(final Expression left, final Expression right) + { + checkLessThanOperand(left); + checkLessThanOperand(right); + + return new ComparisonExpression(left, right) + { + + protected boolean asBoolean(int answer) + { + return answer < 0; + } + + public String getExpressionSymbol() + { + return "<"; + } + + }; + } + + public static BooleanExpression createLessThanEqual(final Expression left, final Expression right) + { + checkLessThanOperand(left); + checkLessThanOperand(right); + + return new ComparisonExpression(left, right) + { + + protected boolean asBoolean(int answer) + { + return answer <= 0; + } + + public String getExpressionSymbol() + { + return "<="; + } + }; + } + + /** + * Only Numeric expressions can be used in >, >=, < or <= expressions.s + * + * @param expr + */ + public static void checkLessThanOperand(Expression expr) + { + if (expr instanceof ConstantExpression) + { + Object value = ((ConstantExpression) expr).getValue(); + if (value instanceof Number) + { + return; + } + + // Else it's boolean or a String.. + throw new SelectorParsingException("Value '" + expr + "' cannot be compared."); + } + + if (expr instanceof BooleanExpression) + { + throw new SelectorParsingException("Value '" + expr + "' cannot be compared."); + } + } + + /** + * Validates that the expression can be used in == or <> expression. + * Cannot not be NULL TRUE or FALSE literals. + * + * @param expr + */ + public static void checkEqualOperand(Expression expr) + { + if (expr instanceof ConstantExpression) + { + Object value = ((ConstantExpression) expr).getValue(); + if (value == null) + { + throw new SelectorParsingException("'" + expr + "' cannot be compared."); + } + } + } + + /** + * + * @param left + * @param right + */ + private static void checkEqualOperandCompatability(Expression left, Expression right) + { + if ((left instanceof ConstantExpression) && (right instanceof ConstantExpression)) + { + if ((left instanceof BooleanExpression) && !(right instanceof BooleanExpression)) + { + throw new SelectorParsingException("'" + left + "' cannot be compared with '" + right + "'"); + } + } + } + + /** + * @param left + * @param right + */ + public ComparisonExpression(Expression left, Expression right) + { + super(left, right); + } + + public Object evaluate(FilterableMessage message) + { + Comparable lv = (Comparable) getLeft().evaluate(message); + if (lv == null) + { + return null; + } + + Comparable rv = (Comparable) getRight().evaluate(message); + if (rv == null) + { + return null; + } + + return compare(lv, rv); + } + + protected Boolean compare(Comparable lv, Comparable rv) + { + Class lc = lv.getClass(); + Class rc = rv.getClass(); + // If the the objects are not of the same type, + // try to convert up to allow the comparison. + if (lc != rc) + { + if (lc == Byte.class) + { + if (rc == Short.class) + { + lv = ((Number) lv).shortValue(); + } + else if (rc == Integer.class) + { + lv = ((Number) lv).intValue(); + } + else if (rc == Long.class) + { + lv = ((Number) lv).longValue(); + } + else if (rc == Float.class) + { + lv = ((Number) lv).floatValue(); + } + else if (rc == Double.class) + { + lv = ((Number) lv).doubleValue(); + } + else + { + return Boolean.FALSE; + } + } + else if (lc == Short.class) + { + if (rc == Integer.class) + { + lv = ((Number) lv).intValue(); + } + else if (rc == Long.class) + { + lv = ((Number) lv).longValue(); + } + else if (rc == Float.class) + { + lv = ((Number) lv).floatValue(); + } + else if (rc == Double.class) + { + lv = ((Number) lv).doubleValue(); + } + else + { + return Boolean.FALSE; + } + } + else if (lc == Integer.class) + { + if (rc == Long.class) + { + lv = ((Number) lv).longValue(); + } + else if (rc == Float.class) + { + lv = ((Number) lv).floatValue(); + } + else if (rc == Double.class) + { + lv = ((Number) lv).doubleValue(); + } + else + { + return Boolean.FALSE; + } + } + else if (lc == Long.class) + { + if (rc == Integer.class) + { + rv = ((Number) rv).longValue(); + } + else if (rc == Float.class) + { + lv = ((Number) lv).floatValue(); + } + else if (rc == Double.class) + { + lv = ((Number) lv).doubleValue(); + } + else + { + return Boolean.FALSE; + } + } + else if (lc == Float.class) + { + if (rc == Integer.class) + { + rv = ((Number) rv).floatValue(); + } + else if (rc == Long.class) + { + rv = ((Number) rv).floatValue(); + } + else if (rc == Double.class) + { + lv = ((Number) lv).doubleValue(); + } + else + { + return Boolean.FALSE; + } + } + else if (lc == Double.class) + { + if (rc == Integer.class) + { + rv = ((Number) rv).doubleValue(); + } + else if (rc == Long.class) + { + rv = ((Number) rv).doubleValue(); + } + else if (rc == Float.class) + { + rv = ((Number) rv).doubleValue(); + } + else + { + return Boolean.FALSE; + } + } + else + { + return Boolean.FALSE; + } + } + + return asBoolean(lv.compareTo(rv)) ? Boolean.TRUE : Boolean.FALSE; + } + + protected abstract boolean asBoolean(int answer); + + public boolean matches(FilterableMessage message) + { + Object object = evaluate(message); + + return (object != null) && (object == Boolean.TRUE); + } + + private static class EqualExpression extends ComparisonExpression + { + public EqualExpression(final Expression left, final Expression right) + { + super(left, right); + } + + public Object evaluate(FilterableMessage message) + { + Object lv = getLeft().evaluate(message); + Object rv = getRight().evaluate(message); + + // Iff one of the values is null + if ((lv == null) ^ (rv == null)) + { + return Boolean.FALSE; + } + + if ((lv == rv) || lv.equals(rv)) + { + return Boolean.TRUE; + } + + if ((lv instanceof Comparable) && (rv instanceof Comparable)) + { + return compare((Comparable) lv, (Comparable) rv); + } + + return Boolean.FALSE; + } + + protected boolean asBoolean(int answer) + { + return answer == 0; + } + + public String getExpressionSymbol() + { + return "="; + } + } +} diff --git a/java/common/src/main/java/org/apache/qpid/filter/ConstantExpression.java b/java/common/src/main/java/org/apache/qpid/filter/ConstantExpression.java new file mode 100644 index 0000000000..20c9f1438a --- /dev/null +++ b/java/common/src/main/java/org/apache/qpid/filter/ConstantExpression.java @@ -0,0 +1,207 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.filter; +// +// Based on like named file from r450141 of the Apache ActiveMQ project +// + +import java.math.BigDecimal; + +/** + * Represents a constant expression + */ +public class ConstantExpression implements Expression +{ + + static class BooleanConstantExpression extends ConstantExpression implements BooleanExpression + { + public BooleanConstantExpression(Object value) + { + super(value); + } + + public boolean matches(FilterableMessage message) + { + Object object = evaluate(message); + + return (object != null) && (object == Boolean.TRUE); + } + } + + public static final BooleanConstantExpression NULL = new BooleanConstantExpression(null); + public static final BooleanConstantExpression TRUE = new BooleanConstantExpression(Boolean.TRUE); + public static final BooleanConstantExpression FALSE = new BooleanConstantExpression(Boolean.FALSE); + + private Object value; + + public static ConstantExpression createFromDecimal(String text) + { + + // Strip off the 'l' or 'L' if needed. + if (text.endsWith("l") || text.endsWith("L")) + { + text = text.substring(0, text.length() - 1); + } + + Number value; + try + { + value = new Long(text); + } + catch (NumberFormatException e) + { + // The number may be too big to fit in a long. + value = new BigDecimal(text); + } + + long l = value.longValue(); + if ((Integer.MIN_VALUE <= l) && (l <= Integer.MAX_VALUE)) + { + value = value.intValue(); + } + + return new ConstantExpression(value); + } + + public static ConstantExpression createFromHex(String text) + { + Number value = Long.parseLong(text.substring(2), 16); + long l = value.longValue(); + if ((Integer.MIN_VALUE <= l) && (l <= Integer.MAX_VALUE)) + { + value = value.intValue(); + } + + return new ConstantExpression(value); + } + + public static ConstantExpression createFromOctal(String text) + { + Number value = Long.parseLong(text, 8); + long l = value.longValue(); + if ((Integer.MIN_VALUE <= l) && (l <= Integer.MAX_VALUE)) + { + value = value.intValue(); + } + + return new ConstantExpression(value); + } + + public static ConstantExpression createFloat(String text) + { + Number value = new Double(text); + + return new ConstantExpression(value); + } + + public ConstantExpression(Object value) + { + this.value = value; + } + + public Object evaluate(FilterableMessage message) + { + return value; + } + + public Object getValue() + { + return value; + } + + /** + * @see java.lang.Object#toString() + */ + public String toString() + { + if (value == null) + { + return "NULL"; + } + + if (value instanceof Boolean) + { + return ((Boolean) value) ? "TRUE" : "FALSE"; + } + + if (value instanceof String) + { + return encodeString((String) value); + } + + return value.toString(); + } + + /** + * TODO: more efficient hashCode() + * + * @see java.lang.Object#hashCode() + */ + public int hashCode() + { + return toString().hashCode(); + } + + /** + * TODO: more efficient hashCode() + * + * @see java.lang.Object#equals(java.lang.Object) + */ + public boolean equals(Object o) + { + + if ((o == null) || !this.getClass().equals(o.getClass())) + { + return false; + } + + return toString().equals(o.toString()); + + } + + /** + * Encodes the value of string so that it looks like it would look like + * when it was provided in a selector. + * + * @param s + * @return + */ + public static String encodeString(String s) + { + StringBuffer b = new StringBuffer(); + b.append('\''); + for (int i = 0; i < s.length(); i++) + { + char c = s.charAt(i); + if (c == '\'') + { + b.append(c); + } + + b.append(c); + } + + b.append('\''); + + return b.toString(); + } + +} diff --git a/java/common/src/main/java/org/apache/qpid/filter/Expression.java b/java/common/src/main/java/org/apache/qpid/filter/Expression.java new file mode 100644 index 0000000000..1030c7b588 --- /dev/null +++ b/java/common/src/main/java/org/apache/qpid/filter/Expression.java @@ -0,0 +1,34 @@ +/** + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.qpid.filter; +// +// Based on like named file from r450141 of the Apache ActiveMQ project +// + +/** + * Represents an expression + */ +public interface Expression +{ + + /** + * @return the value of this expression + */ + public Object evaluate(FilterableMessage message); + +} diff --git a/java/common/src/main/java/org/apache/qpid/filter/FilterableMessage.java b/java/common/src/main/java/org/apache/qpid/filter/FilterableMessage.java new file mode 100644 index 0000000000..b5b00ae70f --- /dev/null +++ b/java/common/src/main/java/org/apache/qpid/filter/FilterableMessage.java @@ -0,0 +1,41 @@ +package org.apache.qpid.filter; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +public interface FilterableMessage +{ + + boolean isPersistent(); + + boolean isRedelivered(); + + Object getHeader(String name); + + String getReplyTo(); + + String getType(); + + byte getPriority(); + + String getMessageId(); + + long getTimestamp(); + + String getCorrelationId(); + + long getExpiration(); +} diff --git a/java/common/src/main/java/org/apache/qpid/filter/LogicExpression.java b/java/common/src/main/java/org/apache/qpid/filter/LogicExpression.java new file mode 100644 index 0000000000..f8ec19d23b --- /dev/null +++ b/java/common/src/main/java/org/apache/qpid/filter/LogicExpression.java @@ -0,0 +1,118 @@ +/** + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.qpid.filter; +// +// Based on like named file from r450141 of the Apache ActiveMQ project +// + +/** + * A filter performing a comparison of two objects + */ +public abstract class LogicExpression extends BinaryExpression implements BooleanExpression +{ + + public static BooleanExpression createOR(BooleanExpression lvalue, BooleanExpression rvalue) + { + return new OrExpression(lvalue, rvalue); + } + + public static BooleanExpression createAND(BooleanExpression lvalue, BooleanExpression rvalue) + { + return new AndExpression(lvalue, rvalue); + } + + /** + * @param left + * @param right + */ + public LogicExpression(BooleanExpression left, BooleanExpression right) + { + super(left, right); + } + + public abstract Object evaluate(FilterableMessage message); + + public boolean matches(FilterableMessage message) + { + Object object = evaluate(message); + + return (object != null) && (object == Boolean.TRUE); + } + + private static class OrExpression extends LogicExpression + { + public OrExpression(final BooleanExpression lvalue, final BooleanExpression rvalue) + { + super(lvalue, rvalue); + } + + public Object evaluate(FilterableMessage message) + { + + Boolean lv = (Boolean) getLeft().evaluate(message); + // Can we do an OR shortcut?? + if ((lv != null) && lv.booleanValue()) + { + return Boolean.TRUE; + } + + Boolean rv = (Boolean) getRight().evaluate(message); + + return (rv == null) ? null : rv; + } + + public String getExpressionSymbol() + { + return "OR"; + } + } + + private static class AndExpression extends LogicExpression + { + public AndExpression(final BooleanExpression lvalue, final BooleanExpression rvalue) + { + super(lvalue, rvalue); + } + + public Object evaluate(FilterableMessage message) + { + + Boolean lv = (Boolean) getLeft().evaluate(message); + + // Can we do an AND shortcut?? + if (lv == null) + { + return null; + } + + if (!lv.booleanValue()) + { + return Boolean.FALSE; + } + + Boolean rv = (Boolean) getRight().evaluate(message); + + return (rv == null) ? null : rv; + } + + public String getExpressionSymbol() + { + return "AND"; + } + } +} diff --git a/java/common/src/main/java/org/apache/qpid/filter/PropertyExpression.java b/java/common/src/main/java/org/apache/qpid/filter/PropertyExpression.java new file mode 100644 index 0000000000..4fb9c0e62f --- /dev/null +++ b/java/common/src/main/java/org/apache/qpid/filter/PropertyExpression.java @@ -0,0 +1,234 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.filter; +// +// Based on like named file from r450141 of the Apache ActiveMQ project +// + + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; + +/** + * Represents a property expression + */ +public class PropertyExpression implements Expression +{ + // Constants - defined the same as JMS + private static enum JMSDeliveryMode { NON_PERSISTENT, PERSISTENT } + + private static final int DEFAULT_PRIORITY = 4; + + private static final Logger _logger = LoggerFactory.getLogger(PropertyExpression.class); + + private static final HashMap JMS_PROPERTY_EXPRESSIONS = new HashMap(); + + { + JMS_PROPERTY_EXPRESSIONS.put("JMSDestination", new Expression() + { + public Object evaluate(FilterableMessage message) + { + //TODO + return null; + } + }); + JMS_PROPERTY_EXPRESSIONS.put("JMSReplyTo", new ReplyToExpression()); + + JMS_PROPERTY_EXPRESSIONS.put("JMSType", new TypeExpression()); + + JMS_PROPERTY_EXPRESSIONS.put("JMSDeliveryMode", new DeliveryModeExpression()); + + JMS_PROPERTY_EXPRESSIONS.put("JMSPriority", new PriorityExpression()); + + JMS_PROPERTY_EXPRESSIONS.put("JMSMessageID", new MessageIDExpression()); + + JMS_PROPERTY_EXPRESSIONS.put("AMQMessageID", new MessageIDExpression()); + + JMS_PROPERTY_EXPRESSIONS.put("JMSTimestamp", new TimestampExpression()); + + JMS_PROPERTY_EXPRESSIONS.put("JMSCorrelationID", new CorrelationIdExpression()); + + JMS_PROPERTY_EXPRESSIONS.put("JMSExpiration", new ExpirationExpression()); + + JMS_PROPERTY_EXPRESSIONS.put("JMSRedelivered", new Expression() + { + public Object evaluate(FilterableMessage message) + { + return message.isRedelivered(); + } + }); + } + + private final String name; + private final Expression jmsPropertyExpression; + + public boolean outerTest() + { + return false; + } + + public PropertyExpression(String name) + { + this.name = name; + + + + jmsPropertyExpression = JMS_PROPERTY_EXPRESSIONS.get(name); + } + + public Object evaluate(FilterableMessage message) + { + + if (jmsPropertyExpression != null) + { + return jmsPropertyExpression.evaluate(message); + } + else + { + return message.getHeader(name); + } + } + + public String getName() + { + return name; + } + + /** + * @see java.lang.Object#toString() + */ + public String toString() + { + return name; + } + + /** + * @see java.lang.Object#hashCode() + */ + public int hashCode() + { + return name.hashCode(); + } + + /** + * @see java.lang.Object#equals(java.lang.Object) + */ + public boolean equals(Object o) + { + + if ((o == null) || !this.getClass().equals(o.getClass())) + { + return false; + } + + return name.equals(((PropertyExpression) o).name); + + } + + private static class ReplyToExpression implements Expression + { + public Object evaluate(FilterableMessage message) + { + String replyTo = message.getReplyTo(); + return replyTo; + } + + } + + private static class TypeExpression implements Expression + { + public Object evaluate(FilterableMessage message) + { + + String type = message.getType(); + return type; + + } + } + + private static class DeliveryModeExpression implements Expression + { + public Object evaluate(FilterableMessage message) + { + JMSDeliveryMode mode = message.isPersistent() ? JMSDeliveryMode.PERSISTENT : + JMSDeliveryMode.NON_PERSISTENT; + if (_logger.isDebugEnabled()) + { + _logger.debug("JMSDeliveryMode is :" + mode); + } + + return mode.toString(); + } + } + + private static class PriorityExpression implements Expression + { + public Object evaluate(FilterableMessage message) + { + byte priority = message.getPriority(); + return (int) priority; + } + } + + private static class MessageIDExpression implements Expression + { + public Object evaluate(FilterableMessage message) + { + + String messageId = message.getMessageId(); + + return messageId; + + } + } + + private static class TimestampExpression implements Expression + { + public Object evaluate(FilterableMessage message) + { + long timestamp = message.getTimestamp(); + return timestamp; + } + } + + private static class CorrelationIdExpression implements Expression + { + public Object evaluate(FilterableMessage message) + { + + String correlationId = message.getCorrelationId(); + + return correlationId; + } + } + + private static class ExpirationExpression implements Expression + { + public Object evaluate(FilterableMessage message) + { + long expiration = message.getExpiration(); + return expiration; + + } + } +} diff --git a/java/common/src/main/java/org/apache/qpid/filter/SelectorParsingException.java b/java/common/src/main/java/org/apache/qpid/filter/SelectorParsingException.java new file mode 100644 index 0000000000..47892edc33 --- /dev/null +++ b/java/common/src/main/java/org/apache/qpid/filter/SelectorParsingException.java @@ -0,0 +1,40 @@ +package org.apache.qpid.filter; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +public class SelectorParsingException extends RuntimeException +{ + public SelectorParsingException(String s) + { + super(s); + } + + public SelectorParsingException(String message, Throwable cause) + { + super(message, cause); + } + + public SelectorParsingException(Throwable cause) + { + super(cause); + } + + public SelectorParsingException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) + { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/java/common/src/main/java/org/apache/qpid/filter/UnaryExpression.java b/java/common/src/main/java/org/apache/qpid/filter/UnaryExpression.java new file mode 100644 index 0000000000..b80b89840c --- /dev/null +++ b/java/common/src/main/java/org/apache/qpid/filter/UnaryExpression.java @@ -0,0 +1,340 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.filter; +// +// Based on like named file from r450141 of the Apache ActiveMQ project +// + +import java.math.BigDecimal; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; + +/** + * An expression which performs an operation on two expression values + */ +public abstract class UnaryExpression implements Expression +{ + + private static final BigDecimal BD_LONG_MIN_VALUE = BigDecimal.valueOf(Long.MIN_VALUE); + private Expression right; + + public static Expression createNegate(Expression left) + { + return new NegativeExpression(left); + } + + public static BooleanExpression createInExpression(PropertyExpression right, List elements, final boolean not) + { + + // Use a HashSet if there are many elements. + Collection t; + if (elements.size() == 0) + { + t = null; + } + else if (elements.size() < 5) + { + t = elements; + } + else + { + t = new HashSet(elements); + } + + final Collection inList = t; + + return new InExpression(right, inList, not); + } + + abstract static class BooleanUnaryExpression extends UnaryExpression implements BooleanExpression + { + public BooleanUnaryExpression(Expression left) + { + super(left); + } + + public boolean matches(FilterableMessage message) + { + Object object = evaluate(message); + + return (object != null) && (object == Boolean.TRUE); + } + } + + public static BooleanExpression createNOT(BooleanExpression left) + { + return new NotExpression(left); + } + + public static BooleanExpression createBooleanCast(Expression left) + { + return new BooleanCastExpression(left); + } + + private static Number negate(Number left) + { + Class clazz = left.getClass(); + if (clazz == Integer.class) + { + return -left.intValue(); + } + else if (clazz == Long.class) + { + return -left.longValue(); + } + else if (clazz == Float.class) + { + return -left.floatValue(); + } + else if (clazz == Double.class) + { + return -left.doubleValue(); + } + else if (clazz == BigDecimal.class) + { + // We ussually get a big deciamal when we have Long.MIN_VALUE constant in the + // Selector. Long.MIN_VALUE is too big to store in a Long as a positive so we store it + // as a Big decimal. But it gets Negated right away.. to here we try to covert it back + // to a Long. + BigDecimal bd = (BigDecimal) left; + bd = bd.negate(); + + if (BD_LONG_MIN_VALUE.compareTo(bd) == 0) + { + return Long.MIN_VALUE; + } + + return bd; + } + else + { + throw new SelectorParsingException("Don't know how to negate: " + left); + } + } + + public UnaryExpression(Expression left) + { + this.right = left; + } + + public Expression getRight() + { + return right; + } + + /** + * @see java.lang.Object#toString() + */ + public String toString() + { + return "(" + getExpressionSymbol() + " " + right.toString() + ")"; + } + + /** + * TODO: more efficient hashCode() + * + * @see java.lang.Object#hashCode() + */ + public int hashCode() + { + return toString().hashCode(); + } + + /** + * TODO: more efficient hashCode() + * + * @see java.lang.Object#equals(java.lang.Object) + */ + public boolean equals(Object o) + { + return ((o != null) && this.getClass().equals(o.getClass())) && toString().equals(o.toString()); + } + + /** + * Returns the symbol that represents this binary expression. For example, addition is + * represented by "+" + * + * @return symbol + */ + public abstract String getExpressionSymbol(); + + private static class NegativeExpression extends UnaryExpression + { + public NegativeExpression(final Expression left) + { + super(left); + } + + public Object evaluate(FilterableMessage message) + { + Object rvalue = getRight().evaluate(message); + if (rvalue == null) + { + return null; + } + + if (rvalue instanceof Number) + { + return negate((Number) rvalue); + } + + return null; + } + + public String getExpressionSymbol() + { + return "-"; + } + } + + private static class InExpression extends BooleanUnaryExpression + { + private final Collection _inList; + private final boolean _not; + + public InExpression(final PropertyExpression right, final Collection inList, final boolean not) + { + super(right); + _inList = inList; + _not = not; + } + + public Object evaluate(FilterableMessage message) + { + + Object rvalue = getRight().evaluate(message); + if (rvalue == null) + { + return null; + } + + if (rvalue.getClass() != String.class) + { + return null; + } + + if (((_inList != null) && _inList.contains(rvalue)) ^ _not) + { + return Boolean.TRUE; + } + else + { + return Boolean.FALSE; + } + + } + + public String toString() + { + StringBuilder answer = new StringBuilder(String.valueOf(getRight())); + answer.append(" "); + answer.append(getExpressionSymbol()); + answer.append(" ( "); + + int count = 0; + for (Object o : _inList) + { + if (count != 0) + { + answer.append(", "); + } + + answer.append(o); + count++; + } + + answer.append(" )"); + + return answer.toString(); + } + + public String getExpressionSymbol() + { + if (_not) + { + return "NOT IN"; + } + else + { + return "IN"; + } + } + } + + private static class NotExpression extends BooleanUnaryExpression + { + public NotExpression(final BooleanExpression left) + { + super(left); + } + + public Object evaluate(FilterableMessage message) + { + Boolean lvalue = (Boolean) getRight().evaluate(message); + if (lvalue == null) + { + return null; + } + + return lvalue ? Boolean.FALSE : Boolean.TRUE; + } + + public String getExpressionSymbol() + { + return "NOT"; + } + } + + private static class BooleanCastExpression extends BooleanUnaryExpression + { + public BooleanCastExpression(final Expression left) + { + super(left); + } + + public Object evaluate(FilterableMessage message) + { + Object rvalue = getRight().evaluate(message); + if (rvalue == null) + { + return null; + } + + if (!rvalue.getClass().equals(Boolean.class)) + { + return Boolean.FALSE; + } + + return ((Boolean) rvalue) ? Boolean.TRUE : Boolean.FALSE; + } + + public String toString() + { + return getRight().toString(); + } + + public String getExpressionSymbol() + { + return ""; + } + } +} diff --git a/java/lib/javacc.jar b/java/lib/javacc.jar index 5506008402..2550727eef 100644 Binary files a/java/lib/javacc.jar and b/java/lib/javacc.jar differ -- cgit v1.2.1