adammurdoch 2002/06/16 17:06:16
Modified: aut/src/java/org/apache/aut/vfs FileObject.java
FileSystemManager.java
aut/src/java/org/apache/aut/vfs/impl
DefaultFileSystemManager.java
DefaultProviderContext.java
aut/src/java/org/apache/aut/vfs/provider
AbstractFileObject.java
FileSystemProviderContext.java
aut/src/test/org/apache/aut/vfs/test
AbstractFileSystemTestCase.java
Added: aut/src/java/org/apache/aut/vfs/impl
URLStreamHandlerProxy.java
aut/src/java/org/apache/aut/vfs/provider
DefaultURLConnection.java
DefaultURLStreamHandler.java
Log:
Added support for accessing the VFS using java.net.URL:
* Added FileSystemManager.getURLStreamHandlerFactory()
* Added FileObject.getURL()
* Added test cases.
Patch submitted by Brian Olsen.
Revision Changes Path
1.5 +8 -1
jakarta-ant-myrmidon/aut/src/java/org/apache/aut/vfs/FileObject.java
Index: FileObject.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/aut/src/java/org/apache/aut/vfs/FileObject.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- FileObject.java 7 Apr 2002 02:27:55 -0000 1.4
+++ FileObject.java 17 Jun 2002 00:06:16 -0000 1.5
@@ -8,6 +8,8 @@
package org.apache.aut.vfs;
import java.io.File;
+import java.net.URL;
+import java.net.MalformedURLException;
/**
* This interface represents a file, and is used to access the content and
@@ -64,6 +66,11 @@
* Returns the name of this file.
*/
FileName getName();
+
+ /**
+ * Returns a URL representing the file.
+ */
+ URL getURL() throws MalformedURLException;
/**
* Determines if this file exists.
1.9 +7 -1
jakarta-ant-myrmidon/aut/src/java/org/apache/aut/vfs/FileSystemManager.java
Index: FileSystemManager.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/aut/src/java/org/apache/aut/vfs/FileSystemManager.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- FileSystemManager.java 24 Apr 2002 02:20:59 -0000 1.8
+++ FileSystemManager.java 17 Jun 2002 00:06:16 -0000 1.9
@@ -8,6 +8,7 @@
package org.apache.aut.vfs;
import java.io.File;
+import java.net.URLStreamHandlerFactory;
/**
* A FileSystemManager is manages a set of file systems. This interface is
@@ -143,4 +144,9 @@
*/
FileObject createFileSystem( String provider, FileObject file )
throws FileSystemException;
+
+ /**
+ * Returns a streamhandler factory to enable URL lookup using this
FileSystemManager.
+ */
+ URLStreamHandlerFactory getURLStreamHandlerFactory();
}
1.8 +32 -2
jakarta-ant-myrmidon/aut/src/java/org/apache/aut/vfs/impl/DefaultFileSystemManager.java
Index: DefaultFileSystemManager.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/aut/src/java/org/apache/aut/vfs/impl/DefaultFileSystemManager.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- DefaultFileSystemManager.java 7 Apr 2002 02:27:56 -0000 1.7
+++ DefaultFileSystemManager.java 17 Jun 2002 00:06:16 -0000 1.8
@@ -8,6 +8,8 @@
package org.apache.aut.vfs.impl;
import java.io.File;
+import java.net.URLStreamHandler;
+import java.net.URLStreamHandlerFactory;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
@@ -16,6 +18,7 @@
import org.apache.aut.vfs.FileObject;
import org.apache.aut.vfs.FileSystemException;
import org.apache.aut.vfs.FileSystemManager;
+import org.apache.aut.vfs.provider.DefaultURLStreamHandler;
import org.apache.aut.vfs.provider.FileReplicator;
import org.apache.aut.vfs.provider.FileSystemProvider;
import org.apache.aut.vfs.provider.LocalFileSystemProvider;
@@ -53,6 +56,9 @@
/** The base file to use for relative URI. */
private FileObject m_baseFile;
+ /** The context to send to providers. */
+ private DefaultProviderContext m_context = new DefaultProviderContext(
this );
+
/**
* Registers a file system provider.
*/
@@ -83,7 +89,7 @@
// Contextualise
setupLogger( provider );
- provider.setContext( new DefaultProviderContext( this ) );
+ provider.setContext( m_context );
// Add to map
for( int i = 0; i < urlSchemes.length; i++ )
@@ -265,5 +271,29 @@
throw new FileSystemException( message );
}
return m_localFileProvider;
+ }
+
+ /**
+ * Get the URLStreamHandlerFactory.
+ */
+ public URLStreamHandlerFactory getURLStreamHandlerFactory()
+ {
+ return new VfsStreamHandlerFactory();
+ }
+
+ // This is an internal class because it needs access to the private
member m_providers.
+ class VfsStreamHandlerFactory implements URLStreamHandlerFactory
+ {
+ public URLStreamHandler createURLStreamHandler( final String
protocol )
+ {
+ final FileSystemProvider provider =
(FileSystemProvider)m_providers.get( protocol );
+ if( provider != null )
+ {
+ return new DefaultURLStreamHandler( m_context );
+ }
+
+ //Route all other calls to the default URLStreamHandlerFactory
+ return new URLStreamHandlerProxy();
+ }
}
}
1.3 +9 -1
jakarta-ant-myrmidon/aut/src/java/org/apache/aut/vfs/impl/DefaultProviderContext.java
Index: DefaultProviderContext.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/aut/src/java/org/apache/aut/vfs/impl/DefaultProviderContext.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- DefaultProviderContext.java 7 Apr 2002 02:27:56 -0000 1.2
+++ DefaultProviderContext.java 17 Jun 2002 00:06:16 -0000 1.3
@@ -38,6 +38,14 @@
}
/**
+ * Locate a file by name.
+ */
+ public FileObject resolveFile( final String name )
+ throws FileSystemException
+ {
+ return m_manager.resolveFile( name );
+ }
+ /**
* Locates a file replicator for the provider to use.
*/
public FileReplicator getReplicator() throws FileSystemException
1.1
jakarta-ant-myrmidon/aut/src/java/org/apache/aut/vfs/impl/URLStreamHandlerProxy.java
Index: URLStreamHandlerProxy.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.aut.vfs.impl;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.net.MalformedURLException;
/**
* A proxy for URLs that are supported by the standard stream handler factory.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Brian Olsen</a>
* @version $Revision: 1.1 $ $Date: 2002/06/17 00:06:16 $
*/
public class URLStreamHandlerProxy
extends URLStreamHandler
{
protected URLConnection openConnection( URL url )
throws IOException
{
URL proxyURL = new URL( url.toExternalForm() );
return proxyURL.openConnection();
}
protected void parseURL( URL u, String spec, int start, int limit )
{
try{
URL url = new URL( u, spec );
setURL( u, url.getProtocol(), url.getHost(),
url.getPort(), url.getAuthority(), url.getUserInfo(),
url.getFile(), url.getQuery(), url.getRef() );
}
catch( MalformedURLException mue )
{
//We retrow this as a simple runtime exception.
//It is retrown in URL as a MalformedURLException anyway.
throw new RuntimeException( mue.getMessage() );
}
}
}
1.9 +15 -1
jakarta-ant-myrmidon/aut/src/java/org/apache/aut/vfs/provider/AbstractFileObject.java
Index: AbstractFileObject.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/aut/src/java/org/apache/aut/vfs/provider/AbstractFileObject.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- AbstractFileObject.java 7 Apr 2002 02:27:56 -0000 1.8
+++ AbstractFileObject.java 17 Jun 2002 00:06:16 -0000 1.9
@@ -12,6 +12,9 @@
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
+import java.net.URL;
+import java.net.MalformedURLException;
+import java.net.URLStreamHandler;
import org.apache.aut.vfs.FileConstants;
import org.apache.aut.vfs.FileContent;
import org.apache.aut.vfs.FileName;
@@ -211,6 +214,17 @@
public FileName getName()
{
return m_name;
+ }
+
+ /**
+ * Returns a URL representation of the file.
+ */
+ public URL getURL() throws MalformedURLException
+ {
+ StringBuffer buf = new StringBuffer();
+
+ return new URL( UriParser.extractScheme( m_name.getURI(), buf ),
null, -1,
+ buf.toString(), new DefaultURLStreamHandler(
m_fs.getContext() ) );
}
/**
1.4 +9 -1
jakarta-ant-myrmidon/aut/src/java/org/apache/aut/vfs/provider/FileSystemProviderContext.java
Index: FileSystemProviderContext.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/aut/src/java/org/apache/aut/vfs/provider/FileSystemProviderContext.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- FileSystemProviderContext.java 7 Apr 2002 02:27:56 -0000 1.3
+++ FileSystemProviderContext.java 17 Jun 2002 00:06:16 -0000 1.4
@@ -29,6 +29,14 @@
throws FileSystemException;
/**
+ * Locate a file by name. See
+ * [EMAIL PROTECTED] FileSystemManager#resolveFile( String)} for a
+ * description of how this works.
+ */
+ FileObject resolveFile( String name )
+ throws FileSystemException;
+
+ /**
* Locates a file replicator for the provider to use.
*/
FileReplicator getReplicator() throws FileSystemException;
1.1
jakarta-ant-myrmidon/aut/src/java/org/apache/aut/vfs/provider/DefaultURLConnection.java
Index: DefaultURLConnection.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.aut.vfs.provider;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
import org.apache.aut.vfs.FileContent;
import org.apache.aut.vfs.FileSystemException;
/**
* A default URL connection that will work for most file systems.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Brian Olsen</a>
* @version $Revision: 1.1 $ $Date: 2002/06/17 00:06:16 $
*/
public class DefaultURLConnection
extends URLConnection
{
FileContent m_content;
public DefaultURLConnection( URL url, FileContent content )
{
super( url );
m_content = content;
}
public void connect()
{
connected = true;
}
public InputStream getInputStream()
throws IOException
{
try{
return m_content.getInputStream();
}
catch( FileSystemException fse )
{
throw new ProtocolException( fse.getMessage() );
}
}
public OutputStream getOutputStream()
throws IOException
{
try{
return m_content.getOutputStream();
}
catch( FileSystemException fse )
{
throw new ProtocolException( fse.getMessage() );
}
}
public int getContentLength()
{
try{
return (int)m_content.getSize();
}
catch( FileSystemException fse ) {}
return -1;
}
}
1.1
jakarta-ant-myrmidon/aut/src/java/org/apache/aut/vfs/provider/DefaultURLStreamHandler.java
Index: DefaultURLStreamHandler.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.aut.vfs.provider;
import java.io.IOException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import org.apache.aut.vfs.FileObject;
import org.apache.aut.vfs.FileSystemException;
/**
* A default URL stream handler that will work for most file systems.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Brian Olsen</a>
* @version $Revision: 1.1 $ $Date: 2002/06/17 00:06:16 $
*/
public class DefaultURLStreamHandler
extends URLStreamHandler
{
FileSystemProviderContext m_context;
public DefaultURLStreamHandler( FileSystemProviderContext context )
{
m_context = context;
}
protected URLConnection openConnection( URL url )
throws IOException
{
try{
FileObject entry = m_context.resolveFile( url.toExternalForm() );
return new DefaultURLConnection( url, entry.getContent() );
}
catch( FileSystemException fse )
{
throw new ProtocolException( fse.getMessage() );
}
}
protected void parseURL( URL u, String spec, int start, int limit )
{
try{
FileObject old = null;
if( u.getProtocol() != null )
old = m_context.resolveFile( u.toExternalForm() );
FileObject newURL = null;
if( start > 0 && spec.charAt( start-1 ) == ':' )
newURL = m_context.resolveFile( old, spec );
else
newURL = old.resolveFile( spec );
final String url = newURL.getName().getURI();
final StringBuffer filePart = new StringBuffer();
final String protocolPart = UriParser.extractScheme( url,
filePart );
setURL( u, protocolPart, null, -1, null, null,
filePart.toString(), null, null );
}
catch( FileSystemException fse )
{
// This is rethrown to MalformedURLException in URL anyway
throw new RuntimeException( fse.getMessage() );
}
}
protected String toExternalForm( URL u )
{
return u.getProtocol() + ":" + u.getFile();
}
}
1.8 +110 -1
jakarta-ant-myrmidon/aut/src/test/org/apache/aut/vfs/test/AbstractFileSystemTestCase.java
Index: AbstractFileSystemTestCase.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/aut/src/test/org/apache/aut/vfs/test/AbstractFileSystemTestCase.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- AbstractFileSystemTestCase.java 6 May 2002 09:29:44 -0000 1.7
+++ AbstractFileSystemTestCase.java 17 Jun 2002 00:06:16 -0000 1.8
@@ -9,6 +9,9 @@
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
+import java.io.IOException;
+import java.net.URLConnection;
+import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@@ -738,6 +741,112 @@
// Should be able to get child by name
FileObject child = file.resolveFile( "some-child" );
assertNotNull( child );
+ }
+
+ /**
+ * Tests url.
+ */
+ public void testURL() throws Exception
+ {
+ FileObject file = m_baseFolder.resolveFile( "some-dir/" );
+ URL url = file.getURL();
+
+ assertEquals( file.getName().getURI(), url.toExternalForm() );
+
+ URL parentURL = new URL( url, ".." );
+ assertEquals( m_baseFolder.getURL(), parentURL );
+
+ URL rootURL = new URL( url, "/" );
+ assertEquals( file.getRoot().getURL(), rootURL );
+ }
+
+ /**
+ * Tests content.
+ */
+ public void testURLContent() throws Exception
+ {
+ // Test non-empty file
+ FileObject file = m_baseFolder.resolveFile( "file1.txt" );
+ URLConnection urlCon = file.getURL().openConnection();
+ assertSameURLContent( m_charContent, urlCon );
+
+ // Test empty file
+ file = m_baseFolder.resolveFile( "empty.txt" );
+ urlCon = file.getURL().openConnection();
+ assertSameURLContent( "", urlCon );
+ }
+
+ /**
+ * Asserts that the content of a file is the same as expected. Checks the
+ * length reported by getSize() is correct, then reads the content as
+ * a byte stream, and as a char stream, and compares the result with
+ * the expected content. Assumes files are encoded using UTF-8.
+ */
+ protected void assertSameURLContent( final String expected,
+ final URLConnection connection )
+ throws Exception
+ {
+ // Get file content as a binary stream
+ final byte[] expectedBin = expected.getBytes( "utf-8" );
+
+ // Check lengths
+ assertEquals( "same content length", expectedBin.length,
connection.getContentLength() );
+
+ // Read content into byte array
+ final InputStream instr = connection.getInputStream();
+ final ByteArrayOutputStream outstr;
+ try
+ {
+ outstr = new ByteArrayOutputStream();
+ final byte[] buffer = new byte[ 256 ];
+ int nread = 0;
+ while( nread >= 0 )
+ {
+ outstr.write( buffer, 0, nread );
+ nread = instr.read( buffer );
+ }
+ }
+ finally
+ {
+ instr.close();
+ }
+
+ // Compare
+ assertTrue( "same binary content", Arrays.equals( expectedBin,
outstr.toByteArray() ) );
+ }
+
+ /**
+ * Tests that folders and unknown files have no content.
+ */
+ public void testNoURLContent() throws Exception
+ {
+ // Try getting the content of a folder
+ FileObject folder = m_baseFolder.resolveFile( "dir1" );
+ try
+ {
+ folder.getURL().openConnection();
+ fail();
+ }
+ catch( IOException e )
+ {
+ final String message = REZ.getString(
"get-folder-content.error", folder );
+ assertSameMessage( message, e );
+ }
+
+ // Try getting the content of an unknown file
+ FileObject unknownFile = m_baseFolder.resolveFile( "unknown-file" );
+ URLConnection connection = unknownFile.getURL().openConnection();
+ try
+ {
+ connection.getInputStream();
+ fail();
+ }
+ catch( IOException e )
+ {
+ final String message = REZ.getString( "read-no-exist.error",
unknownFile );
+ assertSameMessage( message, e );
+ }
+ assertEquals( -1, connection.getContentLength() );
}
/**
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>