001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.activemq.filter; 018 019import java.io.IOException; 020 021import org.apache.activemq.broker.region.MessageReference; 022import org.apache.activemq.command.ActiveMQDestination; 023import org.apache.activemq.command.Message; 024 025/** 026 * MessageEvaluationContext is used to cache selection results. A message 027 * usually has multiple selectors applied against it. Some selector have a high 028 * cost of evaluating against the message. Those selectors may whish to cache 029 * evaluation results associated with the message in the 030 * MessageEvaluationContext. 031 * 032 * 033 */ 034public class MessageEvaluationContext { 035 036 protected MessageReference messageReference; 037 protected boolean loaded; 038 protected boolean dropped; 039 protected Message message; 040 protected ActiveMQDestination destination; 041 042 public MessageEvaluationContext() { 043 } 044 045 public boolean isDropped() throws IOException { 046 getMessage(); 047 return dropped; 048 } 049 050 public Message getMessage() throws IOException { 051 if (!dropped && !loaded) { 052 loaded = true; 053 messageReference.incrementReferenceCount(); 054 message = messageReference.getMessage(); 055 if (message == null) { 056 messageReference.decrementReferenceCount(); 057 dropped = true; 058 loaded = false; 059 } 060 } 061 return message; 062 } 063 064 public void setMessageReference(MessageReference messageReference) { 065 if (this.messageReference != messageReference) { 066 clearMessageCache(); 067 } 068 this.messageReference = messageReference; 069 } 070 071 public void clear() { 072 clearMessageCache(); 073 destination = null; 074 } 075 076 public ActiveMQDestination getDestination() { 077 return destination; 078 } 079 080 public void setDestination(ActiveMQDestination destination) { 081 this.destination = destination; 082 } 083 084 /** 085 * A strategy hook to allow per-message caches to be cleared 086 */ 087 protected void clearMessageCache() { 088 if (loaded) { 089 messageReference.decrementReferenceCount(); 090 } 091 message = null; 092 dropped = false; 093 loaded = false; 094 } 095 096 public MessageReference getMessageReference() { 097 return messageReference; 098 } 099}