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:functionaltests.dataspaces.TestGlobalSpace.java

@Test
public void testGlobalSpace() throws Throwable {

    File in = tmpFolder.newFolder("input_space");
    String inPath = in.getAbsolutePath();

    File out = tmpFolder.newFolder("output_space");
    String outPath = out.getAbsolutePath();

    writeFiles(inFiles, inPath);/*from w w  w . j av  a2  s.  c  om*/

    TaskFlowJob job = new TaskFlowJob();
    job.setName(this.getClass().getSimpleName());
    job.setInputSpace(in.toURI().toURL().toString());
    job.setOutputSpace(out.toURI().toURL().toString());

    JavaTask A = new JavaTask();
    A.setExecutableClassName("org.ow2.proactive.scheduler.examples.EmptyTask");
    A.setName("A");
    for (String[] file : inFiles) {
        A.addInputFiles(file[0], InputAccessMode.TransferFromInputSpace);
        A.addOutputFiles(file[0] + ".glob.A", OutputAccessMode.TransferToGlobalSpace);
    }
    A.setPreScript(new SimpleScript(scriptA, "groovy"));
    A.setForkEnvironment(new ForkEnvironment());
    job.addTask(A);

    JavaTask B = new JavaTask();
    B.setExecutableClassName("org.ow2.proactive.scheduler.examples.EmptyTask");
    B.setName("B");
    B.addDependence(A);
    for (String[] file : inFiles) {
        B.addInputFiles(file[0] + ".glob.A", InputAccessMode.TransferFromGlobalSpace);
        B.addOutputFiles(file[0] + ".out", OutputAccessMode.TransferToOutputSpace);
    }
    B.setPreScript(new SimpleScript(scriptB, "groovy"));
    B.setForkEnvironment(new ForkEnvironment());
    job.addTask(B);

    Scheduler scheduler = schedulerHelper.getSchedulerInterface();
    JobId id = scheduler.submit(job);

    schedulerHelper.waitForEventJobFinished(id);
    assertFalse(schedulerHelper.getJobResult(id).hadException());

    /**
     * check: inFiles > IN > LOCAL A > GLOBAL > LOCAL B > OUT 
     */
    for (String[] inFile : inFiles) {
        File f = new File(outPath + File.separator + inFile[0] + ".out");
        assertTrue("File does not exist: " + f.getAbsolutePath(), f.exists());
        Assert.assertEquals("Original and copied files differ", inFile[1], FileUtils.readFileToString(f));
        f.delete();
        File inf = new File(inPath + File.separator + inFile[0]);
        inf.delete();
    }

    /**
     * check that the file produced is accessible in the global user space via the scheduler API
     */
    String globalURI = scheduler.getGlobalSpaceURIs().get(0);
    assertTrue(globalURI.startsWith("file:"));
    String globalPath = new File(new URI(globalURI)).getAbsolutePath();

    FileSystemManager fsManager = VFSFactory.createDefaultFileSystemManager();
    for (String[] file : inFiles) {
        FileObject outFile = fsManager.resolveFile(globalURI + "/" + file[0] + ".glob.A");
        log("Checking existence of " + outFile.getURL());
        assertTrue(outFile.getURL() + " exists", outFile.exists());

        File outFile2 = new File(globalPath, file[0] + ".glob.A");
        log("Checking existence of " + outFile2);
        assertTrue(outFile2 + " exists", outFile2.exists());
    }
}

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

