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

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

Introduction

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

Prototype

public boolean exists() throws FileSystemException;

Source Link

Document

Determines if this file exists.

Usage

From source file:com.pongasoft.kiwidoc.builder.JSONContentHandler.java

/**
 * Loads the content of the given resource.
 *
 * @param resource the resource to load//from   w w  w . ja v  a  2  s.co m
 * @return the raw content in json format (never <code>null</code>)
 * @throws NoSuchContentException if the content does not exist
 * @throws StoreException         if there is a problem reading the content.
 */
public Map<String, Object> loadRawContent(Resource resource) throws NoSuchContentException, StoreException {
    if (resource == null)
        throw new NoSuchContentException(resource);

    try {
        FileObject contentFile = getContentFile(resource);
        if (!contentFile.exists())
            throw new NoSuchContentException(resource);
        String content = IOUtils.readContentAsString(contentFile);

        if (log.isDebugEnabled())
            log.debug("Loaded " + resource + " : " + content.length());

        return JsonCodec.fromJsonStringToMap(content);
    } catch (IOException e) {
        throw new StoreException(e);
    }
}

From source file:com.newatlanta.appengine.junit.vfs.gae.GaeFolderTestCase.java

private FileObject testRootObject() throws Exception {
    FileObject rootObject = GaeVFS.resolveFile("/");
    assertTrue(rootObject.exists());
    assertEquals(rootObject.getName().getScheme(), "gae");
    assertFolder(rootObject);//from w w  w .j a v  a2  s.c om
    assertNull(rootObject.getParent());
    assertChildren(rootObject, rootChildren);

    // TODO: test rootObject.getName() and FileName methods

    assertEntity(rootObject);
    return rootObject;
}

From source file:com.panet.imeta.job.entries.fileexists.JobEntryFileExists.java

public Result execute(Result previousResult, int nr, Repository rep, Job parentJob) {
    LogWriter log = LogWriter.getInstance();
    Result result = previousResult;
    result.setResult(false);// www. ja v  a 2  s  . co  m

    if (filename != null) {
        String realFilename = getRealFilename();
        try {
            FileObject file = KettleVFS.getFileObject(realFilename);
            if (file.exists() && file.isReadable()) {
                log.logDetailed(toString(), Messages.getString("JobEntryFileExists.File_Exists", realFilename)); //$NON-NLS-1$
                result.setResult(true);
            } else {
                log.logDetailed(toString(),
                        Messages.getString("JobEntryFileExists.File_Does_Not_Exist", realFilename)); //$NON-NLS-1$
            }
        } catch (IOException e) {
            result.setNrErrors(1);
            log.logError(toString(),
                    Messages.getString("JobEntryFileExists.ERROR_0004_IO_Exception", e.toString())); //$NON-NLS-1$
        }
    } else {
        result.setNrErrors(1);
        log.logError(toString(), Messages.getString("JobEntryFileExists.ERROR_0005_No_Filename_Defined")); //$NON-NLS-1$
    }

    return result;
}

From source file:com.panet.imeta.job.entry.validator.FileExistsValidator.java

public boolean validate(CheckResultSourceInterface source, String propertyName,
        List<CheckResultInterface> remarks, ValidatorContext context) {

    String filename = ValidatorUtils.getValueAsString(source, propertyName);
    VariableSpace variableSpace = getVariableSpace(source, propertyName, remarks, context);
    boolean failIfDoesNotExist = getFailIfDoesNotExist(source, propertyName, remarks, context);

    if (null == variableSpace) {
        return false;
    }/*from www  .  j av a2  s.c o m*/

    String realFileName = variableSpace.environmentSubstitute(filename);
    FileObject fileObject = null;
    try {
        fileObject = KettleVFS.getFileObject(realFileName);
        if (fileObject == null || (fileObject != null && !fileObject.exists() && failIfDoesNotExist)) {
            JobEntryValidatorUtils.addFailureRemark(source, propertyName, VALIDATOR_NAME, remarks,
                    JobEntryValidatorUtils.getLevelOnFail(context, VALIDATOR_NAME));
            return false;
        }
        try {
            fileObject.close(); // Just being paranoid
        } catch (IOException ignored) {
        }
    } catch (Exception e) {
        JobEntryValidatorUtils.addExceptionRemark(source, propertyName, VALIDATOR_NAME, remarks, e);
        return false;
    }
    return true;
}

