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

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

Introduction

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

Prototype

FileName getName();

Source Link

Document

Returns the name of this file.

Usage

From source file:org.apache.accumulo.start.classloader.vfs.UniqueFileReplicator.java

@Override
public File replicateFile(FileObject srcFile, FileSelector selector) throws FileSystemException {
    String baseName = srcFile.getName().getBaseName();

    try {/*from   w ww  . ja v a  2s . c o m*/
        String safeBasename = UriParser.encode(baseName, TMP_RESERVED_CHARS).replace('%', '_');
        File file = File.createTempFile("vfsr_", "_" + safeBasename, tempDir);
        file.deleteOnExit();

        final FileObject destFile = context.toFileObject(file);
        destFile.copyFrom(srcFile, selector);

        return file;
    } catch (IOException e) {
        throw new FileSystemException(e);
    }
}

From source file:org.apache.commons.vfs2.example.Shell.java

/**
 * Does a 'cp' command.//from   ww  w . j  a v a 2  s  .c o  m
 */
private void cp(final String[] cmd) throws Exception {
    if (cmd.length < 3) {
        throw new Exception("USAGE: cp <src> <dest>");
    }

    final FileObject src = mgr.resolveFile(cwd, cmd[1]);
    FileObject dest = mgr.resolveFile(cwd, cmd[2]);
    if (dest.exists() && dest.getType() == FileType.FOLDER) {
        dest = dest.resolveFile(src.getName().getBaseName());
    }

    dest.copyFrom(src, Selectors.SELECT_ALL);
}

From source file:org.apache.commons.vfs2.example.Shell.java

/**
 * Does a 'cd' command.//from w w  w  . ja va 2  s . c o m
 * If the taget directory does not exist, a message is printed to <code>System.err</code>.
 */
private void cd(final String[] cmd) throws Exception {
    final String path;
    if (cmd.length > 1) {
        path = cmd[1];
    } else {
        path = System.getProperty("user.home");
    }

    // Locate and validate the folder
    final FileObject tmp = mgr.resolveFile(cwd, path);
    if (tmp.exists()) {
        cwd = tmp;
    } else {
        System.out.println("Folder does not exist: " + tmp.getName());
    }
    System.out.println("Current folder is " + cwd.getName());
}

From source file:org.apache.commons.vfs2.example.Shell.java

/**
 * Does an 'ls' command./*from ww w.  j a v a2s .  c o  m*/
 */
private void ls(final String[] cmd) throws FileSystemException {
    int pos = 1;
    final boolean recursive;
    if (cmd.length > pos && cmd[pos].equals("-R")) {
        recursive = true;
        pos++;
    } else {
        recursive = false;
    }

    final FileObject file;
    if (cmd.length > pos) {
        file = mgr.resolveFile(cwd, cmd[pos]);
    } else {
        file = cwd;
    }

    if (file.getType() == FileType.FOLDER) {
        // List the contents
        System.out.println("Contents of " + file.getName());
        listChildren(file, recursive, "");
    } else {
        // Stat the file
        System.out.println(file.getName());
        final FileContent content = file.getContent();
        System.out.println("Size: " + content.getSize() + " bytes.");
        final DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
        final String lastMod = dateFormat.format(new Date(content.getLastModifiedTime()));
        System.out.println("Last modified: " + lastMod);
    }
}

From source file:org.apache.commons.vfs2.example.Shell.java

/**
 * Lists the children of a folder.//from   ww  w .jav a 2 s  .  co m
 */
private void listChildren(final FileObject dir, final boolean recursive, final String prefix)
        throws FileSystemException {
    final FileObject[] children = dir.getChildren();
    for (final FileObject child : children) {
        System.out.print(prefix);
        System.out.print(child.getName().getBaseName());
        if (child.getType() == FileType.FOLDER) {
            System.out.println("/");
            if (recursive) {
                listChildren(child, recursive, prefix + "    ");
            }
        } else {
            System.out.println();
        }
    }
}

