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.com/filename"/>
        <property name="to" value="sftp://username:password@myserver.com/home/whatever"/>
        <property name="fileSystemOptions" ref="sftpFileSystemOptions"/>

    <bean id="sftpFileSystemConfigBuilder"

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

    <bean id="sftpFileSystemOptions_disableStrictHostKeyChecking"
        <property name="targetObject" ref="sftpFileSystemConfigBuilder"/>
        <property name="targetMethod" value="setStrictHostKeyChecking"/>
        <property name="arguments">
                <ref local="sftpFileSystemOptions"/>

    <!-- use private key instead of password -->
    <bean id="sftpFileSystemOptions_setIdentities"
        <property name="targetObject" ref="sftpFileSystemConfigBuilder"/>
        <property name="targetMethod" value="setIdentities"/>
        <property name="arguments">
               <ref local="sftpFileSystemOptions"/>
               <list><value>Path to the ssh identity file</value></list>

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

6 thoughts on “Writing file system independent applications with commons-vfs

  1. 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?

  2. 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/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s