KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > filter > MessageEvaluationContext


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.filter;
19
20 import java.io.IOException JavaDoc;
21
22 import org.apache.activemq.broker.region.MessageReference;
23 import org.apache.activemq.command.ActiveMQDestination;
24 import org.apache.activemq.command.Message;
25
26 /**
27  * MessageEvaluationContext is used to cache selection results.
28  *
29  * A message usually has multiple selectors applied against it. Some selector
30  * have a high cost of evaluating against the message. Those selectors may whish
31  * to cache evaluation results associated with the message in the
32  * MessageEvaluationContext.
33  *
34  * @version $Revision: 1.4 $
35  */

36 public class MessageEvaluationContext {
37
38     private MessageReference messageReference;
39     private boolean loaded=false;
40     private boolean dropped;
41     private Message message;
42     private ActiveMQDestination destination;
43
44     public MessageEvaluationContext() {
45     }
46
47     public boolean isDropped() throws IOException JavaDoc {
48         getMessage();
49         return dropped;
50     }
51     
52     public Message getMessage() throws IOException JavaDoc {
53         if( !dropped && !loaded ) {
54             loaded=true;
55             messageReference.incrementReferenceCount();
56             message = messageReference.getMessage();
57             if(message==null) {
58                 messageReference.decrementReferenceCount();
59                 dropped=true;
60                 loaded=false;
61             }
62         }
63         return message;
64     }
65
66     public void setMessageReference(MessageReference messageReference) {
67         if (this.messageReference != messageReference) {
68             clearMessageCache();
69         }
70         this.messageReference = messageReference;
71     }
72
73     public void clear() {
74         clearMessageCache();
75         destination = null;
76     }
77
78     public ActiveMQDestination getDestination() {
79         return destination;
80     }
81
82     public void setDestination(ActiveMQDestination destination) {
83         this.destination = destination;
84     }
85
86     /**
87      * A strategy hook to allow per-message caches to be cleared
88      */

89     protected void clearMessageCache() {
90         if( loaded ) {
91             messageReference.decrementReferenceCount();
92         }
93         message=null;
94         dropped=false;
95         loaded=false;
96     }
97 }
98
Popular Tags