From source file:com.newatlanta.appengine.junit.vfs.gae.GaeFolderTestCase.java

private FileObject testCreateTestFolder(FileObject rootObject) throws Exception {
    FileObject testFolder = GaeVFS.resolveFile("testFolder");
    assertFalse(testFolder.exists());
    testFolder.createFolder();/*from ww  w  .j  a  v  a2 s.  c  om*/
    assertFolder(testFolder);
    assertEquals(rootObject, testFolder.getParent());
    assertTrue(testFolder.getName().isAncestor(rootObject.getName()));
    assertTrue(rootObject.getName().isDescendent(testFolder.getName()));
    assertChildren(rootObject, rootChildren);
    assertEntity(rootObject);
    return testFolder;
}

From source file:com.panet.imeta.core.playlist.FilePlayListReplay.java

private void initializeCurrent(FileObject file, String filePart) throws KettleException {
    try {/*from   ww  w . j av a  2 s .c o  m*/
        FileObject lineFile = AbstractFileErrorHandler.getReplayFilename(lineNumberDirectory,
                file.getName().getBaseName(), replayDate, lineNumberExtension, filePart);
        if (lineFile.exists())
            currentLineNumberFile = new FilePlayListReplayLineNumberFile(lineFile, encoding, file, filePart);
        else
            currentLineNumberFile = new FilePlayListReplayFile(file, filePart);

        FileObject errorFile = AbstractFileErrorHandler.getReplayFilename(errorDirectory,
                file.getName().getURI(), replayDate, errorExtension, AbstractFileErrorHandler.NO_PARTS);
        if (errorFile.exists())
            currentErrorFile = new FilePlayListReplayErrorFile(errorFile, file);
        else
            currentErrorFile = new FilePlayListReplayFile(file, AbstractFileErrorHandler.NO_PARTS);
    } catch (IOException e) {
        throw new KettleException(e);
    }
}

From source file:com.newatlanta.appengine.junit.vfs.gae.GaeFolderTestCase.java

public void testFolders() throws Exception {
    // root object
    FileObject rootObject = testRootObject();

    // create folder
    FileObject testFolder = testCreateTestFolder(rootObject);

    // create sub-folders
    FileObject subFolder = testFolder.resolveFile("abc/def/ghi");
    assertFalse(subFolder.exists());
    subFolder.createFolder();//from w w w .  j  a  v a  2  s  . c  o m
    assertFolder(subFolder);
    assertSubFolders(testFolder, new String[] { "abc/def/ghi", "abc/def", "abc" });
    assertEntity(rootObject);

    // rename
    FileObject srcFolder = testFolder.resolveFile("abc/def");
    FileObject destFolder = testFolder.resolveFile("abc/xyz");
    assertTrue(testFolder.canRenameTo(destFolder));
    srcFolder.moveTo(destFolder);
    assertFolder(destFolder);
    assertFalse(srcFolder.exists());
    assertSubFolders(testFolder, new String[] { "abc/xyz/ghi", "abc/xyz", "abc" });

    // TODO: test moving a folder to a different parent

    // delete
    assertTrue(testFolder.exists());
    testFolder.delete(Selectors.SELECT_ALL);
    assertFalse(testFolder.exists());
}

From source file:com.newatlanta.appengine.vfs.provider.GaeFileSystemManager.java

/**
 * Resolves a URI, relative to a base file with the specified FileSystem
 * configuration options.//from   w w  w . j  av a  2  s.c  o  m
 */
