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

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

Introduction

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

Prototype

FileContent getContent() throws FileSystemException;

Source Link

Document

Returns this file's content.

Usage

From source file:org.datacleaner.cli.CliRunner.java

public void run() throws Throwable {
    final String configurationFilePath = _arguments.getConfigurationFile();
    final FileObject configurationFile = VFSUtils.getFileSystemManager().resolveFile(configurationFilePath);
    final InputStream inputStream = configurationFile.getContent().getInputStream();
    try {//from  w  ww .  j ava2  s . co m
        run(new JaxbConfigurationReader().create(inputStream));
    } finally {
        FileHelper.safeClose(inputStream);
    }
}

From source file:org.datacleaner.cli.CliRunner.java

protected void runJob(DataCleanerConfiguration configuration) throws Throwable {
    final JaxbJobReader jobReader = new JaxbJobReader(configuration);

    final String jobFilePath = _arguments.getJobFile();
    final FileObject jobFile = VFS.getManager().resolveFile(jobFilePath);
    final Map<String, String> variableOverrides = _arguments.getVariableOverrides();

    final InputStream inputStream = jobFile.getContent().getInputStream();

    final AnalysisJobBuilder analysisJobBuilder;
    try {//from w  w  w .  ja v  a 2  s.c  o m
        analysisJobBuilder = jobReader.create(inputStream, variableOverrides);
    } finally {
        FileHelper.safeClose(inputStream);
    }

    final AnalysisRunner runner = new AnalysisRunnerImpl(configuration, new CliProgressAnalysisListener());
    final AnalysisResultFuture resultFuture = runner.run(analysisJobBuilder.toAnalysisJob());

    resultFuture.await();

    if (resultFuture.isSuccessful()) {
        final CliOutputType outputType = _arguments.getOutputType();
        AnalysisResultWriter writer = outputType.createWriter();
        writer.write(resultFuture, configuration, _writerRef, _outputStreamRef);
    } else {
        write("ERROR!");
        write("------");

        List<Throwable> errors = resultFuture.getErrors();
        write(errors.size() + " error(s) occurred while executing the job:");

        for (Throwable throwable : errors) {
            write("------");
            StringWriter stringWriter = new StringWriter();
            throwable.printStackTrace(new PrintWriter(stringWriter));
            write(stringWriter.toString());
        }

        throw errors.get(0);
    }
}

From source file:org.datacleaner.configuration.JaxbConfigurationReader.java

public DataCleanerConfiguration create(FileObject file) {
    InputStream inputStream = null;
    try {/*from ww  w.ja va  2 s  .co  m*/
        inputStream = file.getContent().getInputStream();
        return create(inputStream);
    } catch (FileSystemException e) {
        throw new IllegalArgumentException(e);
    } finally {
        FileHelper.safeClose(inputStream);
    }
}

From source file:org.datacleaner.job.JaxbJobReader.java

public AnalysisJobMetadata readMetadata(FileObject file) {
    InputStream inputStream = null;
    try {/*from w ww. j  a  va 2 s. co m*/
        inputStream = file.getContent().getInputStream();
        return readMetadata(inputStream);
    } catch (FileSystemException e) {
        throw new IllegalArgumentException(e);
    } finally {
        FileHelper.safeClose(inputStream);
    }
}

From source file:org.datacleaner.job.JaxbJobReader.java

public AnalysisJobBuilder create(FileObject file) {
    InputStream inputStream = null;
    try {/*from w  w  w  .  ja  v  a2s .c om*/
        inputStream = file.getContent().getInputStream();
        return create(inputStream);
    } catch (FileSystemException e) {
        throw new IllegalArgumentException(e);
    } finally {
        FileHelper.safeClose(inputStream);
    }
}

From source file:org.datacleaner.user.DataCleanerHome.java

private static FileObject copyIfNonExisting(FileObject candidate, FileSystemManager manager, String filename)
        throws FileSystemException {
    FileObject file = candidate.resolveFile(filename);
    if (file.exists()) {
        logger.info("File already exists in DATACLEANER_HOME: " + filename);
        return file;
    }//w  w  w  . jav  a2s  .co  m
    FileObject parentFile = file.getParent();
    if (!parentFile.exists()) {
        parentFile.createFolder();
    }

    final ResourceManager resourceManager = ResourceManager.get();
    final URL url = resourceManager.getUrl("datacleaner-home/" + filename);
    if (url == null) {
        return null;
    }

    InputStream in = null;
    OutputStream out = null;
    try {
        in = url.openStream();
        out = file.getContent().getOutputStream();

        FileHelper.copy(in, out);
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    } finally {
        FileHelper.safeClose(in, out);
    }

    return file;
}

From source file:org.datacleaner.user.upgrade.DataCleanerHomeUpgrader.java

private static FileObject overwriteFileWithDefaults(FileObject targetDirectory, String targetFilename)
        throws FileSystemException {
    FileObject file = targetDirectory.resolveFile(targetFilename);
    FileObject parentFile = file.getParent();
    if (!parentFile.exists()) {
        parentFile.createFolder();/*from   ww w  .  ja  v  a  2  s  .com*/
    }

    final ResourceManager resourceManager = ResourceManager.get();
    final URL url = resourceManager.getUrl("datacleaner-home/" + targetFilename);
    if (url == null) {
        return null;
    }

    InputStream in = null;
    OutputStream out = null;
    try {
        in = url.openStream();
        out = file.getContent().getOutputStream();

        FileHelper.copy(in, out);
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    } finally {
        FileHelper.safeClose(in, out);
    }

    return file;
}

