001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005 This file is part of Granite Data Services.
006
007 Granite Data Services is free software; you can redistribute it and/or modify
008 it under the terms of the GNU Library General Public License as published by
009 the Free Software Foundation; either version 2 of the License, or (at your
010 option) any later version.
011
012 Granite Data Services is distributed in the hope that it will be useful, but
013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 for more details.
016
017 You should have received a copy of the GNU Library General Public License
018 along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020
021 package org.granite.gravity.selector;
022
023 import java.io.IOException;
024
025 import javax.jms.JMSException;
026
027 import org.apache.activemq.command.Response;
028 import org.apache.activemq.filter.BooleanExpression;
029 import org.apache.activemq.filter.MessageEvaluationContext;
030 import org.apache.activemq.selector.SelectorParser;
031 import org.apache.activemq.state.CommandVisitor;
032
033 import flex.messaging.messages.Message;
034
035 /**
036 * @author William DRAI
037 */
038 public class ActiveMQMessageSelector implements MessageSelector {
039
040 private BooleanExpression expression;
041
042
043 public ActiveMQMessageSelector(String selector) {
044 try {
045 this.expression = SelectorParser.parse(selector);
046 }
047 catch (Exception e) {
048 throw new RuntimeException("ActiveMQ SelectorParser error " + selector, e);
049 }
050 }
051
052 public boolean accept(Message message) {
053 try {
054 MessageEvaluationContext context = new MessageEvaluationContext();
055 MessageAdapter ma = new MessageAdapter(message);
056 context.setMessageReference(ma);
057
058 return expression.matches(context);
059 }
060 catch (Exception e) {
061 throw new RuntimeException("ActiveMQ selector accept error " + message, e);
062 }
063 }
064
065
066 private static class MessageAdapter extends org.apache.activemq.command.Message {
067 private Message message = null;
068
069 public MessageAdapter(Message message) {
070 this.message = message;
071 }
072
073 @Override
074 public Object getProperty(String name) throws IOException {
075 return message.getHeader(name);
076 }
077
078 @Override
079 public org.apache.activemq.command.Message copy() {
080 return null;
081 }
082
083 public Response visit(CommandVisitor visitor) throws Exception {
084 return null;
085 }
086
087 public byte getDataStructureType() {
088 return 0;
089 }
090
091 @Override
092 public void clearBody() throws JMSException {
093 }
094 }
095 }