1 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 ; 21 import java.util.regex.Pattern ; 22 23 public class JmsPropertyFilter implements UMOFilter 24 { 25 28 private static Log logger = LogFactory.getLog(JmsPropertyFilter.class); 29 30 33 private String propertyName = null; 34 35 38 private String propertyClass = null; 39 40 43 private String expression = null; 44 45 48 private Pattern 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 ) { 63 try 64 { 65 Message m = (javax.jms.Message )message.getPayload(); 66 67 if (StringUtils.isBlank(propertyClass)) { 68 Object object = m.getObjectProperty(propertyName); 69 if (object == null) return false; 70 String 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 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 nfe) { 104 logger.warn("Unable to convert expression " + 105 expression + " to " + propertyClass + ": " + 106 nfe.toString()); 107 } 108 catch (Exception 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 123 public void setExpression(String expression) 124 { 125 this.expression = expression; 126 } 127 128 131 public String getExpression() 132 { 133 return expression; 134 } 135 136 139 public void setPropertyName(String propertyName) 140 { 141 this.propertyName = propertyName; 142 } 143 144 147 public String getPropertyName() 148 { 149 return propertyName; 150 } 151 152 155 public void setPropertyClass(String propertyClass) 156 { 157 this.propertyClass = propertyClass; 158 } 159 160 163 public String getPropertyClass() 164 { 165 return propertyClass; 166 } 167 168 171 public String getPattern() 172 { 173 return (pattern == null ? null : pattern.pattern()); 174 } 175 176 179 public void setPattern(String pattern) 180 { 181 this.pattern = Pattern.compile(pattern); 182 } 183 184 } 185 | Popular Tags |