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