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

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

Introduction

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

Prototype

public void createFile() throws FileSystemException;

Source Link

Document

Creates this file, if it does not exist.

Usage

From source file:org.pentaho.di.core.vfs.KettleVFS.java

public static OutputStream getOutputStream(FileObject fileObject, boolean append) throws IOException {
    FileObject parent = fileObject.getParent();
    if (parent != null) {
        if (!parent.exists()) {
            throw new IOException(BaseMessages.getString(PKG, "KettleVFS.Exception.ParentDirectoryDoesNotExist",
                    getFilename(parent)));
        }/* w  w  w  .  ja  v  a  2 s .  co  m*/
    }
    try {
        fileObject.createFile();
        FileContent content = fileObject.getContent();
        return content.getOutputStream(append);
    } catch (FileSystemException e) {
        // Perhaps if it's a local file, we can retry using the standard
        // File object. This is because on Windows there is a bug in VFS.
        //
        if (fileObject instanceof LocalFile) {
            try {
                String filename = getFilename(fileObject);
                return new FileOutputStream(new File(filename), append);
            } catch (Exception e2) {
                throw e; // throw the original exception: hide the retry.
            }
        } else {
            throw e;
        }
    }
}

From source file:org.pentaho.di.job.entries.createfile.JobEntryCreateFile.java

public Result execute(Result previousResult, int nr) throws KettleException {
    Result result = previousResult;
    result.setResult(false);/*  w ww .  j  a va  2  s.c  o m*/

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

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

    return result;
}

From source file:org.pentaho.di.job.entries.hadooptransjobexecutor.DistributedCacheUtilTest.java

private FileObject createTestFolderWithContent(String rootFolderName) throws Exception {
    String rootName = "bin/test/" + rootFolderName;
    FileObject root = KettleVFS.getFileObject(rootName);
    FileObject jar1 = KettleVFS.getFileObject(rootName + Const.FILE_SEPARATOR + "jar1.jar");
    FileObject jar2 = KettleVFS.getFileObject(rootName + Const.FILE_SEPARATOR + "jar2.jar");
    FileObject folder = KettleVFS.getFileObject(rootName + Const.FILE_SEPARATOR + "folder");
    FileObject file = KettleVFS//www .  j a  v a 2s .com
            .getFileObject(rootName + Const.FILE_SEPARATOR + "folder" + Const.FILE_SEPARATOR + "file.txt");

    root.createFolder();
    folder.createFolder();
    jar1.createFile();
    jar2.createFile();
    file.createFile();

    return root;
}

From source file:org.pentaho.s3.S3Test.java

@Test
public void listChildren() throws Exception {
    assertNotNull("FileSystemManager is null", fsManager);

    FileObject bucket = fsManager.resolveFile(buildS3URL("/mdamour_list_children_test"));
    // assertEquals(false, bucket.exists());
    bucket.createFolder();//ww w  . ja  v a 2s. co m
    assertEquals(true, bucket.exists());

    FileObject s3FileOut = fsManager.resolveFile(buildS3URL("/mdamour_list_children_test/child01"));
    s3FileOut.createFile();
    OutputStream out = s3FileOut.getContent().getOutputStream();
    out.write(HELLO_S3_STR.getBytes());
    out.close();

    s3FileOut = fsManager.resolveFile(buildS3URL("/mdamour_list_children_test/child02"));
    s3FileOut.createFile();
    out = s3FileOut.getContent().getOutputStream();
    out.write(HELLO_S3_STR.getBytes());
    out.close();

    s3FileOut = fsManager.resolveFile(buildS3URL("/mdamour_list_children_test/child03"));
    s3FileOut.createFile();
    out = s3FileOut.getContent().getOutputStream();
    out.write(HELLO_S3_STR.getBytes());
    out.close();

    bucket = fsManager.resolveFile(buildS3URL("/mdamour_list_children_test"));
    printFileObject(bucket, 0);

    bucket.delete(deleteFileSelector);
    assertEquals(false, bucket.exists());
}

From source file:org.pentaho.s3.S3TestIntegration.java

@Test
public void createDeleteRecursive() throws Exception {
    assertNotNull("FileSystemManager is null", fsManager);

    FileObject bucket = fsManager.resolveFile(buildS3URL("/mdamour_create_delete_test"));
    // assertEquals(false, bucket.exists());
    bucket.createFolder();//w ww  .j  a  v a 2s. c  o m
    assertEquals(true, bucket.exists());

    FileObject s3FileOut = fsManager.resolveFile(buildS3URL("/mdamour_create_delete_test/folder1/folder11"));
    s3FileOut.createFolder();
    assertEquals(true, s3FileOut.exists());

    s3FileOut = fsManager.resolveFile(buildS3URL("/mdamour_create_delete_test/folder1/child"));
    s3FileOut.createFile();
    OutputStream out = s3FileOut.getContent().getOutputStream();
    out.write(HELLO_S3_STR.getBytes());
    out.close();

    s3FileOut = fsManager.resolveFile(buildS3URL("/mdamour_create_delete_test/folder2/child"));
    s3FileOut.createFile();
    out = s3FileOut.getContent().getOutputStream();
    out.write(HELLO_S3_STR.getBytes());
    out.close();

    bucket = fsManager.resolveFile(buildS3URL("/mdamour_create_delete_test"));
    printFileObject(bucket, 0);

    FileObject parentFolder1 = fsManager.resolveFile(buildS3URL("/mdamour_create_delete_test/folder1"));
    parentFolder1.delete(deleteFileSelector);
    assertEquals(false, parentFolder1.exists());

    bucket = fsManager.resolveFile(buildS3URL("/mdamour_create_delete_test"));
    printFileObject(bucket, 0);

    bucket.delete(deleteFileSelector);
    assertEquals(false, bucket.exists());
}

