svn commit: r1649931 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilProperties.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1649931 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilProperties.java

adrianc
Author: adrianc
Date: Tue Jan  6 20:18:43 2015
New Revision: 1649931

URL: http://svn.apache.org/r1649931
Log:
A new method for UtilProperties.java - to be used in upcoming changes.

Modified:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilProperties.java

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilProperties.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilProperties.java?rev=1649931&r1=1649930&r2=1649931&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilProperties.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilProperties.java Tue Jan  6 20:18:43 2015
@@ -281,6 +281,41 @@ public class UtilProperties implements S
         return value == null ? "" : value.trim();
     }
 
+    /**
+     * Returns a new <code>Properties</code> instance created from <code>fileName</code>.
+     * <p>This method is intended for low-level framework classes that need to read
+     * properties files before OFBiz has been fully initialized.</p>
+     *
+     * @param fileName The full name of the properties file ("foo.properties")
+     * @return A new <code>Properties</code> instance created from <code>fileName</code>
+     * @throws IllegalArgumentException if <code>fileName</code> is empty
+     * @throws IllegalStateException if there were any problems reading the file
+     */
+    public static Properties createProperties(String fileName) {
+        Assert.notEmpty("fileName", fileName);
+        InputStream inStream = null;
+        try {
+            URL url = Thread.currentThread().getContextClassLoader().getResource(fileName);
+            if (url == null) {
+                throw new IllegalStateException(fileName + " not found");
+            }
+            inStream = url.openStream();
+            Properties properties = new Properties();
+            properties.load(inStream);
+            return properties;
+        } catch (Exception e) {
+            throw new IllegalStateException("Exception thrown while reading debug.properties: " + e);
+        } finally {
+            if (inStream != null) {
+                try {
+                    inStream.close();
+                } catch (IOException e) {
+                    System.out.println("Exception thrown while closing InputStream: " + e);
+                }
+            }
+        }
+    }
+
     /** Returns the specified resource/properties file
      * @param resource The name of the resource - can be a file, class, or URL
      * @return The properties file
@@ -305,8 +340,7 @@ public class UtilProperties implements S
         Properties properties = urlCache.get(cacheKey);
         if (properties == null) {
             try {
-                properties = new Properties();
-                properties.load(url.openStream());
+                properties = new ExtendedProperties(url, null);
                 urlCache.put(cacheKey, properties);
             } catch (Exception e) {
                 Debug.logInfo(e, module);
@@ -915,7 +949,7 @@ public class UtilProperties implements S
      * &nbsp;&nbsp;...<br />
      * &nbsp;&lt;/property&gt;<br />
      * &nbsp;...<br />
-     * &lt;/resource&gt;<br /><br /></code> where <em>"locale 1", "locale 2"</em> are valid Locale strings.
+     * &lt;/resource&gt;<br /><br /></code> where <em>"locale 1", "locale 2"</em> are valid xml:lang values..
      * </p>
      *
      * @param in XML file InputStream
@@ -932,7 +966,7 @@ public class UtilProperties implements S
             doc = UtilXml.readXmlDocument(in, true, "XML Properties file");
             in.close();
         } catch (Exception e) {
-            Debug.logWarning(e, "XML Locale file for locale " + locale + " could not be loaded.", module);
+            Debug.logWarning(e, "XML file for locale " + locale + " could not be loaded.", module);
             in.close();
             return null;
         }