Permalink
Browse files

JBVFS-178 - add getRoot*() methods to VFSUtils

  • Loading branch information...
1 parent 7c68408 commit 3a9d8a4234f22dbf6f60ece50ee277aeccf394ff @dmlloyd dmlloyd committed Nov 2, 2011
@@ -54,6 +54,7 @@
import java.util.zip.ZipFile;
import org.jboss.logging.Logger;
+import org.jboss.vfs.spi.FileSystem;
import org.jboss.vfs.spi.MountHandle;
import org.jboss.vfs.util.PathTokenizer;
import org.jboss.vfs.util.automount.Automounter;
@@ -553,6 +554,36 @@ public static URI getPhysicalURI(VirtualFile file) throws IOException {
}
/**
+ * Get the physical root URL of the filesystem of a virtual file. This URL is suitable for use as a class loader's
+ * code source or in similar situations where only standard URL types ({@code jar} and {@code file}) are supported.
+ *
+ * @param file the virtual file
+ * @return the root URL
+ * @throws MalformedURLException if the URL is not valid
+ */
+ public static URL getRootURL(VirtualFile file) throws MalformedURLException {
+ final URI uri;
+ try {
+ uri = getRootURI(file);
+ } catch (URISyntaxException e) {
+ throw new MalformedURLException(e.getMessage());
+ }
+ return uri.toURL();
+ }
+
+ /**
+ * Get the physical root URL of the filesystem of a virtual file. This URI is suitable for conversion to a class loader's
+ * code source URL or in similar situations where only standard URL types ({@code jar} and {@code file}) are supported.
+ *
+ * @param file the virtual file
+ * @return the root URI
+ * @throws URISyntaxException if the URI is not valid
+ */
+ public static URI getRootURI(final VirtualFile file) throws URISyntaxException {
+ return VFS.getMount(file).getFileSystem().getRootURI();
+ }
+
+ /**
* Safely close some resource without throwing an exception. Any exception will be logged at TRACE level.
*
* @param c the resource
@@ -25,6 +25,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.FileNotFoundException;
+import java.net.URI;
+import java.net.URISyntaxException;
import java.security.CodeSigner;
import java.util.ArrayList;
import java.util.LinkedList;
@@ -145,6 +147,11 @@ public File getMountSource() {
return null;
}
+ /** {@inheritDoc} */
+ public URI getRootURI() throws URISyntaxException {
+ return null;
+ }
+
private VirtualFile getExistingFile(final VirtualFile mountPoint, final VirtualFile target) throws FileNotFoundException {
final VirtualFile assemblyFile = assembly.getFile(mountPoint, target);
if (assemblyFile == null) {
@@ -22,6 +22,8 @@
package org.jboss.vfs.spi;
+import java.net.URI;
+import java.net.URISyntaxException;
import org.jboss.vfs.VirtualFile;
import java.io.File;
@@ -175,4 +177,12 @@
* @return the source used for mounting
*/
File getMountSource();
+
+ /**
+ * Get the root URI for this file system, or {@code null} if there is no valid root URI.
+ *
+ * @return the root URI
+ * @throws URISyntaxException if the URI isn't valid
+ */
+ URI getRootURI() throws URISyntaxException;
}
@@ -30,6 +30,8 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
import java.security.CodeSigner;
import java.util.ArrayList;
import java.util.Collection;
@@ -292,6 +294,10 @@ public File getMountSource() {
return archiveFile;
}
+ public URI getRootURI() throws URISyntaxException {
+ return new URI("jar", archiveFile.toURI().toString() + "!/", null);
+ }
+
/** {@inheritDoc} */
public void close() throws IOException {
log.tracef("Closing zip filesystem %s", this);
@@ -22,6 +22,8 @@
package org.jboss.vfs.spi;
+import java.net.URI;
+import java.net.URISyntaxException;
import org.jboss.vfs.VirtualFile;
import org.jboss.logging.Logger;
@@ -151,6 +153,10 @@ public File getMountSource() {
return realRoot;
}
+ public URI getRootURI() throws URISyntaxException {
+ return realRoot.toURI();
+ }
+
/**
* {@inheritDoc}
*/
@@ -22,6 +22,8 @@
package org.jboss.vfs.spi;
+import java.net.URI;
+import java.net.URISyntaxException;
import org.jboss.vfs.VirtualFile;
import org.jboss.logging.Logger;
@@ -132,6 +134,10 @@ public File getMountSource() {
return null;
}
+ public URI getRootURI() throws URISyntaxException {
+ return null;
+ }
+
/**
* {@inheritDoc}
*/

0 comments on commit 3a9d8a4

Please sign in to comment.