1 23 24 package com.sun.appserv.web.cache.mapping; 25 26 import java.text.MessageFormat ; 27 28 import java.util.logging.Logger ; 29 import java.util.ResourceBundle ; 30 31 import com.sun.enterprise.web.logging.pwc.LogDomains; 32 33 36 public class ValueConstraint { 37 38 private static Logger _logger; 40 43 private static ResourceBundle _rb = null; 44 45 private String matchValue = null; 47 private float minValue = Float.MIN_VALUE; 48 private float maxValue = Float.MAX_VALUE; 49 50 private int matchExpr = Constants.MATCH_EQUALS; 52 53 private String str; 55 56 private boolean cacheOnMatch = true; 58 private boolean cacheOnMatchFailure = false; 60 61 65 public ValueConstraint(String value, String expr) 66 throws IllegalArgumentException { 67 int match; 68 69 _logger = LogDomains.getLogger(LogDomains.PWC_LOGGER); 71 _rb = _logger.getResourceBundle(); 72 73 if (expr == null || expr.equals("equals")) { 74 match = Constants.MATCH_EQUALS; 75 } else if (expr.equals("greater")) { 76 match = Constants.MATCH_GREATER; 77 try { 78 minValue = Float.parseFloat(value); 79 } catch (NumberFormatException nfe) { 80 String msg = _rb.getString("cache.mapping.greaterExpRequiresNumeric"); 81 Object [] params = { value }; 82 msg = MessageFormat.format(msg, params); 83 84 throw new IllegalArgumentException (msg); 85 } 86 } 87 else if (expr.equals("lesser")) { 88 match = Constants.MATCH_LESSER; 89 try { 90 maxValue = Float.parseFloat(value); 91 } catch (NumberFormatException nfe) { 92 String msg = _rb.getString("cache.mapping.lesserExpRequiresNumeric"); 93 Object [] params = { value }; 94 msg = MessageFormat.format(msg, params); 95 96 throw new IllegalArgumentException (msg); 97 } 98 } 99 else if (expr.equals("not-equals")) 100 match = Constants.MATCH_NOT_EQUALS; 101 else if (expr.equals("in-range")) { 102 match = Constants.MATCH_IN_RANGE; 103 parseRangeValue(value); 104 } 105 else { 106 String msg = _rb.getString("cache.mapping.illegalValueExpr"); 107 Object [] params = { value, expr }; 108 msg = MessageFormat.format(msg, params); 109 110 throw new IllegalArgumentException (msg); 111 } 112 113 this.matchExpr = match; 114 this.matchValue = value; 115 } 116 117 128 private void parseRangeValue(String value) throws IllegalArgumentException { 129 float val1, val2; 130 131 if (value == null || value.length() <= 2) { 132 String msg = _rb.getString("cache.mapping.illegalValueRange"); 133 Object [] params = { value }; 134 msg = MessageFormat.format(msg, params); 135 136 throw new IllegalArgumentException (msg); 137 } 138 139 int separator; 141 if (value.charAt(0) == '-') { 142 separator = value.indexOf('-', 1); 143 } else { 144 separator = value.indexOf('-', 0); 145 } 146 if (separator == -1) { 147 String msg = _rb.getString("cache.mapping.missingRangeSep"); 148 Object [] params = { value }; 149 msg = MessageFormat.format(msg, params); 150 151 throw new IllegalArgumentException (msg); 152 } 153 154 String sval1 = value.substring(0, separator).trim(); 156 try { 157 val1 = Float.parseFloat(sval1); 158 } catch (NumberFormatException nfe) { 159 String msg = _rb.getString("cache.mapping.lowerRangeRequiresNumber"); 160 Object [] params = { sval1 }; 161 msg = MessageFormat.format(msg, params); 162 163 throw new IllegalArgumentException (msg); 164 } 165 166 if (separator == value.length()){ 168 String msg = _rb.getString("cache.mapping.rangeRequiresUpperBound"); 169 Object [] params = { value }; 170 msg = MessageFormat.format(msg, params); 171 172 throw new IllegalArgumentException (msg); 173 } 174 175 String sval2 = value.substring(separator + 1).trim(); 176 try { 177 val2 = Float.parseFloat(sval2); 178 } catch (NumberFormatException nfe) { 179 String msg = _rb.getString("cache.mapping.upperRangeRequiresNumber"); 180 Object [] params = { sval2 }; 181 msg = MessageFormat.format(msg, params); 182 183 throw new IllegalArgumentException (msg); 184 } 185 186 this.minValue = val1; 187 this.maxValue = val2; 188 } 189 190 193 public void setValue(String value) { 194 this.matchValue = value; 195 } 196 197 200 public void setMinValue(float value) { 201 this.minValue = value; 202 } 203 204 207 public void setMaxValue(float value) { 208 this.maxValue = value; 209 } 210 211 214 public void setMatchExpr(int expr) { 215 this.matchExpr = expr; 216 } 217 218 221 public void setCacheOnMatch(boolean cacheOnMatch) { 222 this.cacheOnMatch = cacheOnMatch; 223 } 224 225 229 public void setCacheOnMatchFailure(boolean cacheOnMatchFailure) { 230 this.cacheOnMatchFailure = cacheOnMatchFailure; 231 } 232 233 237 public boolean matches(Object value) { 238 boolean result = false; 239 switch (matchExpr) { 240 case Constants.MATCH_EQUALS: 241 result = matchValue.equals(value); 242 break; 243 case Constants.MATCH_NOT_EQUALS: 244 result = !(matchValue.equals(value)); 245 break; 246 case Constants.MATCH_GREATER: 247 { 249 Float lval = new Float (value.toString()); 250 result = (lval.floatValue() > minValue); 251 } 252 break; 253 case Constants.MATCH_LESSER: 254 { 256 Float lval = new Float (value.toString()); 257 result = (lval.floatValue() < maxValue); 258 } 259 break; 260 case Constants.MATCH_IN_RANGE: 261 { 263 Float lval = new Float (value.toString()); 264 result = (lval.floatValue() >= minValue && 265 lval.floatValue() <= maxValue); 266 } 267 break; 268 } 269 270 return (result == true) ? cacheOnMatch : cacheOnMatchFailure; 272 } 273 274 277 public String toString() { 278 if (str == null) { 279 str = "match value = " + matchValue + " match expr = " + Constants.EXPR_NAMES[matchExpr]; 280 } 281 return str; 282 } 283 } 284 | Popular Tags |