protected String readCode(DesignMetadata md) throws FileNotFoundException, IOException, WGDesignSyncException,
        InstantiationException, IllegalAccessException {

    // No, filecontainers have no code, but thanks for asking....
    if (getType() == WGDocument.TYPE_FILECONTAINER) {
        return null;
    }//from ww  w . ja  va 2  s  . c o m

    FileObject codeFile = getCodeFile();
    if (!codeFile.exists()) {
        throw new WGDesignSyncException("Code of file '" + getCodeFile().getName().getPath()
                + "' could not be read because the file does not exist.");
    }

    LineNumberReader reader = new LineNumberReader(createReader(codeFile));
    StringWriter writer = new StringWriter();
    int headerLines = 0;
    try {
        String line;
        boolean lookForHeaders = true;
        boolean firstLine = true;

        while ((line = reader.readLine()) != null) {
            if (lookForHeaders == true && line.startsWith("##")) {
                processDesignHeader(line, md.getInfo());
                headerLines++;
            } else {
                lookForHeaders = false;

                if (!firstLine) {
                    writer.write("\n");
                } else {
                    firstLine = false;
                }

                writer.write(line);
            }
        }
    } finally {
        reader.close();
        codeFile.getContent().close();
    }
    writer.close();
    md.setHeaderLines(headerLines);
    String code = writer.toString();
    return code;
}

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

/**
 * Appends the specified FileObjects to the list of FileObjects to search
 * for classes and resources.//from   w w  w  .  j  a  v  a 2 s.  c  om
 *
 * @param manager The FileSystemManager.
 * @param files the FileObjects to append to the search path.
 * @throws FileSystemException if an error occurs.
 */
private void addFileObjects(final FileSystemManager manager, final FileObject[] files)
        throws FileSystemException {
    for (int i = 0; i < files.length; i++) {
        FileObject file = files[i];
        if (!file.exists()) {
            // Does not exist - skip
            continue;
        }

        // TODO - use federation instead
        if (file.getType().hasContent() && manager.canCreateFileSystem(file)) {
            // Use contents of the file
            file = manager.createFileSystem(file);
        }

        resources.add(file);
    }
}

From source file:com.sludev.commons.vfs2.provider.azure.AzFileProviderTest.java

/**
 * By default FileObject.getChildren() will use doListChildrenResolved() if available
 * /*ww  w .j  ava2 s. co m*/
 * @throws Exception 
 */
@Test
public void A005_listChildren() throws Exception {
    String currAccountStr = testProperties.getProperty("azure.account.name");
    String currKey = testProperties.getProperty("azure.account.key");
    String currContainerStr = testProperties.getProperty("azure.test0001.container.name");
    String currHost = testProperties.getProperty("azure.host"); // <account>.blob.core.windows.net

    DefaultFileSystemManager currMan = new DefaultFileSystemManager();
    currMan.addProvider(AzConstants.AZSBSCHEME, new AzFileProvider());
    currMan.init();

    StaticUserAuthenticator auth = new StaticUserAuthenticator("", currAccountStr, currKey);
    FileSystemOptions opts = new FileSystemOptions();
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

    String currFileNameStr = "uploadFile02";
    String currUriStr = String.format("%s://%s/%s/%s", AzConstants.AZSBSCHEME, currHost, currContainerStr,
            currFileNameStr);
    FileObject currFile = currMan.resolveFile(currUriStr, opts);

    FileObject[] currObjs = currFile.getChildren();
    for (FileObject obj : currObjs) {
        FileName currName = obj.getName();
        Boolean res = obj.exists();
        FileType ft = obj.getType();

        log.info(String.format("\nNAME.PATH : '%s'\nEXISTS : %b\nTYPE : %s\n\n", currName.getPath(), res, ft));
    }
}

From source file:de.innovationgate.wgpublisher.design.sync.FileContainerDeployment.java

