KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > util > UtilProperties


1 /*
2  * $Id: UtilProperties.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.base.util;
25
26 import java.net.URL JavaDoc;
27 import java.text.MessageFormat JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Locale JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.MissingResourceException JavaDoc;
32 import java.util.Properties JavaDoc;
33 import java.util.ResourceBundle JavaDoc;
34 import java.util.Set JavaDoc;
35 import java.io.FileOutputStream JavaDoc;
36 import java.io.FileNotFoundException JavaDoc;
37 import java.io.IOException JavaDoc;
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 /**
47  * Generic Property Accessor with Cache - Utilities for working with properties files
48  *
49  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
50  * @version $Rev: 5462 $
51  * @since 1.0
52  */

53 public class UtilProperties implements java.io.Serializable JavaDoc {
54
55     public static final String JavaDoc module = UtilProperties.class.getName();
56
57     /** An instance of the generic cache for storing the FlexibleProperties
58      * corresponding to each properties file keyed by a String for the resource location.
59      * This will be used for both non-locale and locale keyed FexibleProperties instances.
60      */

61     protected static UtilCache resourceCache = new UtilCache("properties.UtilPropertiesResourceCache");
62
63     /** An instance of the generic cache for storing the FlexibleProperties
64      * corresponding to each properties file keyed by a URL object
65      */

66     protected static UtilCache urlCache = new UtilCache("properties.UtilPropertiesUrlCache");
67
68     /** An instance of the generic cache for storing the ResourceBundle
69      * corresponding to each properties file keyed by a String for the resource location and the locale
70      */

71     protected static UtilCache bundleLocaleCache = new UtilCache("properties.UtilPropertiesBundleLocaleCache");
72
73
74     /** Compares the specified property to the compareString, returns true if they are the same, false otherwise
75      * @param resource The name of the resource - if the properties file is 'webevent.properties', the resource name is 'webevent'
76      * @param name The name of the property in the properties file
77      * @param compareString The String to compare the property value to
78      * @return True if the strings are the same, false otherwise
79      */

80     public static boolean propertyValueEquals(String JavaDoc resource, String JavaDoc name, String JavaDoc compareString) {
81         String JavaDoc value = getPropertyValue(resource, name);
82
83         if (value == null) return false;
84         return value.trim().equals(compareString);
85     }
86
87     /** Compares Ignoring Case the specified property to the compareString, returns true if they are the same, false otherwise
88      * @param resource The name of the resource - if the properties file is 'webevent.properties', the resource name is 'webevent'
89      * @param name The name of the property in the properties file
90      * @param compareString The String to compare the property value to
91      * @return True if the strings are the same, false otherwise
92      */

93     public static boolean propertyValueEqualsIgnoreCase(String JavaDoc resource, String JavaDoc name, String JavaDoc compareString) {
94         String JavaDoc value = getPropertyValue(resource, name);
95
96         if (value == null) return false;
97         return value.trim().equalsIgnoreCase(compareString);
98     }
99
100     /** Returns the value of the specified property name from the specified resource/properties file.
101      * If the specified property name or properties file is not found, the defaultValue is returned.
102      * @param resource The name of the resource - if the properties file is 'webevent.properties', the resource name is 'webevent'
103      * @param name The name of the property in the properties file
104      * @param defaultValue The value to return if the property is not found
105      * @return The value of the property in the properties file, or if not found then the defaultValue
106      */

107     public static String JavaDoc getPropertyValue(String JavaDoc resource, String JavaDoc name, String JavaDoc defaultValue) {
108         String JavaDoc 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 JavaDoc resource, String JavaDoc name) {
117         String JavaDoc str = getPropertyValue(resource, name);
118         double strValue = 0.00000;
119
120         try {
121             strValue = Double.parseDouble(str);
122         } catch (NumberFormatException JavaDoc nfe) {}
123         return strValue;
124     }
125
126     /** Returns the value of the specified property name from the specified resource/properties file
127      * @param resource The name of the resource - can be a file, class, or URL
128      * @param name The name of the property in the properties file
129      * @return The value of the property in the properties file
130      */

131     public static String JavaDoc getPropertyValue(String JavaDoc resource, String JavaDoc 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 JavaDoc url = UtilURL.fromResource(resource);
139
140                 if (url == null) return "";
141                 properties = FlexibleProperties.makeFlexibleProperties(url);
142                 resourceCache.put(resource, properties);
143             } catch (MissingResourceException JavaDoc 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 JavaDoc value = null;
153
154         try {
155             value = properties.getProperty(name);
156         } catch (Exception JavaDoc e) {
157             Debug.log(e.getMessage(), module);
158         }
159         return value == null ? "" : value.trim();
160     }
161
162     /** Returns the specified resource/properties file
163      * @param resource The name of the resource - can be a file, class, or URL
164      * @return The properties file
165      */

166     public static Properties JavaDoc getProperties(String JavaDoc resource) {
167         if (resource == null || resource.length() <= 0)
168             return null;
169         Properties JavaDoc properties = (FlexibleProperties) resourceCache.get(resource);
170
171         if (properties == null) {
172             try {
173                 URL JavaDoc 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 JavaDoc 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     /** Returns the specified resource/properties file
191      * @param url The URL to the resource
192      * @return The properties file
193      */

194     public static Properties JavaDoc getProperties(URL JavaDoc url) {
195         if (url == null)
196             return null;
197         Properties JavaDoc 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 JavaDoc 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     // ========= URL Based Methods ==========
216

217     /** Compares the specified property to the compareString, returns true if they are the same, false otherwise
218      * @param url URL object specifying the location of the resource
219      * @param name The name of the property in the properties file
220      * @param compareString The String to compare the property value to
221      * @return True if the strings are the same, false otherwise
222      */

223     public static boolean propertyValueEquals(URL JavaDoc url, String JavaDoc name, String JavaDoc compareString) {
224         String JavaDoc value = getPropertyValue(url, name);
225
226         if (value == null) return false;
227         return value.trim().equals(compareString);
228     }
229
230     /** Compares Ignoring Case the specified property to the compareString, returns true if they are the same, false otherwise
231      * @param url URL object specifying the location of the resource
232      * @param name The name of the property in the properties file
233      * @param compareString The String to compare the property value to
234      * @return True if the strings are the same, false otherwise
235      */

236     public static boolean propertyValueEqualsIgnoreCase(URL JavaDoc url, String JavaDoc name, String JavaDoc compareString) {
237         String JavaDoc value = getPropertyValue(url, name);
238
239         if (value == null) return false;
240         return value.trim().equalsIgnoreCase(compareString);
241     }
242
243     /** Returns the value of the specified property name from the specified resource/properties file.
244      * If the specified property name or properties file is not found, the defaultValue is returned.
245      * @param url URL object specifying the location of the resource
246      * @param name The name of the property in the properties file
247      * @param defaultValue The value to return if the property is not found
248      * @return The value of the property in the properties file, or if not found then the defaultValue
249      */

250     public static String JavaDoc getPropertyValue(URL JavaDoc url, String JavaDoc name, String JavaDoc defaultValue) {
251         String JavaDoc 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 JavaDoc url, String JavaDoc name) {
260         String JavaDoc str = getPropertyValue(url, name);
261         double strValue = 0.00000;
262
263         try {
264             strValue = Double.parseDouble(str);
265         } catch (NumberFormatException JavaDoc nfe) {}
266         return strValue;
267     }
268
269     /** Returns the value of the specified property name from the specified resource/properties file
270      * @param url URL object specifying the location of the resource
271      * @param name The name of the property in the properties file
272      * @return The value of the property in the properties file
273      */

274     public static String JavaDoc getPropertyValue(URL JavaDoc url, String JavaDoc 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 JavaDoc 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 JavaDoc value = null;
293
294         try {
295             value = properties.getProperty(name);
296         } catch (Exception JavaDoc e) {
297             Debug.log(e.getMessage(), module);
298         }
299         return value == null ? "" : value.trim();
300     }
301
302     /** Returns the value of a split property name from the specified resource/properties file
303      * Rather than specifying the property name the value of a name.X property is specified which
304      * will correspond to a value.X property whose value will be returned. X is a number from 1 to
305      * whatever and all values are checked until a name.X for a certain X is not found.
306      * @param url URL object specifying the location of the resource
307      * @param name The name of the split property in the properties file
308      * @return The value of the split property from the properties file
309      */

310     public static String JavaDoc getSplitPropertyValue(URL JavaDoc url, String JavaDoc 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 JavaDoc 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 JavaDoc value = null;
330
331         try {
332             int curIdx = 1;
333             String JavaDoc 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 JavaDoc e) {
343             Debug.log(e.getMessage(), module);
344         }
345         return value == null ? "" : value.trim();
346     }
347     
348     /** Sets the specified value of the specified property name to the specified resource/properties file
349     * @param resource The name of the resource - must be a file
350     * @param name The name of the property in the properties file
351     * @param value The value of the property in the properties file */

352     public static void setPropertyValue(String JavaDoc resource, String JavaDoc name, String JavaDoc 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 JavaDoc url = UtilURL.fromResource(resource);
360
361                 if (url == null) return;
362                 properties = FlexibleProperties.makeFlexibleProperties(url);
363                 resourceCache.put(resource, properties);
364             } catch (MissingResourceException JavaDoc 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 JavaDoc propFile = new FileOutputStream JavaDoc(resource);
377             properties.store(propFile, "Dynamically modified by OFBiz Framework (org.ofbiz.base.util : UtilProperties.setPropertyValue) ");
378             propFile.close();
379         } catch (FileNotFoundException JavaDoc e) {
380             Debug.log(e, "Unable to located the resource file.", module);
381         } catch (IOException JavaDoc e) {
382             Debug.logError(e, module);
383         }
384     }
385
386     // ========= Locale & Resource Based Methods ==========
387

388     /** Returns the value of the specified property name from the specified resource/properties file corresponding to the given locale.
389      * <br/>
390      * <br/> Two reasons why we do not use the FlexibleProperties class for this:
391      * <ul>
392      * <li>Doesn't support flexible locale based naming: try fname_locale (5 letter), then fname_locale (2 letter lang only), then fname</li>
393      * <li>Does not support parent properties/bundles so that if the fname_locale5 file doesn't have it then fname_locale2 is tried, then the fname bundle</li>
394      * </ul>
395      * @param resource The name of the resource - can be a file, class, or URL
396      * @param name The name of the property in the properties file
397      * @param locale The locale that the given resource will correspond to
398      * @return The value of the property in the properties file
399      */

400     public static String JavaDoc getMessage(String JavaDoc resource, String JavaDoc name, Locale JavaDoc locale) {
401         if (resource == null || resource.length() <= 0) return "";
402         if (name == null || name.length() <= 0) return "";
403
404         Map JavaDoc bundle = getResourceBundleMap(resource, locale);
405
406         if (bundle == null) return "";
407
408         String JavaDoc value = null;
409         try {
410             value = (String JavaDoc)bundle.get(name);
411         } catch (Exception JavaDoc e) {
412             Debug.log(e.getMessage(), module);
413         }
414         return value == null ? "" : value.trim();
415     }
416
417     /** Returns the value of the specified property name from the specified resource/properties file corresponding
418      * to the given locale and replacing argument place holders with the given arguments using the MessageFormat class
419      * @param resource The name of the resource - can be a file, class, or URL
420      * @param name The name of the property in the properties file
421      * @param locale The locale that the given resource will correspond to
422      * @param arguments An array of Objects to insert into the message argument place holders
423      * @return The value of the property in the properties file
424      */

425     public static String JavaDoc getMessage(String JavaDoc resource, String JavaDoc name, Object JavaDoc[] arguments, Locale JavaDoc locale) {
426         String JavaDoc 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     /** Returns the value of the specified property name from the specified resource/properties file corresponding
439      * to the given locale and replacing argument place holders with the given arguments using the MessageFormat class
440      * @param resource The name of the resource - can be a file, class, or URL
441      * @param name The name of the property in the properties file
442      * @param locale The locale that the given resource will correspond to
443      * @param arguments A List of Objects to insert into the message argument place holders
444      * @return The value of the property in the properties file
445      */

446     public static String JavaDoc getMessage(String JavaDoc resource, String JavaDoc name, List JavaDoc arguments, Locale JavaDoc locale) {
447         String JavaDoc 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     /** Returns the value of the specified property name from the specified resource/properties file corresponding
460      * to the given locale and replacing argument place holders with the given arguments using the FlexibleStringExpander class
461      * @param resource The name of the resource - can be a file, class, or URL
462      * @param name The name of the property in the properties file
463      * @param locale The locale that the given resource will correspond to
464      * @param context A Map of Objects to insert into the message place holders using the ${} syntax of the FlexibleStringExpander
465      * @return The value of the property in the properties file
466      */

467     public static String JavaDoc getMessage(String JavaDoc resource, String JavaDoc name, Map JavaDoc context, Locale JavaDoc locale) {
468         String JavaDoc 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     /** Returns the specified resource/properties file as a ResourceBundle
481      * @param resource The name of the resource - can be a file, class, or URL
482      * @param locale The locale that the given resource will correspond to
483      * @return The ResourceBundle
484      */

485     public static ResourceBundle JavaDoc getResourceBundle(String JavaDoc resource, Locale JavaDoc locale) {
486         ResourceBundleMapWrapper.InternalRbmWrapper bundleMap = getInternalRbmWrapper(resource, locale);
487         if (bundleMap == null) {
488             return null;
489         }
490         ResourceBundle JavaDoc theBundle = bundleMap.getResourceBundle();
491         return theBundle;
492     }
493
494     /** Returns the specified resource/properties file as a Map with the original ResourceBundle in the Map under the key _RESOURCE_BUNDLE_
495      * @param resource The name of the resource - can be a file, class, or URL
496      * @param locale The locale that the given resource will correspond to
497      * @return Map containing all entries in The ResourceBundle
498      */

499     public static Map JavaDoc getResourceBundleMap(String JavaDoc resource, Locale JavaDoc locale) {
500         if (locale == null) {
501             throw new IllegalArgumentException JavaDoc("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 JavaDoc resource, Locale JavaDoc locale) {
509         String JavaDoc 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 JavaDoc bundle = getBaseResourceBundle(resource, locale);
516                     if (bundle == null) {
517                         throw new IllegalArgumentException JavaDoc("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 JavaDoc resourceNotFoundMessagesShown = FastSet.newInstance();
530     protected static ResourceBundle JavaDoc getBaseResourceBundle(String JavaDoc resource, Locale JavaDoc locale) {
531         if (resource == null || resource.length() <= 0) return null;
532         if (locale == null) locale = Locale.getDefault();
533
534         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
535         ResourceBundle JavaDoc bundle = null;
536         try {
537             bundle = ResourceBundle.getBundle(resource, locale, loader);
538         } catch (MissingResourceException JavaDoc e) {
539             String JavaDoc 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 JavaDoc 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     /** Returns the specified resource/properties file
559      *
560      * NOTE: This is NOT fully implemented yet to fulfill all of the requirements
561      * for i18n messages. Do NOT use.
562      *
563      * To be used in an i18n context this still needs to be extended quite
564      * a bit. The behavior needed is that for each getMessage the most specific
565      * locale (with fname_en_US for instance) is searched first, then the next
566      * less specific (fname_en for instance), then without the locale if it is
567      * still not found (plain fname for example, not that these examples would
568      * have .properties appended to them).
569      * This would be accomplished by returning the following structure:
570      * 1. Get "fname" FlexibleProperties object
571      * 2. Get the "fname_en" FlexibleProperties object and if the "fname" one
572      * is not null, set it as the default/parent of the "fname_en" object
573      * 3. Get the "fname_en_US" FlexibleProperties object and if the
574      * "fname_en" one is not null, set it as the default/parent of the
575      * "fname_en_US" object; if the "fname_en" one is null, but the "fname"
576      * one is not, set the "fname" object as the default/parent of the
577      * "fname_en_US" object
578      * Then return the fname_en_US object if not null, else the fname_en, else the fname.
579      *
580      * To make this all more fun, the default locale should be the parent of
581      * the "fname" object in this example so that there is an even higher
582      * chance of finding something for each request.
583      *
584      * For efficiency all of these should be cached indendependently so the same
585      * instance can be shared, speeding up loading time/efficiency.
586      *
587      * All of this should work with the setDefaultProperties method of the
588      * FlexibleProperties class, but it should be tested and updated as
589      * necessary. It's a bit tricky, so chances are it won't work as desired...
590      *
591      * @param resource The name of the resource - can be a file, class, or URL
592      * @param locale The locale that the given resource will correspond to
593      * @return The Properties class
594      */

595     public static Properties JavaDoc getProperties(String JavaDoc resource, Locale JavaDoc locale) {
596         if (resource == null || resource.length() <= 0) return null;
597         if (locale == null) locale = Locale.getDefault();
598
599         String JavaDoc localeString = locale.toString();
600         String JavaDoc resourceLocale = resource + "_" + localeString;
601         Properties JavaDoc properties = (FlexibleProperties) resourceCache.get(resourceLocale);
602
603         if (properties == null) {
604             try {
605                 URL JavaDoc url = UtilURL.fromResource(resourceLocale);
606                 if (url == null) {
607                     properties = getProperties(resource);
608                 } else {
609                     properties = FlexibleProperties.makeFlexibleProperties(url);
610                 }
611             } catch (MissingResourceException JavaDoc 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