KickJava   Java API By Example, From Geeks To Geeks.

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


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
19 package org.apache.activemq.filter;
20
21 import java.io.IOException JavaDoc;
22 import java.util.HashMap JavaDoc;
23
24 import javax.jms.DeliveryMode JavaDoc;
25 import javax.jms.JMSException JavaDoc;
26
27 import org.apache.activemq.command.ActiveMQDestination;
28 import org.apache.activemq.command.Message;
29 import org.apache.activemq.command.TransactionId;
30 import org.apache.activemq.util.JMSExceptionSupport;
31
32 /**
33  * Represents a property expression
34  *
35  * @version $Revision: 1.5 $
36  */

37 public class PropertyExpression implements Expression {
38
39     interface SubExpression {
40         public Object JavaDoc evaluate(Message message);
41     }
42     
43     static final private HashMap JavaDoc JMS_PROPERTY_EXPRESSIONS = new HashMap JavaDoc();
44     static {
45         JMS_PROPERTY_EXPRESSIONS.put("JMSDestination", new SubExpression() {
46             public Object JavaDoc evaluate(Message message) {
47                 ActiveMQDestination dest = message.getOriginalDestination();
48                 if( dest == null )
49                     dest = message.getDestination();
50                 if( dest == null )
51                     return null;
52                 return dest.toString();
53             }
54         });
55         JMS_PROPERTY_EXPRESSIONS.put("JMSReplyTo", new SubExpression() {
56             public Object JavaDoc evaluate(Message message) {
57                 if( message.getReplyTo() == null )
58                     return null;
59                 return message.getReplyTo().toString();
60             }
61         });
62         JMS_PROPERTY_EXPRESSIONS.put("JMSType", new SubExpression() {
63             public Object JavaDoc evaluate(Message message) {
64                 return message.getType();
65             }
66         });
67         JMS_PROPERTY_EXPRESSIONS.put("JMSDeliveryMode", new SubExpression() {
68             public Object JavaDoc evaluate(Message message) {
69                 return new Integer JavaDoc(message.isPersistent() ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT );
70             }
71         });
72         JMS_PROPERTY_EXPRESSIONS.put("JMSPriority", new SubExpression() {
73             public Object JavaDoc evaluate(Message message) {
74                 return new Integer JavaDoc(message.getPriority());
75             }
76         });
77         JMS_PROPERTY_EXPRESSIONS.put("JMSMessageID", new SubExpression() {
78             public Object JavaDoc evaluate(Message message) {
79                 if( message.getMessageId() == null )
80                     return null;
81                 return message.getMessageId().toString();
82             }
83         });
84         JMS_PROPERTY_EXPRESSIONS.put("JMSTimestamp", new SubExpression() {
85             public Object JavaDoc evaluate(Message message) {
86                 return new Long JavaDoc(message.getTimestamp());
87             }
88         });
89         JMS_PROPERTY_EXPRESSIONS.put("JMSCorrelationID", new SubExpression() {
90             public Object JavaDoc evaluate(Message message) {
91                 return message.getCorrelationId();
92             }
93         });
94         JMS_PROPERTY_EXPRESSIONS.put("JMSExpiration", new SubExpression() {
95             public Object JavaDoc evaluate(Message message) {
96                 return new Long JavaDoc(message.getExpiration());
97             }
98         });
99         JMS_PROPERTY_EXPRESSIONS.put("JMSPriority", new SubExpression() {
100             public Object JavaDoc evaluate(Message message) {
101                 return new Integer JavaDoc(message.getPriority());
102             }
103         });
104         JMS_PROPERTY_EXPRESSIONS.put("JMSTimestamp", new SubExpression() {
105             public Object JavaDoc evaluate(Message message) {
106                 return new Long JavaDoc(message.getTimestamp());
107             }
108         });
109         JMS_PROPERTY_EXPRESSIONS.put("JMSRedelivered", new SubExpression() {
110             public Object JavaDoc evaluate(Message message) {
111                 return new Boolean JavaDoc(message.isRedelivered());
112             }
113         });
114         
115         JMS_PROPERTY_EXPRESSIONS.put("JMSXDeliveryCount", new SubExpression() {
116             public Object JavaDoc evaluate(Message message) {
117                 return new Integer JavaDoc(message.getRedeliveryCounter()+1);
118             }
119         });
120         JMS_PROPERTY_EXPRESSIONS.put("JMSXGroupID", new SubExpression() {
121             public Object JavaDoc evaluate(Message message) {
122                 return message.getGroupID();
123             }
124         });
125         JMS_PROPERTY_EXPRESSIONS.put("JMSXGroupSeq", new SubExpression() {
126             public Object JavaDoc evaluate(Message message) {
127                 return new Integer JavaDoc(message.getGroupSequence());
128             }
129         });
130         JMS_PROPERTY_EXPRESSIONS.put("JMSXProducerTXID", new SubExpression() {
131             public Object JavaDoc evaluate(Message message) {
132                 TransactionId txId = message.getOriginalTransactionId();
133                 if( txId==null)
134                     txId = message.getTransactionId();
135                 if( txId==null )
136                     return null;
137                 return new Integer JavaDoc(txId.toString());
138             }
139         });
140     }
141     
142     private final String JavaDoc name;
143     private final SubExpression jmsPropertyExpression;
144     
145     public PropertyExpression(String JavaDoc name) {
146         this.name = name;
147         jmsPropertyExpression = (SubExpression) JMS_PROPERTY_EXPRESSIONS.get(name);
148     }
149
150     public Object JavaDoc evaluate(MessageEvaluationContext message) throws JMSException JavaDoc {
151         try {
152             if( message.isDropped() )
153                 return null;
154             
155             if( jmsPropertyExpression!=null )
156                 return jmsPropertyExpression.evaluate(message.getMessage());
157             try {
158                 return message.getMessage().getProperty(name);
159             } catch (IOException JavaDoc ioe) {
160                 throw JMSExceptionSupport.create("Could not get property: "+name+" reason: "+ioe.getMessage(), ioe);
161             }
162         } catch (IOException JavaDoc e) {
163             throw JMSExceptionSupport.create(e);
164         }
165
166     }
167     
168     public Object JavaDoc evaluate(Message message) throws JMSException JavaDoc {
169         if( jmsPropertyExpression!=null )
170             return jmsPropertyExpression.evaluate(message);
171         try {
172             return message.getProperty(name);
173         } catch (IOException JavaDoc ioe) {
174             throw JMSExceptionSupport.create(ioe);
175         }
176     }
177
178     public String JavaDoc getName() {
179         return name;
180     }
181
182
183     /**
184      * @see java.lang.Object#toString()
185      */

186     public String JavaDoc toString() {
187         return name;
188     }
189
190     /**
191      * @see java.lang.Object#hashCode()
192      */

193     public int hashCode() {
194         return name.hashCode();
195     }
196
197     /**
198      * @see java.lang.Object#equals(java.lang.Object)
199      */

200     public boolean equals(Object JavaDoc o) {
201
202         if (o == null || !this.getClass().equals(o.getClass())) {
203             return false;
204         }
205         return name.equals(((PropertyExpression) o).name);
206
207     }
208
209 }
210
Popular Tags