From source file:org.pentaho.s3.S3TestIntegration.java

@Test
public void doGetType() throws Exception {
    assertNotNull("FileSystemManager is null", fsManager);

    FileObject bucket = fsManager.resolveFile(buildS3URL("/mdamour_get_type_test"));
    // assertEquals(false, bucket.exists());
    bucket.createFolder();/*from  w w  w.j a  v a2 s  . c  o m*/
    assertEquals(true, bucket.exists());

    FileObject s3FileOut = fsManager.resolveFile(buildS3URL("/mdamour_get_type_test/child01"));
    s3FileOut.createFile();
    OutputStream out = s3FileOut.getContent().getOutputStream();
    out.write(HELLO_S3_STR.getBytes());
    out.close();
    assertEquals("Is not a folder type", FileType.FOLDER, bucket.getType());
    assertEquals("Is not a file type", FileType.FILE, s3FileOut.getType());

    fsManager.closeFileSystem(bucket.getFileSystem());
    assertEquals("Is not a folder type (after clearing cache)", FileType.FOLDER,
            fsManager.resolveFile(buildS3URL("/mdamour_get_type_test")).getType());
    assertEquals("Is not a file type (after clearing cache)", FileType.FILE,
            fsManager.resolveFile(buildS3URL("/mdamour_get_type_test/child01")).getType());

    bucket = fsManager.resolveFile(buildS3URL("/mdamour_get_type_test"));
    printFileObject(bucket, 0);

    bucket.delete(deleteFileSelector);
    assertEquals(false, bucket.exists());
}

From source file:org.sonatype.gshell.commands.vfs.EditCommand.java

public Object execute(final CommandContext context) throws Exception {
    assert context != null;

    FileObject file = resolveFile(context, path);

    new FileObjectAssert(file).exists().isReadable().isWritable();

    // TODO: Add to FileObjectAssert
    ensureFileHasContent(file);//from  w  w  w.jav  a  2  s .  c o  m

    FileObject tmp = file;

    // If the file is not on the local file system, then create tmp file for editing
    if (!getFileSystemAccess().isLocalFile(file)) {
        // Create a new temporary file, copy the contents for editing
        tmp = resolveFile(context, "tmp:/gshell-edit-" + System.currentTimeMillis() + ".txt");
        log.debug("Using temporary file: {} ({})", tmp, tmp.getClass());
        tmp.createFile();
        tmp.copyFrom(file, Selectors.SELECT_SELF);
    }

    // Have to dereference the VFS file into a local file so the editor can access it
    File localFile = getFileSystemAccess().getLocalFile(tmp);
    Object result = edit(context, localFile);

    // If we had to use a tmp file for editing, then copy back and clean up
    if (tmp != file) {
        log.debug("Updating original file with edited content");
        file.copyFrom(tmp, Selectors.SELECT_SELF);
        tmp.delete();
        FileObjects.close(tmp);
    }

    FileObjects.close(file);

    return result;
}

From source file:org.sonatype.gshell.commands.vfs.TouchCommand.java

public Object execute(final CommandContext context) throws Exception {
    assert context != null;

    FileObject file = resolveFile(context, path);

    try {/*w  w  w. ja  v a2 s . c o m*/
        if (!file.exists()) {
            file.createFile();
        }

        file.getContent().setLastModifiedTime(System.currentTimeMillis());
    } finally {
        FileObjects.close(file);
    }

    return Result.SUCCESS;
}

From source file:unitTests.dataspaces.AbstractLimitingFileObjectTest.java

private void createRealFileChild() throws FileSystemException {
    realFile.createFolder();//from ww  w .j a  v a2  s .  c o m
    final FileObject childFile = realFile.resolveFile(CHILD_NAME);
    childFile.createFile();
    assertTrue(childFile.exists());
}

From source file:unitTests.dataspaces.VFSMountManagerHelperTest.java

/**
 * Tests closing all FileSystems/*from  ww  w  .  jav  a 2 s  .  c om*/
 * @throws Exception
 */
@Ignore("vfs close file system doesn't seem to work properly")
@Test
public void testCloseFileSystems() throws Exception {
    logger.info("*************** testCloseFileSystems");
    String[] validUrls = server.getVFSRootURLs();
    ArrayList<FileObject> fos = new ArrayList<FileObject>();
    for (String validUrl : validUrls) {
        FileObject mounted = VFSMountManagerHelper.mount(validUrl);
        Assert.assertTrue(mounted.exists());
        fos.add(mounted);
    }

    VFSMountManagerHelper.closeFileSystems(Arrays.asList(validUrls));

    boolean onlyExceptions = true;
    for (FileObject closedFo : fos) {
        try {
            FileObject toto = closedFo.resolveFile("toto");
            toto.createFile();
            onlyExceptions = false;
            logger.error(toto.getURL() + " exists : " + toto.exists());
        } catch (FileSystemException e) {
            // this should occur
        }
    }
    Assert.assertTrue("Only Exceptions received", onlyExceptions);
}