[Arakhnę-Dev] [277] * Bug fix (from OSGi implementation): do not use the Package class loader when loading a resource.

[ Thread Index | Date Index | More arakhne.org/dev Archives ]


Revision: 277
Author:   galland
Date:     2011-08-29 14:36:11 +0200 (Mon, 29 Aug 2011)
Log Message:
-----------
* Bug fix (from OSGi implementation): do not use the Package class loader when loading a resource. A class loader must be specified additionally.

Modified Paths:
--------------
    trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/Resources.java
    trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/ResourcesTest.java

Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/Resources.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/Resources.java	2011-08-26 15:29:32 UTC (rev 276)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/Resources.java	2011-08-29 12:36:11 UTC (rev 277)
@@ -47,18 +47,26 @@
 	 */
 	public static final String NAME_SEPARATOR = "/"; //$NON-NLS-1$
 
+	/** Prefix (or directory name) where resources may be located.
+	 */
+	private static final String RESOURCE_PREFIX = "resources/"; //$NON-NLS-1$
+
 	/**
      * Replies the URL of a resource.
      * <p>
      * You may use Unix-like syntax to write the resource path, ie.
      * you may use slashes to separate filenames.
+     * <p>
+     * The class loader replied by {@link ClassLoaderFinder} is used.
+     * If it is <code>null</code>, the class loader of
+     * the Resources class is used.
      *
      * @param path is the absolute path of the resource. 
      * @return the url of the resource or <code>null</code> if the resource was
      * not found in class paths.
      */
     public static URL getResource(String path) {
-    	return getResource(ClassLoaderFinder.findClassLoader(), path);
+    	return getResource((ClassLoader)null, path);
     }
 
    	/**
@@ -73,14 +81,21 @@
      * Resources.getResources(Package.getPackage("org.arakhne.afc"), "/a/b/c/d.png");
      * Resources.getResources("org/arakhne/afc/a/b/c/d.png");
      * </code></pre>
+     * <p>
+     * If the <var>classLoader</var> parameter is <code>null</code>,
+     * the class loader replied by {@link ClassLoaderFinder} is used.
+     * If this last is <code>null</code>, the class loader of
+     * the Resources class is used.
      *
+     * @param classLoader is the research scope. If <code>null</code>,
+     * the class loader replied by {@link ClassLoaderFinder} is used.
      * @param packagename is the package in which the resource should be located.
-     * @param path is the absolute path of the resource. 
+     * @param path is the relative path of the resource in the package. 
      * @return the url of the resource or <code>null</code> if the resource was
      * not found in class paths.
      * @since 6.2
      */
