[Arakhnę-Dev] [179] * Add utility function to build a resource-based URL from a file or a file-based URL . |
[ Thread Index |
Date Index
| More arakhne.org/dev Archives
]
- To: dev@xxxxxxxxxxx
- Subject: [Arakhnę-Dev] [179] * Add utility function to build a resource-based URL from a file or a file-based URL .
- From: subversion@xxxxxxxxxxxxx
- Date: Tue, 07 Sep 2010 17:37:39 +0200
Revision: 179
Author: galland
Date: 2010-09-07 17:37:38 +0200 (Tue, 07 Sep 2010)
Log Message:
-----------
* Add utility function to build a resource-based URL from a file or a file-based URL.
Modified Paths:
--------------
trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ClasspathUtil.java
trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java
trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/ClasspathUtilTest.java
trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java
Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ClasspathUtil.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ClasspathUtil.java 2010-09-06 21:11:04 UTC (rev 178)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ClasspathUtil.java 2010-09-07 15:37:38 UTC (rev 179)
@@ -22,7 +22,10 @@
import java.io.File;
import java.net.URL;
+import java.net.URLClassLoader;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;
@@ -73,4 +76,51 @@
return startupClasspath;
}
+ /** Replies the current classpath.
+ *
+ * @return the current classpath, never <code>null</code>.
+ * @since 6.0
+ */
+ public static URL[] getCurrentClasspath() {
+ URL[] startupClasspath = getStartupClasspath();
+ List<URL> additionalUrls = new LinkedList<URL>(Arrays.asList(startupClasspath));
+
+ URL[] addurls;
+
+ ClassLoader loader = ClassLoaderFinder.findClassLoader();
+ if (loader instanceof DynamicURLClassLoader) {
+ DynamicURLClassLoader dLoader = (DynamicURLClassLoader)loader;
+ addurls = dLoader.getURLs();
+ }
+ else if (loader instanceof URLClassLoader) {
+ URLClassLoader dLoader = (URLClassLoader)loader;
+ addurls = dLoader.getURLs();
+ }
+ else {
+ return startupClasspath;
+ }
+
+ for(URL url : addurls) {
+ if (!contains(additionalUrls, url)) {
+ additionalUrls.add(0, url);
+ }
+ }
+
+ URL[] allUrls = new URL[additionalUrls.size()];
+ additionalUrls.toArray(allUrls);
+ return allUrls;
+ }
+
+ private static boolean contains(List<URL> list, URL url) {
+ String s1;
+ String s2 = url.toString();
+ s2 = s2.replaceFirst("/$", ""); //$NON-NLS-1$ //$NON-NLS-2$
+ for(URL u : list) {
+ s1 = u.toExternalForm().replaceFirst("/$", ""); //$NON-NLS-1$ //$NON-NLS-2$
+ if (s2.equals(s1))
+ return true;
+ }
+ return false;
+ }
+
}
\ No newline at end of file
Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java 2010-09-06 21:11:04 UTC (rev 178)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java 2010-09-07 15:37:38 UTC (rev 179)
@@ -47,6 +47,10 @@
*/
public class FileSystem {
+ static {
+ URLHandlerUtil.installArakhneHandlers();
+ }
+
/** Regular expression pattern which corresponds to Windows native filename.
*/
private static final String WINDOW_NATIVE_FILENAME_PATTERN = "^([A-Za-z]:)?([^\\\\/:*?\"<>|]*\\\\)*[^\\\\/:*?\"<>|]*$"; //$NON-NLS-1$
@@ -1884,6 +1888,43 @@
}
return null;
}
+
+ /** Replies an URL for the given file and translate it into a
+ * resource URL if the given file is inside the classpath.
+ *
+ * @param file is the filename to translate.
+ * @return the URL which is corresponding to file, or <code>null</code> if
+ * the url cannot be computed.
+ */
+ public static URL convertFileToURL(File file) {
+ if (file==null) return null;
+ URL url;
+ try {
+ url = file.toURI().toURL();
+ }
+ catch (MalformedURLException e) {
+ return null;
+ }
+ String s = url.toExternalForm().replaceAll("/$", ""); //$NON-NLS-1$//$NON-NLS-2$
+ String sp;
+ URL[] classpath = ClasspathUtil.getCurrentClasspath();
+
+ for(URL path : classpath) {
+ sp = path.toExternalForm().replaceAll("/$", ""); //$NON-NLS-1$//$NON-NLS-2$
+ if (s.startsWith(sp)) {
+ StringBuffer buffer = new StringBuffer("resource:"); //$NON-NLS-1$
+ buffer.append(s.substring(sp.length()).replaceAll("^/", "")); //$NON-NLS-1$//$NON-NLS-2$
+ try {
+ return new URL(buffer.toString());
+ }
+ catch (MalformedURLException e) {
+ //
+ }
+ }
+ }
+
+ return url;
+ }
}
Modified: trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/ClasspathUtilTest.java
===================================================================
--- trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/ClasspathUtilTest.java 2010-09-06 21:11:04 UTC (rev 178)
+++ trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/ClasspathUtilTest.java 2010-09-07 15:37:38 UTC (rev 179)
@@ -21,8 +21,12 @@
import java.io.File;
import java.net.URL;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
import java.util.regex.Pattern;
+import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
/**
@@ -66,4 +70,80 @@
}
}
+ /**
+ */
+ public void testGetCurrentClasspath_standardClassLoader() {
+ URL[] urls = ClasspathUtil.getCurrentClasspath();
+ assertNotNull(urls);
+
+ String[] paths = System.getProperty("java.class.path").split( //$NON-NLS-1$
+ Pattern.quote(File.pathSeparator));
+
+ assertEquals(paths.length, urls.length);
+
+ for(int i=0; i<paths.length; i++) {
+ URL u = FileSystem.convertStringToUrl(paths[i], true);
+ if (!isEquals(u, urls[i])) {
+ throw new AssertionFailedError("expected: "+u+"; actual: "+urls[i]); //$NON-NLS-1$//$NON-NLS-2$
+ }
+ }
+ }
+
+ /**
+ * @throws Exception
+ */
+ public void testGetCurrentClasspath_dynamicClassLoader() throws Exception {
+ URL addUrl = new File("/toto").toURI().toURL(); //$NON-NLS-1$
+ DynamicURLClassLoader myLoader = new DynamicURLClassLoader(
+ ClasspathUtilTest.class.getClassLoader(),
+ null);
+ ClassLoaderFinder.setPreferredClassLoader(myLoader);
+ myLoader.addURL(addUrl);
+ try {
+ URL[] urls = ClasspathUtil.getCurrentClasspath();
+ assertNotNull(urls);
+
+ List<URL> list = new LinkedList<URL>();
+ String[] paths = System.getProperty("java.class.path").split( //$NON-NLS-1$
+ Pattern.quote(File.pathSeparator));
+ for(String s : paths) {
+ list.add(FileSystem.convertStringToUrl(s, true));
+ }
+ list.add(addUrl);
+
+ assertEquals(paths.length+1, urls.length);
+
+ for(URL r : urls) {
+ Iterator<URL> iter = list.iterator();
+ URL u;
+ boolean found = false;
+ while (!found && iter.hasNext()) {
+ u = iter.next();
+ if (isEquals(u, r)) {
+ iter.remove();
+ found = true;
+ }
+ }
+ if (!found) {
+ throw new AssertionFailedError("additional path found: "+r); //$NON-NLS-1$
+ }
+ }
+
+ if (!list.isEmpty()) {
+ throw new AssertionFailedError("expected: "+list.toString()); //$NON-NLS-1$
+ }
+ }
+ finally {
+ ClassLoaderFinder.popPreferredClassLoader();
+ }
+ }
+
+ private boolean isEquals(URL expected, URL actual) {
+ String u1 = expected==null ? null : expected.toExternalForm().replaceFirst("/$", ""); //$NON-NLS-1$ //$NON-NLS-2$
+ String u2 = actual==null ? null : actual.toExternalForm().replaceFirst("/$", ""); //$NON-NLS-1$ //$NON-NLS-2$
+ if (u1==u2) return true;
+ if (u1==null || u2==null) return false;
+ return u1.equals(u2);
+ }
+
}
Modified: trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java
===================================================================
--- trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java 2010-09-06 21:11:04 UTC (rev 178)
+++ trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java 2010-09-07 15:37:38 UTC (rev 179)
@@ -94,7 +94,6 @@
@Override
public void setUp() throws Exception {
super.setUp();
- URLHandlerUtil.installArakhneHandlers();
// Disable native library loading during unit tests
this.oldLibraryLoaderState = LibraryLoader.isEnable();
LibraryLoader.setEnable(false);
@@ -107,7 +106,6 @@
public void tearDown() throws Exception {
// Restore library loading state
LibraryLoader.setEnable(this.oldLibraryLoaderState);
- URLHandlerUtil.uninstallArakhneHandlers();
super.tearDown();
}
@@ -1089,5 +1087,36 @@
assertEquals(new File("c.txt"), //$NON-NLS-1$
FileSystem.normalizeWindowsNativeFilename("file://c.txt")); //$NON-NLS-1$
}
+
+ /**
+ * @throws Exception
+ */
+ public void testConvertFileToURLFile() throws Exception {
+ URLHandlerUtil.installArakhneHandlers();
+ try {
+ File f1 = new File("/toto"); //$NON-NLS-1$
+ URL u1 = f1.toURI().toURL();
+ URL u2 = Resources.getResource("org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+ URL u2e = new URL("resource:org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+ File f2 = FileSystem.convertUrlToFile(u2);
+
+ URL actual;
+
+ actual = FileSystem.convertFileToURL(f1);
+ assertEqualUrls(u1, actual);
+ actual = FileSystem.convertFileToURL(f2);
+ assertEqualUrls(u2e, actual);
+ }
+ finally {
+ URLHandlerUtil.installArakhneHandlers();
+ }
+ }
+
+ private void assertEqualUrls(URL expected, URL actual) {
+ String u1 = expected==null ? null : expected.toExternalForm().replaceAll("/$", ""); //$NON-NLS-1$//$NON-NLS-2$
+ String u2 = actual==null ? null : actual.toExternalForm().replaceAll("/$", ""); //$NON-NLS-1$//$NON-NLS-2$
+ assertEquals(u1, u2);
+ }
+
}