@Override
public FileObject resolveFile(FileObject baseFile, String uri, FileSystemOptions opts)
        throws FileSystemException {
    // let the specified provider handle it
    if (!isCombinedLocal || isSchemeSpecified(uri)) {
        return super.resolveFile(baseFile, uri, opts);
    }

    FileObject localFile;
    FileObject gaeFile;

    if (baseFile != null) {
        // if uri starts with "/", determine if it includes the base path;
        // if it doesn't, then remove the leading "/" to create a relative
        // path; this is required to properly resolve "file://"
        uri = checkRelativity(baseFile, uri);

        FileObject fileObject = super.resolveFile(baseFile, uri, opts);
        if (fileObject.exists() && (fileObject.getType().hasContent())) {
            return fileObject; // return existing file
        }
        // fileObject doesn't exist or is a folder, check other file system
        if (fileObject.getName().getScheme().equals("gae")) {
            gaeFile = fileObject;
            FileName baseName = baseFile.getName();
            if (baseName instanceof GaeFileName) {
                String localUri = "file://" + ((GaeFileName) baseName).getRootPath() + baseName.getPath() + "/"
                        + uri;
                localFile = super.resolveFile(null, localUri, opts);
            } else {
                localFile = super.resolveFile(baseFile, "file://" + uri, opts);
            }
            if (localFile.exists() && (localFile.getType().hasContent())) {
                return localFile; // return existing local files
            }
        } else {
            localFile = fileObject;
            gaeFile = super.resolveFile(baseFile, "gae://" + uri, opts);
        }
    } else {
        // neither scheme nor baseFile specified, check local first
        localFile = super.resolveFile(null, uri, opts);
        if (localFile.exists() && (localFile.getType().hasContent())) {
            return localFile; // return existing local files
        }
        // localFile doesn't exist or is a folder, check GAE file system
        gaeFile = super.resolveFile(null, "gae://" + uri, opts);
    }

    ((GaeFileObject) gaeFile).setCombinedLocal(true);

    // when we get here we either have a non-existing file, or a folder;
    // return the GAE file/folder if it exists
    if (gaeFile.exists()) {
        return gaeFile;
    }

    // never return local folders
    if (localFile.exists()) {
        gaeFile.createFolder(); // create GAE "shadow" for existing local folder
        return gaeFile;
    }
    return gaeFile; // neither local nor GAE file/folder exists
}

From source file:be.ibridge.kettle.job.entry.createfile.JobEntryCreateFile.java

public Result execute(Result prev_result, int nr, Repository rep, Job parentJob) {
    LogWriter log = LogWriter.getInstance();
    Result result = new Result(nr);
    result.setResult(false);/*w  w  w .  j  a  va2  s  .  c om*/

    if (filename != null) {
        String realFilename = getRealFilename();
        FileObject fileObject = null;
        try {
            fileObject = KettleVFS.getFileObject(realFilename);

            if (fileObject.exists()) {
                if (isFailIfFileExists()) {
                    // File exists and fail flag is on.
                    result.setResult(false);
                    log.logError(toString(), "File [" + realFilename + "] exists, failing.");
                } else {
                    // File already exists, no reason to try to create it
                    result.setResult(true);
                    log.logBasic(toString(), "File [" + realFilename + "] already exists, not recreating.");
                }
            } else {
                //  No file yet, create an empty file.
                fileObject.createFile();
                log.logBasic(toString(), "File [" + realFilename + "] created!");
                result.setResult(true);
            }
        } catch (IOException e) {
            log.logError(toString(),
                    "Could not create file [" + realFilename + "], exception: " + e.getMessage());
            result.setResult(false);
            result.setNrErrors(1);
        } finally {
            if (fileObject != null) {
                try {
                    fileObject.close();
                } catch (IOException ex) {
                }
                ;
            }
        }
    } else {
        log.logError(toString(), "No filename is defined.");
    }

    return result;
}

From source file:com.thinkberg.vfs.s3.tests.S3FileProviderTest.java

public void testGetFolder() throws FileSystemException {
    FileObject object = ROOT.resolveFile(FOLDER);
    assertTrue(object.exists());
    assertEquals(FileType.FOLDER, object.getType());
}