public void performUpdate(WGDatabase db)
        throws IOException, WGException, InstantiationException, IllegalAccessException, WGDesignSyncException {

    FileObject codeFile = getCodeFile();
    // Check if file has been deleted in the meantime
    if (!codeFile.exists()) {
        return;/*from  w  w w .j a  v a2  s.  c om*/
    }

    WGFileContainer con = (WGFileContainer) db.getDocumentByDocumentKey(getDocumentKey());
    if (con == null) {
        WGDocumentKey docKey = new WGDocumentKey(getDocumentKey());
        con = db.createFileContainer(docKey.getName());
        con.save();
    }

    // Find new and updated files
    FileObject[] filesArray = codeFile.getChildren();
    if (filesArray == null) {
        throw new WGDesignSyncException("Cannot collect files from directory '"
                + codeFile.getName().getPathDecoded() + "'. Please verify directory existence.");
    }

    List<FileObject> files = new ArrayList<FileObject>(Arrays.asList(filesArray));
    Collections.sort(files, new FileNameComparator());

    FileObject file;
    Map existingFiles = new HashMap();
    for (int i = 0; i < files.size(); i++) {
        file = (FileObject) files.get(i);
        if (isExcludedFileContainerFile(file)) {
            continue;
        }

        ContainerFile containerFile = getContainerFile(file);
        if (containerFile != null) {
            if (existingFiles.containsKey(containerFile.getName())) {
                // Possible in case-sensitive file systems. Another file of the same name with another case is in the dir.
                continue;
            }

            if (file.getContent().getLastModifiedTime() != containerFile.getTimestamp()) {
                doRemoveFile(con, file.getName().getBaseName());
                doSaveDocument(con);
                doAttachFile(con, file);
                doSaveDocument(con);
                containerFile.setTimestamp(file.getContent().getLastModifiedTime());
            }
        } else {
            containerFile = new ContainerFile(file);
            if (con.hasFile(file.getName().getBaseName())) {
                doRemoveFile(con, file.getName().getBaseName());
                doSaveDocument(con);
            }
            doAttachFile(con, file);
            doSaveDocument(con);
        }
        existingFiles.put(containerFile.getName(), containerFile);
    }

    // Remove deleted files from container
    _files = existingFiles;
    List fileNamesInContainer = WGUtils.toLowerCase(con.getFileNames());
    fileNamesInContainer.removeAll(existingFiles.keySet());
    Iterator deletedFiles = fileNamesInContainer.iterator();
    while (deletedFiles.hasNext()) {
        con.removeFile((String) deletedFiles.next());
        con.save();
    }

    // Update metadata and save
    FCMetadata metaData = (FCMetadata) readMetaData();
    metaData.writeToDocument(con);
    con.save();
    FileObject metadataFile = getMetadataFile();
    if (metadataFile.exists()) {
        _timestampOfMetadataFile = metadataFile.getContent().getLastModifiedTime();
    }

    // We wont do this here, since this would rebuild all ContainerFiles, which is not neccessary
    // Instead we have updated just the metadata file timestamp
    // resetUpdateInformation();
}

From source file:hadoopInstaller.installation.UploadConfiguration.java

private void uploadConfiguration(FileObject remoteDirectory, Host host) throws InstallationError {
    try {/*  w  w  w .  j  a  va 2  s  .co  m*/
        FileObject configurationDirectory = remoteDirectory.resolveFile("hadoop/etc/hadoop/"); //$NON-NLS-1$
        if (deleteOldFiles) {
            configurationDirectory.delete(new AllFileSelector());
            log.debug("HostInstallation.Upload.DeletingOldFiles", //$NON-NLS-1$
                    host.getHostname());
        } else if (!configurationDirectory.exists()) {
            throw new InstallationError("HostInstallation.Upload.NotDeployed"); //$NON-NLS-1$
        }
        configurationDirectory.copyFrom(filesToUpload, new AllFileSelector());
        modifyEnvShFile(host, configurationDirectory, InstallerConstants.ENV_FILE_HADOOP);
        modifyEnvShFile(host, configurationDirectory, InstallerConstants.ENV_FILE_YARN);
        try {
            configurationDirectory.close();
        } catch (FileSystemException ex) {
            log.warn("HostInstallation.CouldNotClose", //$NON-NLS-1$
                    configurationDirectory.getName().getURI());
        }
    } catch (FileSystemException e) {
        throw new InstallationError(e, "HostInstallation.Upload.Error", //$NON-NLS-1$
                remoteDirectory.getName().getURI());
    }
}

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

