Writing file system independent applications with commons-vfs

Lately I've been playing with commons virtual file system (commons-vfs), which is a really cool way to make your application file system independent. You can write the same code to access files in your local file system, samba servers, sftp servers, http, webdav,... see the complete list.

        FileSystemManager fileSystemManager = VFS.getManager();
        FileObject from = fileSystemManager.resolveFile(getFrom());
        FileObject to = fileSystemManager.resolveFile(getTo(), getFileSystemOptions());
        to.copyFrom(from, new AllFileSelector());

The application context with Spring would be something like the following. You can see that setting the file system options it's a pita because they don't follow the beans convention and you need to call different methods instead of setting bean properties. I hop they change ths in next versions.

    <bean id="myBean" class="TestBean">
        <property name="from" value="http://www.myserver-DOT-com/filename"/>
        <property name="to" value="sftp://username:password-AT-myserver.com/home/whatever"/>
        <property name="fileSystemOptions" ref="sftpFileSystemOptions"/>
    </bean>

    <bean id="sftpFileSystemConfigBuilder"
          class="org.apache.commons.vfs.provider.sftp.SftpFileSystemConfigBuilder"
          factory-method="getInstance"/>

    <bean id="sftpFileSystemOptions" class="org.apache.commons.vfs.FileSystemOptions"/>

    <bean id="sftpFileSystemOptions_disableStrictHostKeyChecking"
 class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject" ref="sftpFileSystemConfigBuilder"/>
        <property name="targetMethod" value="setStrictHostKeyChecking"/>
        <property name="arguments">
            <list>
               <ref local="sftpFileSystemOptions"/>
                <value>no</value>
           </list>
        </property>
    </bean>
    <!-- use private key instead of password -->
    <bean id="sftpFileSystemOptions_setIdentities"
     class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject" ref="sftpFileSystemConfigBuilder"/>
        <property name="targetMethod" value="setIdentities"/>
        <property name="arguments">
            <list>
               <ref local="sftpFileSystemOptions"/>
               <list><value>Path to the ssh identity file</value></list>
            </list>
        </property>
    </bean>

Another drawback is that you need to build from sources, no releases are available yet :(

Comments [6]

Comments:

Does it support extended attributes if supported by the underlying filesystem?

Posted by Torgeir on September 14, 2005 at 04:24 PM PDT #

Each filesystem has its own FileSystemConfigBuilder that allows you to create a filesystem dependant FileSystemOptions object. So sftp has identity file support which has no sense in http, for instance.
Is that what you asked for?

Posted by Carlos Sanchez on September 14, 2005 at 07:16 PM PDT #

Just googled your blog now.

Maybe the DelegatingFileSystemOptionsBuilder is of some help, at least it should allow you to easily create one.

The idea behind this class is to use strings only to create the configuration, so the following code snipped is the same:


FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setIdentities(opts, new File[]{new File("path/to/ident.dsa")});

FileSystemOptions optsDel = new FileSystemOptions();
DelegatingFileSystemOptionsBuilder delegate = new DelegatingFileSystemOptionsBuilder(VFS.getManager());
delegate.setConfigString(optsDel, "sftp", "identities", "path/to/ident.dsa");


The "DelegatingFileSystemOptionsBuilder" heavily uses reflection to use (in this example) the SftpFileSystemConfigBuilder then and convert the string to the correct argument type.

Let me know if this is of some help.


Also you can find already built nightlies here: http://cvs.apache.org/builds/jakarta-commons/nightly/commons-vfs/

Posted by Mario Ivankovits on November 07, 2005 at 05:02 AM PST #

Your work is great. Happy New Year! And Marry Cristmass!

Posted by nFranklin on January 03, 2006 at 08:25 AM PST #

Can vfs be utilized in a EJB, i.e., does vfs use java.io file access?

Posted by patrick on June 22, 2006 at 10:09 AM PDT #

I have no idea, you would need to ask in the apache commons mailing list

Posted by Carlos Sanchez on June 22, 2006 at 10:28 AM PDT #

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed