Example usage for org.apache.commons.vfs2 FileObject getFileSystem

List of usage examples for org.apache.commons.vfs2 FileObject getFileSystem

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 FileObject getFileSystem.

Prototype

FileSystem getFileSystem();

Source Link

Document

Returns the file system that contains this file.

Usage

From source file:ShowProperties.java

public static void main(String[] args) throws FileSystemException {
    if (args.length == 0) {
        System.err.println("Please pass the name of a file as parameter.");
        System.err.println("e.g. java org.apache.commons.vfs2.example.ShowProperties LICENSE.txt");
        return;//from   w w  w  .  j a v a 2  s . co m
    }
    for (int i = 0; i < args.length; i++) {
        try {
            FileSystemManager mgr = VFS.getManager();
            System.out.println();
            System.out.println("Parsing: " + args[i]);
            FileObject file = mgr.resolveFile(args[i]);
            System.out.println("URL: " + file.getURL());
            System.out.println("getName(): " + file.getName());
            System.out.println("BaseName: " + file.getName().getBaseName());
            System.out.println("Extension: " + file.getName().getExtension());
            System.out.println("Path: " + file.getName().getPath());
            System.out.println("Scheme: " + file.getName().getScheme());
            System.out.println("URI: " + file.getName().getURI());
            System.out.println("Root URI: " + file.getName().getRootURI());
            System.out.println("Parent: " + file.getName().getParent());
            System.out.println("Type: " + file.getType());
            System.out.println("Exists: " + file.exists());
            System.out.println("Readable: " + file.isReadable());
            System.out.println("Writeable: " + file.isWriteable());
            System.out.println("Root path: " + file.getFileSystem().getRoot().getName().getPath());
            if (file.exists()) {
                if (file.getType().equals(FileType.FILE)) {
                    System.out.println("Size: " + file.getContent().getSize() + " bytes");
                } else if (file.getType().equals(FileType.FOLDER) && file.isReadable()) {
                    FileObject[] children = file.getChildren();
                    System.out.println("Directory with " + children.length + " files");
                    for (int iterChildren = 0; iterChildren < children.length; iterChildren++) {
                        System.out.println("#" + iterChildren + ": " + children[iterChildren].getName());
                        if (iterChildren > 5) {
                            break;
                        }
                    }
                }
                System.out.println("Last modified: "
                        + DateFormat.getInstance().format(new Date(file.getContent().getLastModifiedTime())));
            } else {
                System.out.println("The file does not exist");
            }
            file.close();
        } catch (FileSystemException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:com.googlecode.vfsjfilechooser2.utils.VFSUtils.java

/**
 * Returns the root filesystem of a given file
 * @param fileObject A file/*  w w w .j a va2 s .  c  om*/
 * @return the root filesystem of a given file
 */
public static FileObject createFileSystemRoot(FileObject fileObject) {
    try {
        return fileObject.getFileSystem().getRoot();
    } catch (FileSystemException ex) {
        return null;
    }
}

From source file:com.googlecode.vfsjfilechooser2.utils.VFSUtils.java

/**
 * Returns the root file system of a file representation
 * @param fileObject A file abstraction// www  .ja  va 2 s .com
 * @return the root file system of a file representation
 */
public static FileObject getRootFileSystem(FileObject fileObject) {
    try {
        if ((fileObject == null) || !fileObject.exists()) {
            return null;
        }

        return fileObject.getFileSystem().getRoot();
    } catch (FileSystemException ex) {
        return null;
    }
}

From source file:net.dempsy.distconfig.apahcevfs.ApacheVfsPropertiesReader.java

@Override
public VersionedProperties read(final PropertiesWatcher watcher) throws IOException {
    final FileObject latest = getLatest(parentDirObj);
    final int ver = latest == null ? -1 : getVersion(latest);
    final VersionedProperties ret = new VersionedProperties(ver, Utils.read(latest));
    if (watcher != null) {
        // if there's a watcher ....
        final FileObject next = nextFile(latest, parentDirObj);
        final Proxy proxy = new Proxy(next, watcher);
        next.getFileSystem().addListener(next, proxy);

        // but now, if the file exists due to a race condition, lets at least address that.
        if (next.exists())
            proxy.doIt();//  w w  w . ja  va2 s.co  m
    }
    return ret;
}

From source file:com.stratuscom.harvester.CommonsVFSTest.java

/**
If we create a virtual file system based on a jar file, we should be
able to add other jar files by adding junctions to the root, with the name
of the file we're adding.//w ww.java2s.  c o  m
        
        
Unfortunately, this theory doesn't pan out...
org.apache.commons.vfs.FileSystemException: Attempting to create a nested junction at "null/otherStart.properties".  Nested junctions are not supported.
at org.apache.commons.vfs.impl.VirtualFileSystem.addJunction(VirtualFileSystem.java:111)
        
 */
@Test
@Ignore /*Didin't work, see above */
public void testFileSystemJunctions() throws Exception {
    FileObject reggieJar = fileSystemManager.resolveFile(new File("../../build/test/files"),
            "reggie-module.jar");
    assertTrue("Bad file:" + reggieJar.toString(), reggieJar.toString().endsWith("reggie-module.jar"));
    FileObject reggieJarFS = fileSystemManager.createFileSystem(reggieJar);

    FileObject virtRoot = fileSystemManager.createVirtualFileSystem((String) null);
    virtRoot.getFileSystem().addJunction("/", reggieJarFS);
    checkPresentAndReadable(virtRoot, "start.properties");
    FileObject startProperties = virtRoot.resolveFile("start.properties");
    assertNotNull(startProperties);
    assertTrue(
            "Properties file unreadable:" + startProperties.toString() + " type=" + startProperties.getType(),
            startProperties.isReadable());

    /* Now try to add in a junction to a jar file */
    virtRoot.getFileSystem().addJunction("otherStart.properties", startProperties);
    checkPresentAndReadable(virtRoot, "otherStart.properties");
}

From source file:de.blizzy.backup.vfs.RemoteLocation.java

private FileSystem getFileSystem() throws FileSystemException {
    if (fileSystem == null) {
        FileSystemOptions opts = getFileSystemOptions();
        FileObject fileObject = VFS.getManager().resolveFile(
                getProtocol() + "://" + host + ":" + String.valueOf(port), //$NON-NLS-1$ //$NON-NLS-2$
                opts);/* www  . j av  a2s.c  om*/
        fileSystem = fileObject.getFileSystem();
    }
    return fileSystem;
}

From source file:de.innovationgate.wgpublisher.design.fs.AbstractDesignFile.java

private FileObject getOrCreateMetadataDir() throws FileSystemException, WGDesignSyncException {
    FileObject metadataDir = getMetadataDir();

    if (!metadataDir.exists() && metadataDir.getFileSystem().hasCapability(Capability.CREATE)) {
        metadataDir.createFolder();/*from ww w .  ja va2  s .c  o m*/
    }

    return metadataDir;
}

From source file:de.innovationgate.wgpublisher.design.fs.AbstractDesignFile.java

protected FileObject getOrCreateMetadataFile()
        throws InstantiationException, IllegalAccessException, IOException, WGDesignSyncException {

    FileObject metadataFile = getMetadataFile();

    if (!metadataFile.exists() && metadataFile.getFileSystem().hasCapability(Capability.CREATE)) {
        getOrCreateMetadataDir();//  ww  w  . j a  v  a2  s.  co m
        createMetadataFile(metadataFile);
    }

    return metadataFile;
}

From source file:com.seeburger.vfs2.util.VFSClassLoader.java

/**
 * Calls super.getPermissions both for the code source and also
 * adds the permissions granted to the parent layers.
 * @param cs the CodeSource.//from   www . j  a  v a  2 s. com
 * @return The PermissionCollections.
 */
@Override
protected PermissionCollection getPermissions(final CodeSource cs) {
    try {
        final String url = cs.getLocation().toString();
        FileObject file = lookupFileObject(url);
        if (file == null) {
            return super.getPermissions(cs);
        }

        FileObject parentLayer = file.getFileSystem().getParentLayer();
        if (parentLayer == null) {
            return super.getPermissions(cs);
        }

        Permissions combi = new Permissions();
        PermissionCollection permCollect = super.getPermissions(cs);
        copyPermissions(permCollect, combi);

        for (FileObject parent = parentLayer; parent != null; parent = parent.getFileSystem()
                .getParentLayer()) {
            final CodeSource parentcs = new CodeSource(parent.getURL(), parent.getContent().getCertificates());
            permCollect = super.getPermissions(parentcs);
            copyPermissions(permCollect, combi);
        }

        return combi;
    } catch (final FileSystemException fse) {
        throw new SecurityException(fse.getMessage());
    }
}

From source file:com.stratuscom.harvester.classloading.VirtualFileSystemClassLoader.java

public void addClasspathFilters(List<ClasspathFilter> filters, FileObject fileRoot) throws FileSystemException {
    for (ClasspathFilter filter : filters) {
        FileObject entryObject = fileRoot.resolveFile(filter.getJarName());

        FileObject entryFileSystem = fileRoot.getFileSystem().getFileSystemManager()
                .createFileSystem(entryObject);
        classpathEntries.add(new ClasspathEntry(filter, entryFileSystem));
    }//from w  w w . j ava2s . c o  m
}