1 24 package org.ofbiz.base.util; 25 26 import java.net.URL ; 27 import java.text.MessageFormat ; 28 import java.util.List ; 29 import java.util.Locale ; 30 import java.util.Map ; 31 import java.util.MissingResourceException ; 32 import java.util.Properties ; 33 import java.util.ResourceBundle ; 34 import java.util.Set ; 35 import java.io.FileOutputStream ; 36 import java.io.FileNotFoundException ; 37 import java.io.IOException ; 38 39 import javolution.util.FastSet; 40 41 import org.ofbiz.base.util.collections.FlexibleProperties; 42 import org.ofbiz.base.util.collections.ResourceBundleMapWrapper; 43 import org.ofbiz.base.util.string.FlexibleStringExpander; 44 import org.ofbiz.base.util.cache.UtilCache; 45 46 53 public class UtilProperties implements java.io.Serializable { 54 55 public static final String module = UtilProperties.class.getName(); 56 57 61 protected static UtilCache resourceCache = new UtilCache("properties.UtilPropertiesResourceCache"); 62 63 66 protected static UtilCache urlCache = new UtilCache("properties.UtilPropertiesUrlCache"); 67 68 71 protected static UtilCache bundleLocaleCache = new UtilCache("properties.UtilPropertiesBundleLocaleCache"); 72 73 74 80 public static boolean propertyValueEquals(String resource, String name, String compareString) { 81 String value = getPropertyValue(resource, name); 82 83 if (value == null) return false; 84 return value.trim().equals(compareString); 85 } 86 87 93 public static boolean propertyValueEqualsIgnoreCase(String resource, String name, String compareString) { 94 String value = getPropertyValue(resource, name); 95 96 if (value == null) return false; 97 return value.trim().equalsIgnoreCase(compareString); 98 } 99 100 107 public static String getPropertyValue(String resource, String name, String defaultValue) { 108 String value = getPropertyValue(resource, name); 109 110 if (value == null || value.length() == 0) 111 return defaultValue; 112 else 113 return value; 114 } 115 116 public static double getPropertyNumber(String resource, String name) { 117 String str = getPropertyValue(resource, name); 118 double strValue = 0.00000; 119 120 try { 121 strValue = Double.parseDouble(str); 122 } catch (NumberFormatException nfe) {} 123 return strValue; 124 } 125 126 131 public static String getPropertyValue(String resource, String name) { 132 if (resource == null || resource.length() <= 0) return ""; 133 if (name == null || name.length() <= 0) return ""; 134 FlexibleProperties properties = (FlexibleProperties) resourceCache.get(resource); 135 136 if (properties == null) { 137 try { 138 URL url = UtilURL.fromResource(resource); 139 140 if (url == null) return ""; 141 properties = FlexibleProperties.makeFlexibleProperties(url); 142 resourceCache.put(resource, properties); 143 } catch (MissingResourceException e) { 144 Debug.log(e.getMessage(), module); 145 } 146 } 147 if (properties == null) { 148 Debug.log("[UtilProperties.getPropertyValue] could not find resource: " + resource, module); 149 return ""; 150 } 151 152 String value = null; 153 154 try { 155 value = properties.getProperty(name); 156 } catch (Exception e) { 157 Debug.log(e.getMessage(), module); 158 } 159 return value == null ? "" : value.trim(); 160 } 161 162 166 public static Properties getProperties(String resource) { 167 if (resource == null || resource.length() <= 0) 168 return null; 169 Properties properties = (FlexibleProperties) resourceCache.get(resource); 170 171 if (properties == null) { 172 try { 173 URL url = UtilURL.fromResource(resource); 174 175 if (url == null) 176 return null; 177 properties = FlexibleProperties.makeFlexibleProperties(url); 178 resourceCache.put(resource, properties); 179 } catch (MissingResourceException e) { 180 Debug.log(e.getMessage(), module); 181 } 182 } 183 if (properties == null) { 184 Debug.log("[UtilProperties.getProperties] could not find resource: " + resource, module); 185 return null; 186 } 187 return properties; 188 } 189 190 194 public static Properties getProperties(URL url) { 195 if (url == null) 196 return null; 197 Properties properties = (FlexibleProperties) resourceCache.get(url); 198 199 if (properties == null) { 200 try { 201 properties = FlexibleProperties.makeFlexibleProperties(url); 202 resourceCache.put(url, properties); 203 } catch (MissingResourceException e) { 204 Debug.log(e.getMessage(), module); 205 } 206 } 207 if (properties == null) { 208 Debug.log("[UtilProperties.getProperties] could not find resource: " + url, module); 209 return null; 210 } 211 return properties; 212 } 213 214 215 217 223 public static boolean propertyValueEquals(URL url, String name, String compareString) { 224 String value = getPropertyValue(url, name); 225 226 if (value == null) return false; 227 return value.trim().equals(compareString); 228 } 229 230 236 public static boolean propertyValueEqualsIgnoreCase(URL url, String name, String compareString) { 237 String value = getPropertyValue(url, name); 238 239 if (value == null) return false; 240 return value.trim().equalsIgnoreCase(compareString); 241 } 242 243 250 public static String getPropertyValue(URL url, String name, String defaultValue) { 251 String value = getPropertyValue(url, name); 252 253 if (value == null || value.length() <= 0) 254 return defaultValue; 255 else 256 return value; 257 } 258 259 public static double getPropertyNumber(URL url, String name) { 260 String str = getPropertyValue(url, name); 261 double strValue = 0.00000; 262 263 try { 264 strValue = Double.parseDouble(str); 265 } catch (NumberFormatException nfe) {} 266 return strValue; 267 } 268 269 274 public static String getPropertyValue(URL url, String name) { 275 if (url == null) return ""; 276 if (name == null || name.length() <= 0) return ""; 277 FlexibleProperties properties = (FlexibleProperties) urlCache.get(url); 278 279 if (properties == null) { 280 try { 281 properties = FlexibleProperties.makeFlexibleProperties(url); 282 urlCache.put(url, properties); 283 } catch (MissingResourceException e) { 284 Debug.log(e.getMessage(), module); 285 } 286 } 287 if (properties == null) { 288 Debug.log("[UtilProperties.getPropertyValue] could not find resource: " + url, module); 289 return null; 290 } 291 292 String value = null; 293 294 try { 295 value = properties.getProperty(name); 296 } catch (Exception e) { 297 Debug.log(e.getMessage(), module); 298 } 299 return value == null ? "" : value.trim(); 300 } 301 302 310 public static String getSplitPropertyValue(URL url, String name) { 311 if (url == null) return ""; 312 if (name == null || name.length() <= 0) return ""; 313 314 FlexibleProperties properties = (FlexibleProperties) urlCache.get(url); 315 316 if (properties == null) { 317 try { 318 properties = FlexibleProperties.makeFlexibleProperties(url); 319 urlCache.put(url, properties); 320 } catch (MissingResourceException e) { 321 Debug.log(e.getMessage(), module); 322 } 323 } 324 if (properties == null) { 325 Debug.log("[UtilProperties.getPropertyValue] could not find resource: " + url, module); 326 return null; 327 } 328 329 String value = null; 330 331 try { 332 int curIdx = 1; 333 String curName = null; 334 335 while ((curName = properties.getProperty("name." + curIdx)) != null) { 336 if (name.equals(curName)) { 337 value = properties.getProperty("value." + curIdx); 338 break; 339 } 340 curIdx++; 341 } 342 } catch (Exception e) { 343 Debug.log(e.getMessage(), module); 344 } 345 return value == null ? "" : value.trim(); 346 } 347 348 352 public static void setPropertyValue(String resource, String name, String value) { 353 if (resource == null || resource.length() <= 0) return; 354 if (name == null || name.length() <= 0) return; 355 FlexibleProperties properties = (FlexibleProperties) resourceCache.get(resource); 356 357 if (properties == null) { 358 try { 359 URL url = UtilURL.fromResource(resource); 360 361 if (url == null) return; 362 properties = FlexibleProperties.makeFlexibleProperties(url); 363 resourceCache.put(resource, properties); 364 } catch (MissingResourceException e) { 365 Debug.log(e.getMessage(), module); 366 } 367 } 368 369 if (properties == null) { 370 Debug.log("[UtilProperties.setPropertyValue] could not find resource: " + resource, module); 371 return; 372 } 373 374 try { 375 properties.setProperty(name, value); 376 FileOutputStream propFile = new FileOutputStream (resource); 377 properties.store(propFile, "Dynamically modified by OFBiz Framework (org.ofbiz.base.util : UtilProperties.setPropertyValue) "); 378 propFile.close(); 379 } catch (FileNotFoundException e) { 380 Debug.log(e, "Unable to located the resource file.", module); 381 } catch (IOException e) { 382 Debug.logError(e, module); 383 } 384 } 385 386 388 400 public static String getMessage(String resource, String name, Locale locale) { 401 if (resource == null || resource.length() <= 0) return ""; 402 if (name == null || name.length() <= 0) return ""; 403 404 Map bundle = getResourceBundleMap(resource, locale); 405 406 if (bundle == null) return ""; 407 408 String value = null; 409 try { 410 value = (String )bundle.get(name); 411 } catch (Exception e) { 412 Debug.log(e.getMessage(), module); 413 } 414 return value == null ? "" : value.trim(); 415 } 416 417 425 public static String getMessage(String resource, String name, Object [] arguments, Locale locale) { 426 String value = getMessage(resource, name, locale); 427 428 if (value == null || value.length() == 0) { 429 return ""; 430 } else { 431 if (arguments != null && arguments.length > 0) { 432 value = MessageFormat.format(value, arguments); 433 } 434 return value; 435 } 436 } 437 438 446 public static String getMessage(String resource, String name, List arguments, Locale locale) { 447 String value = getMessage(resource, name, locale); 448 449 if (value == null || value.length() == 0) { 450 return ""; 451 } else { 452 if (arguments != null && arguments.size() > 0) { 453 value = MessageFormat.format(value, arguments.toArray()); 454 } 455 return value; 456 } 457 } 458 459 467 public static String getMessage(String resource, String name, Map context, Locale locale) { 468 String value = getMessage(resource, name, locale); 469 470 if (value == null || value.length() == 0) { 471 return ""; 472 } else { 473 if (context != null && context.size() > 0) { 474 value = FlexibleStringExpander.expandString(value, context); 475 } 476 return value; 477 } 478 } 479 480 485 public static ResourceBundle getResourceBundle(String resource, Locale locale) { 486 ResourceBundleMapWrapper.InternalRbmWrapper bundleMap = getInternalRbmWrapper(resource, locale); 487 if (bundleMap == null) { 488 return null; 489 } 490 ResourceBundle theBundle = bundleMap.getResourceBundle(); 491 return theBundle; 492 } 493 494 499 public static Map getResourceBundleMap(String resource, Locale locale) { 500 if (locale == null) { 501 throw new IllegalArgumentException ("Locale cannot be null"); 502 } 503 504 ResourceBundleMapWrapper.InternalRbmWrapper bundleMap = getInternalRbmWrapper(resource, locale); 505 return new ResourceBundleMapWrapper(bundleMap); 506 } 507 508 public static ResourceBundleMapWrapper.InternalRbmWrapper getInternalRbmWrapper(String resource, Locale locale) { 509 String resourceCacheKey = resource + "_" + locale.toString(); 510 ResourceBundleMapWrapper.InternalRbmWrapper bundleMap = (ResourceBundleMapWrapper.InternalRbmWrapper) bundleLocaleCache.get(resourceCacheKey); 511 if (bundleMap == null) { 512 synchronized (UtilProperties.class) { 513 bundleMap = (ResourceBundleMapWrapper.InternalRbmWrapper) bundleLocaleCache.get(resourceCacheKey); 514 if (bundleMap == null) { 515 ResourceBundle bundle = getBaseResourceBundle(resource, locale); 516 if (bundle == null) { 517 throw new IllegalArgumentException ("Could not find resource bundle [" + resource + "] in the locale [" + locale + "]"); 518 } 519 bundleMap = new ResourceBundleMapWrapper.InternalRbmWrapper(bundle); 520 if (bundleMap != null) { 521 bundleLocaleCache.put(resourceCacheKey, bundleMap); 522 } 523 } 524 } 525 } 526 return bundleMap; 527 } 528 529 protected static Set resourceNotFoundMessagesShown = FastSet.newInstance(); 530 protected static ResourceBundle getBaseResourceBundle(String resource, Locale locale) { 531 if (resource == null || resource.length() <= 0) return null; 532 if (locale == null) locale = Locale.getDefault(); 533 534 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 535 ResourceBundle bundle = null; 536 try { 537 bundle = ResourceBundle.getBundle(resource, locale, loader); 538 } catch (MissingResourceException e) { 539 String resourceFullName = resource + "_" + locale.toString(); 540 if (!resourceNotFoundMessagesShown.contains(resourceFullName)) { 541 resourceNotFoundMessagesShown.add(resourceFullName); 542 Debug.log("[UtilProperties.getPropertyValue] could not find resource: " + resource + " for locale " + locale.toString() + ": " + e.toString(), module); 543 return null; 544 } 545 } 546 if (bundle == null) { 547 String resourceFullName = resource + "_" + locale.toString(); 548 if (!resourceNotFoundMessagesShown.contains(resourceFullName)) { 549 resourceNotFoundMessagesShown.add(resourceFullName); 550 Debug.log("[UtilProperties.getPropertyValue] could not find resource: " + resource + " for locale " + locale.toString(), module); 551 return null; 552 } 553 } 554 555 return bundle; 556 } 557 558 595 public static Properties getProperties(String resource, Locale locale) { 596 if (resource == null || resource.length() <= 0) return null; 597 if (locale == null) locale = Locale.getDefault(); 598 599 String localeString = locale.toString(); 600 String resourceLocale = resource + "_" + localeString; 601 Properties properties = (FlexibleProperties) resourceCache.get(resourceLocale); 602 603 if (properties == null) { 604 try { 605 URL url = UtilURL.fromResource(resourceLocale); 606 if (url == null) { 607 properties = getProperties(resource); 608 } else { 609 properties = FlexibleProperties.makeFlexibleProperties(url); 610 } 611 } catch (MissingResourceException e) { 612 Debug.log(e.getMessage(), module); 613 } 614 resourceCache.put(resourceLocale, properties); 615 } 616 617 if (properties == null) 618 Debug.logInfo("[UtilProperties.getProperties] could not find resource: " + resource + ", locale: " + locale, module); 619 620 return properties; 621 } 622 } | Popular Tags |