[Arakhnę-Dev] [101] Add Resources utility class. |
[ Thread Index |
Date Index
| More arakhne.org/dev Archives
]
Revision: 101
Author: galland
Date: 2009-12-23 14:55:33 +0100 (Wed, 23 Dec 2009)
Log Message:
-----------
Add Resources utility class.
Modified Paths:
--------------
trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ClassLoaderFinder.java
trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/LibraryLoader.java
trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java
Added 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/ClassLoaderFinder.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ClassLoaderFinder.java 2009-12-23 09:32:29 UTC (rev 100)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ClassLoaderFinder.java 2009-12-23 13:55:33 UTC (rev 101)
@@ -42,7 +42,7 @@
* It tries to find the preferred class loader.
* If none was found, the default class loader will be replied.
*
- * @return the class loader
+ * @return the class loader, never <code>null</code>
*/
public static ClassLoader findClassLoader() {
Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/LibraryLoader.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/LibraryLoader.java 2009-12-23 09:32:29 UTC (rev 100)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/LibraryLoader.java 2009-12-23 13:55:33 UTC (rev 101)
@@ -137,7 +137,7 @@
private static URL findLibraryURL(String path, String libName, String platform, String arch) {
ClassLoader cl = ClassLoaderFinder.findClassLoader();
- if (cl==null) cl = LibraryLoader.class.getClassLoader();
+ assert(cl!=null);
String resourcePath = path;
if (resourcePath==null) resourcePath = ""; //$NON-NLS-1$
else if ((resourcePath.length()>0)&&(!resourcePath.endsWith("/"))) { //$NON-NLS-1$
@@ -159,9 +159,9 @@
if (arch!=null) buf.append(arch);
realLibName = System.mapLibraryName(buf.toString());
}
- URL libRes = cl.getResource(resourcePath+realLibName);
+ URL libRes = Resources.getResource(cl, resourcePath+realLibName);
if (libRes!=null) return libRes;
- return cl.getResource(realLibName);
+ return Resources.getResource(cl,realLibName);
}
/**
Added: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/Resources.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/Resources.java (rev 0)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/Resources.java 2009-12-23 13:55:33 UTC (rev 101)
@@ -0,0 +1,152 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2004-2009 Stéphane GALLAND
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * This program is free software; you can redistribute it and/or modify
+ */
+
+package org.arakhne.vmutil;
+
+import java.io.InputStream;
+import java.net.URL;
+
+/**
+ * This utility class provides to load resources according to
+ * several heuristics:<ul>
+ * <li>search the resource in class paths;</li>
+ * <li>search the resource in ./resources subdirectory in class paths.</li>
+ * </ul>
+ *
+ * @author Stéphane GALLAND <galland@xxxxxxxxxxx>
+ * @version $Name$ $Revision$ $Date$
+ * @since since JDK 1.5
+ */
+public class Resources {
+
+ /**
+ * Replies the URL of a resource.
+ * <p>
+ * You may use Unix-like syntax to write the resource path, ie.
+ * you may use slashs to separate filenames, and may not start the
+ * path with a slash.
+ *
+ * @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);
+ }
+
+ /**
+ * Replies the URL of a resource.
+ * <p>
+ * You may use Unix-like syntax to write the resource path, ie.
+ * you may use slashs to separate filenames, and may not start the
+ * path with a slash.
+ *
+ * @param clazz is the class which is restricting the scope of the search.
+ * @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(Class<?> clazz, String path) {
+ return getResource(clazz.getClassLoader(), path);
+ }
+
+ /**
+ * Replies the URL of a resource.
+ * <p>
+ * You may use Unix-like syntax to write the resource path, ie.
+ * you may use slashs to separate filenames, and may not start the
+ * path with a slash.
+ *
+ * @param classLoader is the research scope.
+ * @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(ClassLoader classLoader, String path) {
+ String resourcePath = path;
+ if (path.startsWith("/")) { //$NON-NLS-1$
+ resourcePath = path.substring(1);
+ }
+ URL url = classLoader.getResource(resourcePath);
+ if (url==null) {
+ // Try to find in ./resources sub directory
+ url = classLoader.getResource("resources/"+resourcePath); //$NON-NLS-1$
+ }
+ return url;
+ }
+
+ /**
+ * Replies the input stream of a resource.
+ * <p>
+ * You may use Unix-like syntax to write the resource path, ie.
+ * you may use slashs to separate filenames, and may not start the
+ * path with a slash.
+ *
+ * @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);
+ }
+
+ /**
+ * Replies the input stream of a resource.
+ * <p>
+ * You may use Unix-like syntax to write the resource path, ie.
+ * you may use slashs to separate filenames, and may not start the
+ * path with a slash.
+ *
+ * @param clazz is the class which is restricting the scope of the search.
+ * @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(Class<?> clazz, String path) {
+ return getResourceAsStream(clazz.getClassLoader(), path);
+ }
+
+ /**
+ * Replies the input stream of a resource.
+ * <p>
+ * You may use Unix-like syntax to write the resource path, ie.
+ * you may use slashs to separate filenames, and may not start the
+ * path with a slash.
+ *
+ * @param classLoader is the research scope.
+ * @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(ClassLoader classLoader, String path) {
+ String resourcePath = path;
+ if (path.startsWith("/")) { //$NON-NLS-1$
+ resourcePath = path.substring(1);
+ }
+ InputStream is = classLoader.getResourceAsStream(resourcePath);
+ if (is==null) {
+ // Try to find in ./resources sub directory
+ is = classLoader.getResourceAsStream("resources/"+resourcePath); //$NON-NLS-1$
+ }
+ return is;
+ }
+
+}
Modified: trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java
===================================================================
--- trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java 2009-12-23 09:32:29 UTC (rev 100)
+++ trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java 2009-12-23 13:55:33 UTC (rev 101)
@@ -506,7 +506,7 @@
assertEquals(new URL("jar:file:/home/test/j.jar!/org/arakhne/vmutil/ff.properties"), //$NON-NLS-1$
FileSystem.convertStringToUrl("jar:file:/home/test/j.jar!/org/arakhne/vmutil/ff.properties", false)); //$NON-NLS-1$
- URL testResource = FileSystemTest.class.getResource("/org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+ URL testResource = Resources.getResource("/org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
assertNotNull(testResource);
URL testResourceFileRel = new File("org/arakhne/vmutil/test.txt").toURI().toURL(); //$NON-NLS-1$
URL testResourceFileAbs = new File("/org/arakhne/vmutil/test.txt").toURI().toURL(); //$NON-NLS-1$
Added: trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/ResourcesTest.java
===================================================================
--- trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/ResourcesTest.java (rev 0)
+++ trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/ResourcesTest.java 2009-12-23 13:55:33 UTC (rev 101)
@@ -0,0 +1,99 @@
+/* $Id$
+ *
+ * Copyright (C) 2007-09 Stéphane GALLAND
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * This program is free software; you can redistribute it and/or modify
+ */
+package org.arakhne.vmutil;
+
+import java.io.InputStream;
+import java.net.URL;
+
+import junit.framework.TestCase;
+
+/**
+* @author Stéphane GALLAND <galland@xxxxxxxxxxx>
+* @version $Name$ $Revision$ $Date$
+*/
+public class ResourcesTest extends TestCase {
+
+ /**
+ */
+ public void testGetResourceString() {
+ URL u1 = Resources.getResource("/org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+ assertNotNull(u1);
+
+ URL u2 = Resources.getResource("org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+ assertNotNull(u2);
+
+ assertEquals(u1,u2);
+ }
+
+ /**
+ */
+ public void testGetResourceClassString() {
+ URL u1 = Resources.getResource(ResourcesTest.class, "/org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+ assertNotNull(u1);
+
+ URL u2 = Resources.getResource(ResourcesTest.class, "org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+ assertNotNull(u2);
+
+ assertEquals(u1,u2);
+ }
+
+ /**
+ */
+ public void testGetResourceClassLoaderString() {
+ URL u1 = Resources.getResource(ResourcesTest.class.getClassLoader(), "/org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+ assertNotNull(u1);
+
+ URL u2 = Resources.getResource(ResourcesTest.class.getClassLoader(), "org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+ assertNotNull(u2);
+
+ assertEquals(u1,u2);
+ }
+
+ /**
+ */
+ public void testGetResourceAsStreamString() {
+ InputStream is = Resources.getResourceAsStream("/org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+ assertNotNull(is);
+
+ is = Resources.getResourceAsStream("org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+ assertNotNull(is);
+ }
+
+ /**
+ */
+ public void testGetResourceAsStreamClassString() {
+ InputStream is = Resources.getResourceAsStream(ResourcesTest.class, "/org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+ assertNotNull(is);
+
+ is = Resources.getResourceAsStream(ResourcesTest.class, "org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+ assertNotNull(is);
+ }
+
+ /**
+ */
+ public void testGetResourceAsStreamClassLoaderString() {
+ InputStream is = Resources.getResourceAsStream(ResourcesTest.class.getClassLoader(), "/org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+ assertNotNull(is);
+
+ is = Resources.getResourceAsStream(ResourcesTest.class.getClassLoader(), "org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+ assertNotNull(is);
+ }
+
+}