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

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

Introduction

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

Prototype

boolean exists() throws FileSystemException;

Source Link

Document

Determines if this file exists.

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  a  2 s  . c  om
    }
    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 ww  w .  j  a v a  2 s  . 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:fi.mystes.synapse.mediator.vfs.VFSTestHelper.java

public static void createDirectory(String dirPath) throws FileSystemException {
    FileObject dir = VFS.getManager().resolveFile(dirPath);
    if (!dir.exists()) {
        dir.createFolder();/*  w w w  .j  ava  2s  . co  m*/
    }
}

From source file:com.yenlo.synapse.transport.vfs.VFSUtils.java

/**
 * Release a file item lock acquired either by the VFS listener or a sender
 *
 * @param fsManager which is used to resolve the processed file
 * @param fo representing the processed file
 *//*from  w w w  . ja  v a  2 s  .c  om*/
public static void releaseLock(FileSystemManager fsManager, FileObject fo) {
    try {
        String fullPath = fo.getName().getURI();
        int pos = fullPath.indexOf("?");
        if (pos > -1) {
            fullPath = fullPath.substring(0, pos);
        }
        FileObject lockObject = fsManager.resolveFile(fullPath + ".lock");
        if (lockObject.exists()) {
            lockObject.delete();
        }
    } catch (FileSystemException e) {
        log.error("Couldn't release the lock for the file : " + fo.getName() + " after processing");
    }
}

From source file:com.yenlo.synapse.transport.vfs.VFSUtils.java

/**
 * Acquires a file item lock before processing the item, guaranteing that the file is not
 * processed while it is being uploaded and/or the item is not processed by two listeners
 *
 * @param fsManager used to resolve the processing file
 * @param fo representing the processing file item
 * @return boolean true if the lock has been acquired or false if not
 *//*from   w ww  .  j a va2  s.c o m*/
public synchronized static boolean acquireLock(FileSystemManager fsManager, FileObject fo) {

    // generate a random lock value to ensure that there are no two parties
    // processing the same file
    Random random = new Random();
    byte[] lockValue = String.valueOf(random.nextLong()).getBytes();

    try {
        // check whether there is an existing lock for this item, if so it is assumed
        // to be processed by an another listener (downloading) or a sender (uploading)
        // lock file is derived by attaching the ".lock" second extension to the file name
        String fullPath = fo.getName().getURI();
        int pos = fullPath.indexOf("?");
        if (pos != -1) {
            fullPath = fullPath.substring(0, pos);
        }
        FileObject lockObject = fsManager.resolveFile(fullPath + ".lock");
        if (lockObject.exists()) {
            log.debug("There seems to be an external lock, aborting the processing of the file " + fo.getName()
                    + ". This could possibly be due to some other party already "
                    + "processing this file or the file is still being uploaded");
        } else {

            // write a lock file before starting of the processing, to ensure that the
            // item is not processed by any other parties
            lockObject.createFile();
            OutputStream stream = lockObject.getContent().getOutputStream();
            try {
                stream.write(lockValue);
                stream.flush();
                stream.close();
            } catch (IOException e) {
                lockObject.delete();
                log.error("Couldn't create the lock file before processing the file " + fullPath, e);
                return false;
            } finally {
                lockObject.close();
            }

            // check whether the lock is in place and is it me who holds the lock. This is
            // required because it is possible to write the lock file simultaneously by
            // two processing parties. It checks whether the lock file content is the same
            // as the written random lock value.
            // NOTE: this may not be optimal but is sub optimal
            FileObject verifyingLockObject = fsManager.resolveFile(fullPath + ".lock");
            if (verifyingLockObject.exists() && verifyLock(lockValue, verifyingLockObject)) {
                return true;
            }
        }
    } catch (FileSystemException fse) {
        log.error("Cannot get the lock for the file : " + maskURLPassword(fo.getName().getURI())
                + " before processing");
    }
    return false;
}

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

public static FileObject getLatest(final FileObject parent) throws FileSystemException {
    try {/*from  ww  w .ja  v  a 2s  .co m*/
        final FileObject[] children = parent.getChildren();
        if (children == null || children.length == 0)
            return null;
        return Arrays.stream(children).reduce(null, findLatest, findLatest);
    } catch (final FileNotFoundException | FileNotFolderException afnfe) {
        if (parent.exists())
            throw afnfe;
        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/*from ww w  .j a  va2  s .c o m*/
 * @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:com.googlecode.vfsjfilechooser2.utils.VFSUtils.java

/**
 * Tells whether a file exists/*  w w w.j  av  a  2s.c  o m*/
 * @param fileObject A file representation
 * @return whether a file exists
 */
public static boolean exists(FileObject fileObject) {
    if (fileObject == null) {
        return false;
    }

    try {
        return fileObject.exists();
    } catch (FileSystemException ex) {
        return false;
    }
}

From source file:fr.cls.atoll.motu.processor.wps.TestServiceMetadata.java

public static String[] getServiceMetadataSchemaAsString(String schemaPath) throws MotuException {

    List<String> stringList = new ArrayList<String>();
    String localIso19139SchemaPath = "file:///c:/tempVFS/testISO";
    String localIso19139RootSchemaRelPath = "/srv/srv.xsd";
    String localIso19139RootSchemaPath = String.format("%s%s", localIso19139SchemaPath,
            localIso19139RootSchemaRelPath);

    FileObject dest = Organizer.resolveFile(localIso19139RootSchemaPath);
    boolean hasIso19139asLocalSchema = false;
    try {//from w w  w  .  j  a v a  2s  . c  o  m
        if (dest != null) {
            hasIso19139asLocalSchema = dest.exists();
        }
    } catch (FileSystemException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    if (hasIso19139asLocalSchema) {
        try {
            dest.close();
        } catch (FileSystemException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    } else {

        // URL url = Organizer.findResource("schema/iso/srv/srv.xsd");
        // URL url =
        // Organizer.findResource("J:/dev/iso/19139/20070417/schema/src/main/resources/iso/19139/20070417/srv/srv.xsd");
        // URL url = Organizer.findResource("iso/19139/20070417/srv/srv.xsd");
        URL url = Organizer.findResource(schemaPath);
        System.out.println(url);

        // String[] arr = url.toString().split("!");

        // FileObject jarFile = Organizer.resolveFile(arr[0]);
        FileObject jarFile = Organizer.resolveFile(url.toString());

        // List the children of the Jar file
        FileObject[] children = null;
        try {
            children = jarFile.getChildren();
        } catch (FileSystemException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("Children of " + jarFile.getName().getURI());
        for (int i = 0; i < children.length; i++) {
            System.out.println(children[i].getName().getBaseName());
        }

        dest = Organizer.resolveFile(localIso19139SchemaPath);
        Organizer.deleteDirectory(dest);

        Organizer.copyFile(jarFile, dest);
    }

    // stringList.add(url.toString());
    // stringList.add("J:/dev/iso/19139/20070417/schema/src/main/resources/iso/19139/20070417/srv/srv.xsd");
    stringList.add(localIso19139RootSchemaPath);
    // stringList.add("C:/Documents and Settings/dearith/Mes documents/Atoll/SchemaIso/srv/serviceMetadata.xsd");
    String[] inS = new String[stringList.size()];
    inS = stringList.toArray(inS);
    return inS;
}

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

@Test
public void testXOM() {
    Document doc = null;//from  www  . ja va  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;
    }
}