-    public static URL getResource(Package packagename, String path) {
+    public static URL getResource(ClassLoader classLoader, Package packagename, String path) {
     	if (packagename==null || path==null) return null;
     	StringBuffer b = new StringBuffer();
     	b.append(packagename.getName().replaceAll(
@@ -105,6 +120,11 @@
      * Resources.getResources(Resources.class, "/a/b/c/d.png");
      * Resources.getResources("org/arakhne/vmutil/a/b/c/d.png");
      * </code></pre>
+     * <p>
+     * The class loader of the given class is used. If it is <code>null</code>,
+     * the class loader replied by {@link ClassLoaderFinder} is used.
+     * If it is also <code>null</code>, the class loader of this Resources
+     * class is used.
      *
      * @param classname is located in the package in which the resource should be also located.
      * @param path is the absolute path of the resource. 
@@ -113,7 +133,7 @@
      */
     public static URL getResource(Class<?> classname, String path) {
     	if (classname==null) return null;
-    	URL u = getResource(classname.getPackage(), path);
+    	URL u = getResource(classname.getClassLoader(), classname.getPackage(), path);
     	if (u==null)
     		u = getResource(classname.getClassLoader(), path);
     	return u;
@@ -124,8 +144,14 @@
      * <p>
      * You may use Unix-like syntax to write the resource path, ie.
      * you may use slashes to separate filenames.
+     * <p>
+     * If the <var>classLoader</var> parameter is <code>null</code>,
+     * the class loader replied by {@link ClassLoaderFinder} is used.
+     * If this last is <code>null</code>, the class loader of
+     * the Resources class is used.
      *
-     * @param classLoader is the research scope.
+     * @param classLoader is the research scope. If <code>null</code>,
+     * the class loader replied by {@link ClassLoaderFinder} is used.
      * @param path is the absolute path of the resource. 
      * @return the url of the resource or <code>null</code> if the resource was
      * not found in class paths.
@@ -146,7 +172,10 @@
     	
     	if (url==null) {
     		// Try to find in ./resources sub directory
-    		url = loader.getResource("resources/"+resourcePath); //$NON-NLS-1$
+    		StringBuffer b = new StringBuffer();
+    		b.append(RESOURCE_PREFIX);
+    		b.append(resourcePath);
+    		url = loader.getResource(b.toString());
     	}
     	return url;
     }
@@ -157,13 +186,17 @@
      * You may use Unix-like syntax to write the resource path, ie.
      * you may use slashes to separate filenames, and may not start the
      * path with a slash.
+     * <p>
+     * The class loader replied by {@link ClassLoaderFinder} is used.
+     * If it is <code>null</code>, the class loader of
+     * the Resources class is used.
      *
      * @param path is the absolute path of the resource. 
      * @return the url of the resource or <code>null</code> if the resource was
      * not found in class paths.
      */
     public static InputStream getResourceAsStream(String path) {
-    	return getResourceAsStream(ClassLoaderFinder.findClassLoader(), path);
+    	return getResourceAsStream((ClassLoader)null, path);
     }
     
    	/**
@@ -178,14 +211,21 @@
      * Resources.getResources(Package.getPackage("org.arakhne.afc"), "/a/b/c/d.png");
      * Resources.getResources("org/arakhne/afc/a/b/c/d.png");
      * </code></pre>
+     * <p>
+     * If the <var>classLoader</var> parameter is <code>null</code>,
+     * the class loader replied by {@link ClassLoaderFinder} is used.
+     * If this last is <code>null</code>, the class loader of
+     * the Resources class is used.
      *
+     * @param classLoader is the research scope. If <code>null</code>,
+     * the class loader replied by {@link ClassLoaderFinder} is used.
      * @param packagename is the package in which the resource should be located.
-     * @param path is the absolute path of the resource. 
+     * @param path is the relative path of the resource in the package. 
      * @return the url of the resource or <code>null</code> if the resource was
      * not found in class paths.
      * @since 6.2
      */
-    public static InputStream getResourceAsStream(Package packagename, String path) {
+    public static InputStream getResourceAsStream(ClassLoader classLoader, Package packagename, String path) {
     	if (packagename==null || path==null) return null;
     	StringBuffer b = new StringBuffer();
     	b.append(packagename.getName().replaceAll(
@@ -195,7 +235,7 @@
     		b.append(NAME_SEPARATOR);
     	}
     	b.append(path);
-    	return getResourceAsStream(packagename.getClass().getClassLoader(), b.toString());
+    	return getResourceAsStream(classLoader, b.toString());
     }
 
    	/**
@@ -210,6 +250,11 @@
      * Resources.getResources(Resources.class, "/a/b/c/d.png");
      * Resources.getResources("org/arakhne/vmutil/a/b/c/d.png");
      * </code></pre>
+     * <p>
+     * The class loader of the given class is used. If it is <code>null</code>,
+     * the class loader replied by {@link ClassLoaderFinder} is used.
+     * If it is also <code>null</code>, the class loader of this Resources
+     * class is used.
      *
      * @param classname is located in the package in which the resource should be also located.
      * @param path is the absolute path of the resource. 
@@ -218,7 +263,7 @@
      */
     public static InputStream getResourceAsStream(Class<?> classname, String path) {
     	if (classname==null) return null;
-    	InputStream is = getResourceAsStream(classname.getPackage(), path);
+    	InputStream is = getResourceAsStream(classname.getClassLoader(), classname.getPackage(), path);
     	if (is==null)
     		is = getResourceAsStream(classname.getClassLoader(), path);
     	return is;
@@ -230,8 +275,14 @@
      * You may use Unix-like syntax to write the resource path, ie.
      * you may use slashes to separate filenames, and may not start the
      * path with a slash.
+     * <p>
+     * If the <var>classLoader</var> parameter is <code>null</code>,
+     * the class loader replied by {@link ClassLoaderFinder} is used.
+     * If this last is <code>null</code>, the class loader of
+     * the Resources class is used.
      *
-     * @param classLoader is the research scope.
+     * @param classLoader is the research scope. If <code>null</code>,
+     * the class loader replied by {@link ClassLoaderFinder} is used.
      * @param path is the absolute path of the resource. 
      * @return the url of the resource or <code>null</code> if the resource was
      * not found in class paths.
@@ -242,14 +293,22 @@
     	if (path.startsWith("/")) { //$NON-NLS-1$
     		resourcePath = path.substring(1);
     	}
-    	ClassLoader loader = (classLoader==null)
-    						? Resources.class.getClassLoader()
-    						: classLoader;
+    	ClassLoader loader = classLoader;
+    	if (loader==null) {
+    		loader = ClassLoaderFinder.findClassLoader();
+    	}
+    	if (loader==null) {
+    		loader = Resources.class.getClassLoader();
+    	}
+    	
     	assert(loader!=null);
     	InputStream is = loader.getResourceAsStream(resourcePath);
     	if (is==null) {
     		// Try to find in ./resources sub directory
-    		is = loader.getResourceAsStream("resources/"+resourcePath); //$NON-NLS-1$
+    		StringBuffer b = new StringBuffer();
+    		b.append(RESOURCE_PREFIX);
+    		b.append(resourcePath);
+    		is = loader.getResourceAsStream(b.toString());
     	}
     	return is;
     }

Modified: trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/ResourcesTest.java
===================================================================
--- trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/ResourcesTest.java	2011-08-26 15:29:32 UTC (rev 276)
+++ trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/ResourcesTest.java	2011-08-29 12:36:11 UTC (rev 277)
@@ -73,29 +73,30 @@
    	/**
      */
     public void testGetResourcePackageString() {
+    	ClassLoader l = ResourcesTest.class.getClassLoader();
     	Package p = Package.getPackage("org.arakhne.vmutil"); //$NON-NLS-1$
     	assertNull(Resources.getResource(ResourcesTest.class, null));
     	
-    	URL u1 = Resources.getResource(p, "/org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+    	URL u1 = Resources.getResource(l, p, "/org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
     	assertNull(u1);
     	
-    	URL u2 = Resources.getResource(p, "org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+    	URL u2 = Resources.getResource(l, p, "org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
     	assertNull(u2);
     	
-    	URL u3 = Resources.getResource(p, "test.txt"); //$NON-NLS-1$
+    	URL u3 = Resources.getResource(l, p, "test.txt"); //$NON-NLS-1$
     	assertNotNull(u3);
 
-    	URL u4 = Resources.getResource(p, "/test.txt"); //$NON-NLS-1$
+    	URL u4 = Resources.getResource(l, p, "/test.txt"); //$NON-NLS-1$
     	assertNotNull(u4);
     	
     	assertEquals(u3, u4);
 
-    	assertNull(Resources.getResource((Package)null, null));
+    	assertNull(Resources.getResource(l, (Package)null, null));
     	
-    	u1 = Resources.getResource((Package)null, "test.txt"); //$NON-NLS-1$
+    	u1 = Resources.getResource(l, (Package)null, "test.txt"); //$NON-NLS-1$
     	assertNull(u1);
     	
-    	u2 = Resources.getResource((Package)null, "/test.txt"); //$NON-NLS-1$
+    	u2 = Resources.getResource(l, (Package)null, "/test.txt"); //$NON-NLS-1$
     	assertNull(u2);
     }
 
@@ -167,30 +168,31 @@
    	/**
      */
     public void testGetResourceAsStreamPackageString() {
+    	ClassLoader l = ResourcesTest.class.getClassLoader();
     	Package p = Package.getPackage("org.arakhne.vmutil"); //$NON-NLS-1$
     	assertNull(Resources.getResourceAsStream(ResourcesTest.class, null));
 
-    	InputStream is = Resources.getResourceAsStream(p, "/org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+    	InputStream is = Resources.getResourceAsStream(l, p, "/org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
     	assertNull(is);
     	
-    	is = Resources.getResourceAsStream(p, "org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+    	is = Resources.getResourceAsStream(l, p, "org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
     	assertNull(is);
 
-    	is = Resources.getResourceAsStream(p, "/test.txt"); //$NON-NLS-1$
+    	is = Resources.getResourceAsStream(l, p, "/test.txt"); //$NON-NLS-1$
     	assertNotNull(is);
 
-    	is = Resources.getResourceAsStream(p, "test.txt"); //$NON-NLS-1$
+    	is = Resources.getResourceAsStream(l, p, "test.txt"); //$NON-NLS-1$
     	assertNotNull(is);
 
-    	assertNull(Resources.getResourceAsStream((Package)null, null));
+    	assertNull(Resources.getResourceAsStream(l, (Package)null, null));
 
-    	is = Resources.getResourceAsStream((Package)null, "/org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+    	is = Resources.getResourceAsStream(l, (Package)null, "/org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
     	assertNull(is);
     	
-    	is = Resources.getResourceAsStream((Package)null, "org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+    	is = Resources.getResourceAsStream(l, (Package)null, "org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
     	assertNull(is);
 
-    	is = Resources.getResourceAsStream((Package)null, "test.txt"); //$NON-NLS-1$
+    	is = Resources.getResourceAsStream(l, (Package)null, "test.txt"); //$NON-NLS-1$
     	assertNull(is);
     }
 


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/