From source file:org.apache.commons.vfs2.example.ShowProperties.java

public static void main(final String[] args) {
    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 ww  w .j a va 2 s.  c om*/
    }
    for (final String arg : args) {
        try {
            final FileSystemManager mgr = VFS.getManager();
            System.out.println();
            System.out.println("Parsing: " + arg);
            final FileObject file = mgr.resolveFile(arg);
            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()) {
                    final 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 > SHOW_MAX) {
                            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 (final FileSystemException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:org.apache.hadoop.gateway.topology.file.FileTopologyProvider.java

private static Topology loadTopology(FileObject file) throws IOException, SAXException, URISyntaxException {
    log.loadingTopologyFile(file.getName().getFriendlyURI());
    Digester digester = digesterLoader.newDigester();
    FileContent content = file.getContent();
    TopologyBuilder topologyBuilder = digester.parse(content.getInputStream());
    Topology topology = topologyBuilder.build();
    topology.setUri(file.getURL().toURI());
    topology.setName(FilenameUtils.removeExtension(file.getName().getBaseName()));
    topology.setTimestamp(content.getLastModifiedTime());
    return topology;
}

From source file:org.apache.hadoop.gateway.topology.file.FileTopologyProvider.java

private Map<FileName, Topology> loadTopologies(FileObject directory) throws FileSystemException {
    Map<FileName, Topology> map = new HashMap<FileName, Topology>();
    if (directory.exists() && directory.getType().hasChildren()) {
        for (FileObject file : directory.getChildren()) {
            if (file.exists() && !file.getType().hasChildren()
                    && SUPPORTED_TOPOLOGY_FILE_EXTENSIONS.contains(file.getName().getExtension())) {
                try {
                    map.put(file.getName(), loadTopology(file));
                } catch (IOException e) {
                    // Maybe it makes sense to throw exception
                    log.failedToLoadTopology(file.getName().getFriendlyURI(), e);
                } catch (SAXException e) {
                    // Maybe it makes sense to throw exception
                    log.failedToLoadTopology(file.getName().getFriendlyURI(), e);
                } catch (Exception e) {
                    // Maybe it makes sense to throw exception
                    log.failedToLoadTopology(file.getName().getFriendlyURI(), e);
                }//  w  w w  . j  a  va2  s .co m
            }
        }
    }
    return map;
}

From source file:org.apache.hadoop.gateway.topology.file.FileTopologyProviderTest.java

private FileObject createDir(String name) throws FileSystemException {
    FileSystemManager fsm = VFS.getManager();
    FileObject dir = fsm.resolveFile(name);
    dir.createFolder();/*  w  w  w . j a  v a  2  s .  c o  m*/
    assertTrue("Failed to create test dir " + dir.getName().getFriendlyURI(), dir.exists());
    return dir;
}

From source file:org.apache.hadoop.gateway.topology.file.FileTopologyProviderTest.java

private FileObject createFile(FileObject parent, String name, String resource, long timestamp)
        throws IOException {
    FileObject file = parent.resolveFile(name);
    if (!file.exists()) {
        file.createFile();//from   w ww.  j  a  v a2s.c  o m
    }
    InputStream input = ClassLoader.getSystemResourceAsStream(resource);
    OutputStream output = file.getContent().getOutputStream();
    IOUtils.copy(input, output);
    output.flush();
    input.close();
    output.close();
    file.getContent().setLastModifiedTime(timestamp);
    assertTrue("Failed to create test file " + file.getName().getFriendlyURI(), file.exists());
    assertTrue("Failed to populate test file " + file.getName().getFriendlyURI(),
            file.getContent().getSize() > 0);

    //    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    //    IOUtils.copy( file.getContent().getInputStream(), buffer );
    //    System.out.println( new String( buffer.toString( "UTF-8" ) ) );

    return file;
}