From source file:org.datacleaner.user.UserPreferencesImpl.java

/**
 * Loads a user preferences file and initializes a
 * {@link UserPreferencesImpl} object using it.
 * //from  ww  w.  ja v  a  2  s .  c  o m
 * @param userPreferencesFile
 * @param loadDatabaseDrivers
 * @return
 */
public static UserPreferences load(final FileObject userPreferencesFile, final boolean loadDatabaseDrivers) {
    try {
        if (userPreferencesFile == null || !userPreferencesFile.exists()) {
            logger.info("User preferences file does not exist");
            return new UserPreferencesImpl(userPreferencesFile);
        }
    } catch (FileSystemException e1) {
        logger.debug("Could not determine if file exists: {}", userPreferencesFile);
    }

    ChangeAwareObjectInputStream inputStream = null;
    try {
        inputStream = new ChangeAwareObjectInputStream(userPreferencesFile.getContent().getInputStream());
        inputStream.addRenamedClass("org.datacleaner.user.UserPreferences", UserPreferencesImpl.class);
        UserPreferencesImpl result = (UserPreferencesImpl) inputStream.readObject();

        if (loadDatabaseDrivers) {
            List<UserDatabaseDriver> installedDatabaseDrivers = result.getDatabaseDrivers();
            for (UserDatabaseDriver userDatabaseDriver : installedDatabaseDrivers) {
                try {
                    userDatabaseDriver.loadDriver();
                } catch (IllegalStateException e) {
                    logger.error("Could not load database driver", e);
                }
            }
        }

        result._userPreferencesFile = userPreferencesFile;
        result.refreshProxySettings();
        return result;
    } catch (InvalidClassException e) {
        logger.warn("User preferences file version does not match application version: {}", e.getMessage());
        return new UserPreferencesImpl(userPreferencesFile);
    } catch (Exception e) {
        logger.warn("Could not read user preferences file", e);
        return new UserPreferencesImpl(userPreferencesFile);
    } finally {
        FileHelper.safeClose(inputStream);
    }
}

From source file:org.datacleaner.util.VFSUtilsTest.java

public void test2HttpAccess() throws Exception {
    // first check if we have a connection
    try {//from www . j  av a 2 s .  c  o m
        InetAddress.getByName("eobjects.org");
    } catch (UnknownHostException e) {
        System.err.println("Skipping test " + getClass().getSimpleName() + "." + getName()
                + " since we don't seem to be able to reach eobjects.org");
        e.printStackTrace();
        return;
    }

    FileObject file = VFSUtils.getFileSystemManager().resolveFile("http://eobjects.org");
    try (InputStream in = file.getContent().getInputStream()) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String readLine = reader.readLine();
        assertNotNull(readLine);
    }
}

From source file:org.efaps.db.store.VFSStoreResource.java

/**
 * The method writes the context (from the input stream) to a temporary file
 * (same file URL, but with extension {@link #EXTENSION_TEMP}).
 *
 * @param _in   input stream defined the content of the file
 * @param _size length of the content (or negative meaning that the length
 *              is not known; then the content gets the length of readable
 *              bytes from the input stream)
 * @param _fileName name of the file//from   www.j  av a 2 s .  c o m
 * @return size of the created temporary file object
 * @throws EFapsException on error
 */
@Override
public long write(final InputStream _in, final long _size, final String _fileName) throws EFapsException {
    try {
        long size = _size;
        final FileObject tmpFile = this.manager.resolveFile(this.manager.getBaseFile(),
                this.storeFileName + VFSStoreResource.EXTENSION_TEMP);
        if (!tmpFile.exists()) {
            tmpFile.createFile();
        }
        final FileContent content = tmpFile.getContent();
        OutputStream out = content.getOutputStream(false);
        if (getCompress().equals(Compress.GZIP)) {
            out = new GZIPOutputStream(out);
        } else if (getCompress().equals(Compress.ZIP)) {
            out = new ZipOutputStream(out);
        }

        // if size is unkown!
        if (_size < 0) {
            int length = 1;
            size = 0;
            while (length > 0) {
                length = _in.read(this.buffer);
                if (length > 0) {
                    out.write(this.buffer, 0, length);
                    size += length;
                }
            }
        } else {
            Long length = _size;
            while (length > 0) {
                final int readLength = length.intValue() < this.buffer.length ? length.intValue()
                        : this.buffer.length;
                _in.read(this.buffer, 0, readLength);
                out.write(this.buffer, 0, readLength);
                length -= readLength;
            }
        }
        if (getCompress().equals(Compress.GZIP) || getCompress().equals(Compress.ZIP)) {
            out.close();
        }
        tmpFile.close();
        setFileInfo(_fileName, size);
        return size;
    } catch (final IOException e) {
        VFSStoreResource.LOG.error("write of content failed", e);
        throw new EFapsException(VFSStoreResource.class, "write.IOException", e);
    }

}