KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > jms > filters > JmsPropertyFilter


1 /*
2  * $Id:
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.jms.filters;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15
16 import org.mule.umo.UMOFilter;
17 import org.mule.umo.UMOMessage;
18 import org.mule.util.StringUtils;
19
20 import javax.jms.Message JavaDoc;
21 import java.util.regex.Pattern JavaDoc;
22
23 public class JmsPropertyFilter implements UMOFilter
24 {
25     /**
26      * Logger used by this class
27      */

28     private static Log logger = LogFactory.getLog(JmsPropertyFilter.class);
29
30     /**
31      * Name of the JMS property to filter on
32      */

33     private String JavaDoc propertyName = null;
34
35     /**
36      * Class type of the JMS property
37      */

38     private String JavaDoc propertyClass = null;
39
40     /**
41      * Expression value to match on
42      */

43     private String JavaDoc expression = null;
44
45     /**
46      * Optional regular expression pattern to search on
47      */

48     private Pattern JavaDoc pattern = null;
49
50     public boolean accept(UMOMessage message)
51     {
52         if (StringUtils.isBlank(propertyName)) {
53             logger.warn("No property name was specified");
54             return false;
55         }
56         
57         if (StringUtils.isBlank(expression) && pattern == null) {
58             logger.warn("Either no expression or pattern was specified");
59             return false;
60         }
61         
62         if (message.getPayload() instanceof javax.jms.Message JavaDoc) {
63             try
64         {
65                 Message m = (javax.jms.Message JavaDoc)message.getPayload();
66
67         if (StringUtils.isBlank(propertyClass)) {
68             Object JavaDoc object = m.getObjectProperty(propertyName);
69             if (object == null) return false;
70             String JavaDoc value = object.toString();
71
72             if (pattern != null)
73             {
74             return pattern.matcher(value).find();
75             }
76             else
77             {
78             return value.equals(expression);
79             }
80         }
81         else if (propertyClass.equals("java.lang.String"))
82         {
83             String JavaDoc value = m.getStringProperty(propertyName);
84             if (value == null) return false;
85
86             if (pattern != null)
87             {
88             return pattern.matcher(value).find();
89             }
90             else
91             {
92             return value.equals(expression);
93             }
94         } else if (propertyClass.equals("java.lang.Integer")) {
95             int value = m.getIntProperty(propertyName);
96             int match = Integer.parseInt(expression);
97             return (value == match);
98         } else if (propertyClass.equals("java.lang.Short")) {
99             short value = m.getShortProperty(propertyName);
100             short match = Short.parseShort(expression);
101             return (value == match);
102         }
103         } catch (NumberFormatException JavaDoc nfe) {
104             logger.warn("Unable to convert expression " +
105             expression + " to " + propertyClass + ": " +
106                 nfe.toString());
107         }
108         catch (Exception JavaDoc e)
109         {
110         logger.warn("Error filtering on property " + propertyName
111             + ": " + e.toString());
112         }
113         } else {
114             logger.warn("Expected a payload of javax.jms.Message but instead received " + message.getPayload().getClass().getName());
115         }
116         
117         return false;
118     }
119
120     /**
121      * Sets the match expression
122      */

123     public void setExpression(String JavaDoc expression)
124     {
125         this.expression = expression;
126     }
127     
128     /**
129      * Returns the match expression
130      */

131     public String JavaDoc getExpression()
132     {
133         return expression;
134     }
135
136     /**
137      * Sets the name of the property
138      */

139     public void setPropertyName(String JavaDoc propertyName)
140     {
141         this.propertyName = propertyName;
142     }
143     
144     /**
145      * Returns the name of the property
146      */

147     public String JavaDoc getPropertyName()
148     {
149         return propertyName;
150     }
151
152     /**
153      * Sets the class type of the property
154      */

155     public void setPropertyClass(String JavaDoc propertyClass)
156     {
157         this.propertyClass = propertyClass;
158     }
159     
160     /**
161      * Returns the class type of the property
162      */

163     public String JavaDoc getPropertyClass()
164     {
165         return propertyClass;
166     }
167
168     /**
169      * Sets the regex pattern to match on
170      */

171     public String JavaDoc getPattern()
172     {
173         return (pattern == null ? null : pattern.pattern());
174     }
175
176     /**
177      * Return the regex pattern to match on
178      */

179     public void setPattern(String JavaDoc pattern)
180     {
181         this.pattern = Pattern.compile(pattern);
182     }
183
184 }
185
Popular Tags