Commits

Anonymous committed 32aa66d

Issue number: PS-41
Obtained from: http://jira.opensymphony.com/secure/ViewIssue.jspa?key=PS-41

Updated propertysets to use classloaderutils instead, and cleaned up the
code.

git-svn-id: http://svn.opensymphony.com/svn/propertyset/trunk@90bf3cbcdd-1c1a-0410-9a68-d6f521e3fa7b

Comments (0)

Files changed (2)

src/java/com/opensymphony/module/propertyset/PropertySetManager.java

 
 import com.opensymphony.module.propertyset.config.PropertySetConfig;
 
+import com.opensymphony.util.ClassLoaderUtil;
+
 import java.util.Map;
 
 
     //~ Methods ////////////////////////////////////////////////////////////////
 
     public static PropertySet getInstance(String name, Map args) {
-        PropertySet ps = getInstance(name, args, PropertySetManager.class.getClassLoader());
-
-        if (ps == null) {
-            ps = getInstance(name, args, Thread.currentThread().getContextClassLoader());
-        }
-
-        return ps;
-    }
-
-    public static PropertySet getInstance(String name, Map args, ClassLoader loader) {
         PropertySetConfig psc = PropertySetConfig.getConfig();
         String clazz = psc.getClassName(name);
         Map config = psc.getArgs(name);
         Class psClass = null;
 
         try {
-            psClass = loader.loadClass(clazz);
+            psClass = ClassLoaderUtil.loadClass(clazz, PropertySetManager.class);
         } catch (ClassNotFoundException ex) {
             return null;
         }

src/java/com/opensymphony/module/propertyset/config/PropertySetConfig.java

  */
 package com.opensymphony.module.propertyset.config;
 
+import com.opensymphony.util.ClassLoaderUtil;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
 import org.w3c.dom.*;
 
 import org.xml.sax.SAXException;
 
     private static PropertySetConfig config;
     private static final Object lock = new Object();
+    private static final Log log = LogFactory.getLog(PropertySetConfig.class);
+    private static final String[] CONFIG_LOCATIONS = new String[] {
+        "propertyset.xml", "/propertyset.xml", "META-INF/propertyset.xml",
+        "/META-INF/propertyset.xml", "META-INF/propertyset-default.xml",
+        "/META-INF/propertyset-default.xml"
+    };
 
     //~ Instance fields ////////////////////////////////////////////////////////
 
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
+        } finally {
+            //close the input stream
+            if (is != null) {
+                try {
+                    is.close();
+                } catch (IOException e) { /* ignore */
+                }
+            }
         }
 
         // get propertysets
         return (String) propertySets.get(name);
     }
 
-    private InputStream load() {
-        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+    /**
+     * Load the config from locations found in {@link #CONFIG_LOCATIONS}
+     *
+     * @return  An inputstream to load from
+     * @throws IllegalArgumentException     If none of the config files could be found.
+     */
+    private InputStream load() throws IllegalArgumentException {
         InputStream is = null;
 
-        try {
-            is = classLoader.getResourceAsStream("propertyset.xml");
-        } catch (Exception e) {
-        }
-
-        if (is == null) {
-            try {
-                is = classLoader.getResourceAsStream("/propertyset.xml");
-            } catch (Exception e) {
-            }
-        }
-
-        if (is == null) {
-            try {
-                is = classLoader.getResourceAsStream("META-INF/propertyset.xml");
-            } catch (Exception e) {
-            }
-        }
+        for (int i = 0; i < CONFIG_LOCATIONS.length; i++) {
+            String location = CONFIG_LOCATIONS[i];
 
-        if (is == null) {
             try {
-                is = classLoader.getResourceAsStream("/META-INF/propertyset.xml");
-            } catch (Exception e) {
-            }
-        }
+                is = ClassLoaderUtil.getResourceAsStream(location, this.getClass());
 
-        if (is == null) {
-            try {
-                is = classLoader.getResourceAsStream("META-INF/propertyset-default.xml");
+                //if we have found something then stop looking
+                if (is != null) {
+                    return is;
+                }
             } catch (Exception e) {
+                //do nothing.
             }
         }
 
         if (is == null) {
-            try {
-                is = classLoader.getResourceAsStream("/META-INF/propertyset-default.xml");
-            } catch (Exception e) {
-            }
+            String exceptionMessage = "Could not load propertyset config using '" + CONFIG_LOCATIONS + "'.  Please check your classpath.";
+            log.fatal(exceptionMessage);
+            throw new IllegalArgumentException(exceptionMessage);
         }
 
         return is;