Example usage for org.apache.commons.vfs FileContent close

List of usage examples for org.apache.commons.vfs FileContent close

Introduction

In this page you can find the example usage for org.apache.commons.vfs FileContent close.

Prototype

void close() throws FileSystemException;

Source Link

Document

Closes all resources used by the content, including any open stream.

Usage

From source file:com.sonatype.nexus.plugin.groovyconsole.DefaultScriptStorage.java

private void updateScript(FileObject file) {
    FileName name = file.getName();//from   ww w.ja  v  a  2 s . c o m
    getLogger().info("New script file found: " + name);

    String script;
    try {
        FileContent content = file.getContent();
        script = IOUtils.toString(content.getInputStream());
        content.close();
    } catch (IOException e) {
        getLogger().warn("Unable to read script file: " + name, e);
        return;
    }

    synchronized (scripts) {
        scripts.put(getName(name), script);
    }
}

From source file:org.jclouds.vfs.provider.blobstore.test.BlobStoreProviderTestCase.java

private void writeFile(FileObject base, String name, String value) throws FileSystemException, IOException {
    FileObject file = base.resolveFile(name);
    FileContent content = file.getContent();
    CharStreams.write(value, CharStreams
            .newWriterSupplier(Utils.newOutputStreamSupplier(content.getOutputStream()), Charsets.UTF_8));
    content.close();
}

From source file:org.kalypso.simulation.grid.GridJobSubmitter.java

public void submitJob(final FileObject workingDir, final String executable, ISimulationMonitor monitor,
        String... arguments) throws SimulationException {
    if (monitor == null) {
        monitor = new NullSimulationMonitor();
    }/*from   ww w. j  a v  a 2s.  com*/

    // prepare streams
    FileObject stdoutFile = null;
    FileObject stderrFile = null;
    int returnCode = IStatus.ERROR;
    try {
        // stream stdout and stderr to files
        stdoutFile = workingDir.resolveFile("stdout");
        stderrFile = workingDir.resolveFile("stderr");

        // create process handle
        final String processFactoryId = "org.kalypso.simulation.gridprocess";
        // TODO: refactor so tempdir is created inside process
        final String tempDirName = workingDir.getName().getBaseName();
        final IProcess process = KalypsoCommonsExtensions.createProcess(processFactoryId, tempDirName,
                executable, arguments);
        // process.setProgressMonitor( new SimulationMonitorAdaptor( monitor ) );
        process.environment().put("OMP_NUM_THREADS", "4");

        final FileContent stdOutContent = stdoutFile.getContent();
        final OutputStream stdOut = stdOutContent.getOutputStream();
        final FileContent stdErrContent = stderrFile.getContent();
        final OutputStream stdErr = stdErrContent.getOutputStream();

        // stage-in files
        final FileSystemManager manager = workingDir.getFileSystem().getFileSystemManager();
        for (final URI einput : m_externalInputs.keySet()) {
            final FileObject inputFile = manager.resolveFile(getUriAsString(einput));
            final String destName = m_externalInputs.get(einput);
            if (destName != null) {
                final FileObject destFile = workingDir.resolveFile(destName);
                VFSUtil.copy(inputFile, destFile, null, true);
            } else {
                VFSUtil.copy(inputFile, workingDir, null, true);
            }
        }

        // start process
        returnCode = process.startProcess(stdOut, stdErr, null, null);
    } catch (final CoreException e) {
        // when process cannot be created
        throw new SimulationException("Could not create process.", e);
    } catch (final ProcessTimeoutException e) {
        e.printStackTrace();
    } catch (final FileNotFoundException e) {
        // can only happen when files cannot be created in tmpdir
        throw new SimulationException("Could not create temporary files for stdout and stderr.", e);
    } catch (final IOException e) {
        throw new SimulationException("Process I/O error.", e);
    } finally {
        // close files
        if (stdoutFile != null) {
            try {
                stdoutFile.getContent().close();
            } catch (final FileSystemException e) {
                // gobble
            }
        }
        if (stderrFile != null) {
            try {
                stderrFile.getContent().close();
            } catch (final FileSystemException e) {
                // gobble
            }
        }
    }

    // process failure handling
    if (returnCode != IStatus.OK) {
        String errString = "Process failed.";
        try {
            final FileContent content = stderrFile.getContent();
            final InputStream input2 = content.getInputStream();
            errString = errString + "\n" + IOUtils.toString(input2);
            content.close();
        } catch (final IOException e) {
            // ignore
        }
        monitor.setFinishInfo(returnCode, errString);
        throw new SimulationException(errString);
    } else {
        monitor.setFinishInfo(IStatus.OK, "Process finished successfully.");
    }
}

From source file:unitTests.dataspaces.AbstractLimitingFileObjectTest.java

@Test
public void testReadOnlyGetContentInputStream() throws IOException {
    createRealFile();//from  w  w  w  . j  a va2  s  .com

    final FileContent content = readOnlyFile.getContent();
    try {
        content.getInputStream().close();
    } finally {
        content.close();
    }
}

From source file:unitTests.dataspaces.AbstractLimitingFileObjectTest.java

@Test
public void testReadOnlyGetContentOutputStream() throws IOException {
    createRealFile();//  www  . ja  v  a2 s .  com

    final FileContent content = readOnlyFile.getContent();
    try {
        content.getOutputStream();
        fail("Expected exception");
    } catch (FileSystemException x) {
    } finally {
        content.close();
    }
}

From source file:unitTests.dataspaces.AbstractLimitingFileObjectTest.java

@Test
public void testReadOnlyGetContentRandomInputStream() throws IOException {
    createRealFile();/*from   w  w  w .j  a v a2  s  . c  o m*/

    final FileContent content = readOnlyFile.getContent();
    try {
        content.getRandomAccessContent(RandomAccessMode.READ).close();
    } finally {
        content.close();
    }
}

From source file:unitTests.dataspaces.AbstractLimitingFileObjectTest.java

@Test
public void testReadOnlyGetContentRandomOutputStream() throws IOException {
    createRealFile();//w  w  w  . j  a  v  a2s . co m

    final FileContent content = readOnlyFile.getContent();
    try {
        content.getRandomAccessContent(RandomAccessMode.READWRITE).close();
        fail("Expected exception");
    } catch (FileSystemException x) {
    } finally {
        content.close();
    }
}

From source file:unitTests.dataspaces.AbstractLimitingFileObjectTest.java

@Test
public void testReadWriteGetContentInputStream() throws IOException {
    createRealFile();//  w ww.j  a v a  2s .c o m

    final FileContent content = readWriteFile.getContent();
    try {
        content.getInputStream().close();
    } finally {
        content.close();
    }
}

From source file:unitTests.dataspaces.AbstractLimitingFileObjectTest.java

@Test
public void testReadWriteGetContentOutputStream() throws IOException {
    createRealFile();/* w  w  w .ja  v a2s  . c  o m*/

    final FileContent content = readWriteFile.getContent();
    try {
        content.getOutputStream();
    } finally {
        content.close();
    }
}

From source file:unitTests.dataspaces.AbstractLimitingFileObjectTest.java

@Test
public void testReadWriteGetContentRandomInputStream() throws IOException {
    createRealFile();// www. j  av  a  2  s  .c om

    final FileContent content = readWriteFile.getContent();
    try {
        content.getRandomAccessContent(RandomAccessMode.READ).close();
    } finally {
        content.close();
    }
}