svn commit: r1649979 - in /ofbiz/trunk/framework/base/src/org/ofbiz/base/util: UtilProperties.java UtilURL.java

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

svn commit: r1649979 - in /ofbiz/trunk/framework/base/src/org/ofbiz/base/util: UtilProperties.java UtilURL.java

adrianc
Author: adrianc
Date: Wed Jan  7 00:24:28 2015
New Revision: 1649979

URL: http://svn.apache.org/r1649979
Log:
Some optimizations for URL lookups.

Modified:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilProperties.java
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilURL.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=1649979&r1=1649978&r2=1649979&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 Wed Jan  7 00:24:28 2015
@@ -897,31 +897,53 @@ public class UtilProperties implements S
         if (propertiesNotFound.contains(resourceName)) {
             return null;
         }
+        boolean containsProtocol = resource.contains(":");
+        ClassLoader loader = Thread.currentThread().getContextClassLoader();
         URL url = null;
         try {
             // Check for complete URL first
             if (resource.endsWith(".xml") || resource.endsWith(".properties")) {
-                url = FlexibleLocation.resolveLocation(resource);
+                if (containsProtocol) {
+                    url = FlexibleLocation.resolveLocation(resource, loader);
+                } else {
+                    url = UtilURL.fromResource(resource, loader);
+                }
                 if (url != null) {
                     return url;
                 }
             }
             // Check for *.properties file
-            url = FlexibleLocation.resolveLocation(resourceName + ".properties");
+            if (containsProtocol) {
+                url = FlexibleLocation.resolveLocation(resourceName + ".properties", loader);
+            } else {
+                url = UtilURL.fromResource(resourceName + ".properties", loader);
+            }
             if (url != null) {
                 return url;
             }
             // Check for Java XML properties file
-            url = FlexibleLocation.resolveLocation(resourceName + ".xml");
+            if (containsProtocol) {
+                url = FlexibleLocation.resolveLocation(resourceName + ".xml", loader);
+            } else {
+                url = UtilURL.fromResource(resourceName + ".xml", loader);
+            }
             if (url != null) {
                 return url;
             }
             // Check for Custom XML properties file
-            url = FlexibleLocation.resolveLocation(resource + ".xml");
+            if (containsProtocol) {
+                url = FlexibleLocation.resolveLocation(resource + ".xml", loader);
+            } else {
+                url = UtilURL.fromResource(resource + ".xml", loader);
+            }
             if (url != null) {
                 return url;
             }
-            url = FlexibleLocation.resolveLocation(resourceName);
+            if (containsProtocol) {
+                url = FlexibleLocation.resolveLocation(resource, loader);
+            } else {
+                url = UtilURL.fromResource(resource, loader);
+            }
             if (url != null) {
                 return url;
             }

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilURL.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilURL.java?rev=1649979&r1=1649978&r2=1649979&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilURL.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilURL.java Wed Jan  7 00:24:28 2015
@@ -21,6 +21,8 @@ package org.ofbiz.base.util;
 import java.io.File;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * URL Utilities - Simple Class for flexibly working with properties files
@@ -29,6 +31,7 @@ import java.net.URL;
 public class UtilURL {
 
     public static final String module = UtilURL.class.getName();
+    private static final Map<String, URL> urlMap = new ConcurrentHashMap<String, URL>();
 
     public static <C> URL fromClass(Class<C> contextClass) {
         String resourceName = contextClass.getName();
@@ -73,6 +76,14 @@ public class UtilURL {
      * @return
      */
     public static URL fromResource(String resourceName, ClassLoader loader) {
+        URL url = urlMap.get(resourceName);
+        if (url != null) {
+            try {
+                return new URL(url.toString());
+            } catch (MalformedURLException e) {
+                Debug.logWarning(e, "Exception thrown while copying URL: ", module);
+            }
+        }
         if (loader == null) {
             try {
                 loader = Thread.currentThread().getContextClassLoader();
@@ -82,39 +93,30 @@ public class UtilURL {
                 loader = utilURL.getClass().getClassLoader();
             }
         }
-        URL url = loader.getResource(resourceName);
+        url = loader.getResource(resourceName);
         if (url != null) {
+            urlMap.put(resourceName, url);
             return url;
         }
-        String propertiesResourceName = null;
-        /* Commenting this out for now. Calling code should check for this.
-        if (!resourceName.endsWith(".properties")) {
-            propertiesResourceName = resourceName.concat(".properties");
-            url = loader.getResource(propertiesResourceName);
-            if (url != null) {
-                return url;
-            }
-        }
-        */
         url = ClassLoader.getSystemResource(resourceName);
         if (url != null) {
+            urlMap.put(resourceName, url);
             return url;
         }
-        if (propertiesResourceName != null) {
-            url = ClassLoader.getSystemResource(propertiesResourceName);
-            if (url != null) {
-                return url;
-            }
-        }
         url = fromFilename(resourceName);
         if (url != null) {
+            urlMap.put(resourceName, url);
             return url;
         }
         url = fromOfbizHomePath(resourceName);
         if (url != null) {
+            urlMap.put(resourceName, url);
             return url;
         }
         url = fromUrlString(resourceName);
+        if (url != null) {
+            urlMap.put(resourceName, url);
+        }
         return url;
     }