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

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

Introduction

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

Prototype

boolean isReadable() throws FileSystemException;

Source Link

Document

Determines if this file can be read.

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 .ja  v  a2 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:fulcrum.xml.ParserTest.java

private static InputStream getInputStream(String location) {
    InputStream is = null;//from  w  ww  .  java 2s  .c o  m
    try {
        FileSystemManager fsManager = VFS.getManager();
        FileObject fileObj = fsManager.resolveFile(location);
        if (fileObj != null && fileObj.exists() && fileObj.isReadable()) {
            FileContent content = fileObj.getContent();
            is = content.getInputStream();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return is;
}

From source file:fulcrum.xml.soap12.Soap12XOMTest.java

@Test
public void testXOM() {
    Document doc = null;//  w  w  w.  jav  a  2 s  .  co  m
    Builder parser = null;
    try {
        FileSystemManager fsManager = VFS.getManager();
        FileObject fileObj = fsManager.resolveFile(SIMPLE);
        if (fileObj != null && fileObj.exists() && fileObj.isReadable()) {

            FileContent content = fileObj.getContent();
            InputStream is = content.getInputStream();
            LOGGER.info("STARTING PARSE:");
            parser = new Builder();
            doc = parser.build(is);
            LOGGER.info("ENDING PARSE");
            System.out.println(doc.toXML());
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        parser = null;
    }
}

From source file:mondrian.spi.impl.ApacheVfs2VirtualFileHandler.java

public InputStream readVirtualFile(String url) throws FileSystemException {
    // Treat catalogUrl as an Apache VFS (Virtual File System) URL.
    // VFS handles all of the usual protocols (http:, file:)
    // and then some.
    FileSystemManager fsManager = VFS.getManager();
    if (fsManager == null) {
        throw Util.newError("Cannot get virtual file system manager");
    }//from  w w  w. j  ava  2 s  .  co  m

    File userDir = new File("").getAbsoluteFile();
    FileObject file = fsManager.resolveFile(userDir, url);
    FileContent fileContent = null;
    try {
        if (!file.isReadable()) {
            throw Util.newError("Virtual file is not readable: " + url);
        }

        fileContent = file.getContent();
    } finally {
        file.close();
    }

    if (fileContent == null) {
        throw Util.newError("Cannot get virtual file content: " + url);
    }

    return fileContent.getInputStream();
}

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

void checkPresentAndReadable(FileObject root, String name) throws FileSystemException {
    FileObject fo = root.resolveFile(name);
    assertNotNull(fo);//  w ww . j a  va2  s .c o m
    assertTrue("File unreadable:" + fo.toString() + " type=" + fo.getType(), fo.isReadable());
}

From source file:fulcrum.xml.Parser.java

/**
 * Only provides parsing functions to the "fulcrum.xml" package.
 * /*from w w w. j  a  v a2  s .co m*/
 * @see Document
 * 
 * @param fileLocation
 * @param document
 * @throws ParseException
 */
protected void parse(String fileLocation, Document document) throws ParseException {
    FileObject fileObj = null;
    try {
        fileObj = fsManager.resolveFile(fileLocation);

        if (fileObj != null && fileObj.exists() && fileObj.isReadable()) {
            FileContent content = fileObj.getContent();
            InputStream is = content.getInputStream();
            int size = is.available();
            LOGGER.debug("Total File Size: " + size + " bytes");
            byte[] buffer = new byte[size];
            is.read(buffer, 0, size);
            LOGGER.debug("Start parsing");
            parse(buffer, document);
            LOGGER.debug("Finished paring");
            buffer = null;
            content.close();
            fileObj.close();
        }
    } catch (Exception e) {
        throw new ParseException(e);
    }
}

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

/**
Make sure we can use the jar:syntax to get to the 'start.properties' file
inside the constructed reggie module jar.
 *///from   w  w w . j a v a 2 s . c om
@Test
public void testFileInReggieModuleJar() throws Exception {
    FileObject reggieJar = fileSystemManager.resolveFile(new File("target/reggie-module"), "reggie-module.jar");
    assertTrue("Bad file:" + reggieJar.toString(), reggieJar.toString().endsWith("reggie-module.jar"));
    FileObject reggieJarFS = fileSystemManager.createFileSystem(Strings.JAR, reggieJar);

    FileObject startProperties = reggieJarFS.resolveFile("start.properties");
    assertNotNull(startProperties);
    assertTrue(
            "Properties file unreadable:" + startProperties.toString() + " type=" + startProperties.getType(),
            startProperties.isReadable());
}

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./*from  ww  w .j  ava2s .  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:com.stratuscom.harvester.classloading.VFSClassLoaderTest.java

/**
Just to make sure that we have the base setup correct, ensure that we
can read the 'start.properties' file inside the reggie-module jar.
@throws Exception/*  ww w  .j  a v a 2  s.c o m*/
 */
@Test
public void testCanReadStartDotProperties() throws Exception {
    FileObject startProperties = reggieModuleRoot.resolveFile("start.properties");
    assertNotNull(startProperties);
    assertTrue(
            "Properties file unreadable:" + startProperties.toString() + " type=" + startProperties.getType(),
            startProperties.isReadable());
}

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

/**
 * Find the file object for a resource by searching through all the
 * classpath entries that have been set up.
 *
 * @param name//w  ww  .j  a  va 2s  .c o m
 * @return
 */
public FileObject findResourceFileObject(String name) {
    for (ClasspathEntry cpEntry : classpathEntries) {
        try {
            FileObject fo = cpEntry.resolveFile(name);
            if (fo != null && fo.isReadable()) {
                return fo;
            }
        } catch (FileSystemException ex) {
            Logger.getLogger(VirtualFileSystemClassLoader.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return null;
}