public List<String> getDesignNames() throws WGADesignRetrievalException {

    try {//  ww w.  j  ava  2s  .  com
        List<String> designs = new ArrayList<String>();

        // Add child design directories - All with a syncinfo/design.xml or those that are completely empty and can be initialized
        _dir.refresh();
        FileObject[] children = _dir.getChildren();
        for (int i = 0; i < children.length; i++) {
            FileObject child = children[i];
            if (child.getType().equals(FileType.FOLDER)) {
                FileObject resolvedChild = WGUtils.resolveDirLink(child);
                if (resolvedChild.getType().equals(FileType.FOLDER)) {
                    FileObject syncInfo = DesignDirectory.getDesignDefinitionFile(resolvedChild);
                    if (syncInfo != null || child.getChildren().length == 0) {
                        designs.add(child.getName().getBaseName());
                    }
                }
            } else if (child.getType().equals(FileType.FILE)
                    && child.getName().getExtension().equalsIgnoreCase(ARCHIVED_DESIGN_EXTENSION)) {
                designs.add(DESIGNNAMEPREFIX_ARCHIVE + child.getName().getBaseName().substring(0,
                        child.getName().getBaseName().lastIndexOf(".")));
            }
        }

        // Add additional directories
        Iterator<Map.Entry<String, String>> dirs = _additionalDirs.entrySet().iterator();
        while (dirs.hasNext()) {
            Map.Entry<String, String> entry = dirs.next();
            FileObject dir = VFS.getManager().resolveFile((String) entry.getValue());
            if (dir.exists() && dir.getType().equals(FileType.FOLDER)) {
                FileObject syncInfo = DesignDirectory.getDesignDefinitionFile(dir);
                if (syncInfo != null || dir.getChildren().length == 0) {
                    designs.add(DESIGNNAMEPREFIX_ADDITIONALDIR + entry.getKey());
                }
            }
        }

        return designs;
    } catch (FileSystemException e) {
        throw new WGADesignRetrievalException("Exception retrieving file system designs", e);
    }

}

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

protected static FileObject createConflictFile(FileObject targetFile) throws FileSystemException {
    String conflictFileName = targetFile.getName().getBaseName() + "_ovlconflict."
            + targetFile.getName().getExtension();
    targetFile = targetFile.getParent().resolveFile(conflictFileName);
    if (targetFile.exists()) {
        targetFile.delete();/* ww w. j a  v  a2  s. c  o  m*/
    }
    return targetFile;
}

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

protected DesignMetadata readMetaData() throws FileNotFoundException, IOException, InstantiationException,
        IllegalAccessException, WGDesignSyncException {

    FileObject metadataFile = getMetadataFile();

    // If not exists we return a default
    if (!metadataFile.exists()) {
        return createDefaultMetadata();
    }/*  ww  w .  j  a  v a2s .c  om*/

    // If the file exists, but is empty, we put default metadata information into it
    if (metadataFile.getContent().getSize() == 0) {
        createMetadataFile(metadataFile);
    }

    String metaXML;
    Reader reader = createReader(metadataFile);
    try {
        metaXML = WGUtils.readString(reader);
    } finally {
        reader.close();
        metadataFile.close();
    }

    DesignMetadata metaData;
    if (metaXML.trim().equals("")) {
        createMetadataFile(metadataFile);
        metaData = createDefaultMetadata();
    } else {
        DesignMetadataInfo info = (DesignMetadataInfo) DesignSyncManager.getXstream().fromXML(metaXML);
        if (info instanceof TMLMetadataInfo) {
            metaData = new TMLMetadata();
        } else if (info instanceof FCMetadataInfo) {
            metaData = new FCMetadata();
        } else if (info instanceof ScriptMetadataInfo) {
            metaData = new ScriptMetadata();
        } else {
            metaData = new DesignMetadata();
        }
        metaData.setInfo(info);
    }

    return metaData;
}

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

/**
 * Searches through the search path of for the first class or resource
 * with specified name.//w  w  w .jav a  2  s. com
 * @param name The resource to load.
 * @return The Resource.
 * @throws FileSystemException if an error occurs.
 */
private Resource loadResource(final String name) throws FileSystemException {
    final Iterator<FileObject> it = resources.iterator();
    while (it.hasNext()) {
        final FileObject baseFile = it.next();
        final FileObject file = baseFile.resolveFile(name, NameScope.DESCENDENT_OR_SELF);
        if (file.exists()) {
            return new Resource(name, baseFile, file);
        }
    }

    return null;
}