Using The API
The
FileSystemManager
interface provides access to Commons VFS. Using this interface
you can locate files and create file systems.
There are a
number of ways
to obtain a
Once you have a
FileSystemManager fsManager = VFS.getManager(); FileObject jarFile = fsManager.resolveFile("jar:lib/aJarFile.jar"); Each file is represented by a FileObject instance. Using this interface you can create or delete the file, list its children, read or write its content, and so on. For example: // Locate the Jar file FileSystemManager fsManager = VFS.getManager(); FileObject jarFile = fsManager.resolveFile("jar:lib/aJarFile.jar"); // List the children of the Jar file FileObject[] children = jarFile.getChildren(); System.out.println("Children of " + jarFile.getName().getURI()); for (int i = 0; i < children.length; i++) { System.out.println(children[i].getName().getBaseName()); }
In some cases you might want to explicitely free resources allocated by the filesystem.
You can do this by calling
VFS.getManager().closeFileSystem(fs).
If you use VFS as singleton (as described above) you should take care that this will close the filesystem for
all threads. See the FileObject Javadocs for more detail. CacheCommons VFS uses a SoftRefFilesCache to release memory if a file is no longer used by the application. This cache will return the same instance for a file as long as it is "strongly reachable" e.g. you hold a reference to this object. If the FileObject is no longer reachable, and the jvm needs some memory, it will be released.
There is also a internal cache of each file object avoid the need to access the network layer. Now its possible
to configure this behviour through the use of CacheStrategy.
User AuthenticationYou can put the credentials into the url, but the drawback here is, that it is easily possible to get access to the password. To solve you can use the UserAuthenticator For example: StaticUserAuthenticator auth = new StaticUserAuthenticator("domain", "username", "password"); FileSystemOptions opts = new FileSystemOptions(); DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth); FileObject fo = VFS.getManager().resolveFile("smb://host/anyshare/dir", opts); Internally the UserAuthenticator uses char arrays which will be zeroed before it is freed for garbage collection.Unhappily none of the current libraries use char arrays and so VFS has to create a string. Thus, the main advantage of this solution - security - is lost, but hey, thats not VFS fault ;-)
VFS calls ExamplesFor an example of using the API, take a look at the classes in the example package. Configuring Commons VFS
Commons VFS is represented using the
FileSystemManager
interface. There are a number of ways to create and configure a
The simplest method is to use the static VFS.getManager() method, which returns the default Commons VFS implementation.
This method will also automatically scan the classpath for a /META-INF/vfs-providers.xml file
(also in jar files).
If such a file is found Commons VFS uses it in addition to the default providers.xml.
This allows you to start using a new filesystem by simply drop its implementation into the classpath.
The configuration file format is described below.
To configure Commons VFS programatically, you can create an
instance of
DefaultFileSystemManager
and configure it manually. The default constructor
Here are the steps for using
You should make sure that you call
The third method for configuring Commons VFS, is to configure
it from a file. Create an instance of
StandardFileSystemManager,
and use its
Configuration File
The configuration file is an XML file. The root element
of the configuration file should be a
The
The
The
The
The
The
The
Below is an example configuration file: <providers> <provider class-name="org.apache.commons.vfs2.provider.zip.ZipFileProvider"> <scheme name="zip"/> </provider> <extension-map extension="zip" scheme="zip"/> <mime-type-map mime-type="application/zip" scheme="zip"/> <provider class-name="org.apache.commons.vfs2.provider.ftp.FtpFileProvider"> <scheme name="ftp"/> <if-available class-name="org.apache.commons.net.ftp.FTPFile"/> </provider> <default-provider class-name="org.apache.commons.vfs2.provider.url.UrlFileProvider"/> </providers> |