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

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

Introduction

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

Prototype

@Override
void close() throws FileSystemException;

Source Link

Document

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

Usage

From source file:com.carrotgarden.nexus.example.script.ScriptStorageImpl.java

private void updateScript(final FileObject file) {

    final FileName name = file.getName();

    log.info("New script file found: " + name);

    String script;//  w  w w.java2s.  c  om

    try {

        final FileContent content = file.getContent();

        script = IOUtil.toString(content.getInputStream());

        content.close();

    } catch (final IOException e) {

        log.warn("Unable to read script file: " + name, e);

        return;

    }

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

}

From source file:cz.lbenda.dataman.db.ExtConfFactory.java

/** Load extend configuration to given database configuration */
public void load() {
    if (exConf != null && StringUtils.isBlank(exConf.getSrc())) {
        loadExConfType(exConf);//from w  w  w.  j  av  a 2s . com
    } else if (exConf != null) {
        if (exConf.getSrc().startsWith("db://")) {
            String path = exConf.getSrc().substring(5, exConf.getSrc().length());
            dbConfig.getConnectionProvider()
                    .onPreparedStatement(String.format(
                            "select usr, exConf from %s where (usr = ? or usr is null or usr = '')", path),
                            tuple2 -> {
                                PreparedStatement ps = tuple2.get1();
                                String extendConfiguration = null;
                                try {
                                    ps.setString(1, dbConfig.getConnectionProvider().getUser().getUsername());
                                    try (ResultSet rs = ps.executeQuery()) {
                                        while (rs.next()) {
                                            if (rs.getString(1) == null && extendConfiguration == null) { // The null user is used only if no specific user configuration is read
                                                extendConfiguration = rs.getString(2);
                                            } else if (rs.getString(1) != null) {
                                                extendConfiguration = rs.getString(2);
                                            }
                                        }
                                    }
                                } catch (SQLException e) {
                                    LOG.error("Problem with read extend config from table: " + exConf.getSrc(),
                                            e);
                                    ExceptionMessageFrmController.showException(
                                            "Problem with read extend config from table: " + exConf.getSrc(),
                                            e);
                                }
                                if (!StringUtils.isBlank(extendConfiguration)) {
                                    loadExConfType(new StringReader(extendConfiguration));
                                } else {
                                    StringUtils.isBlank(null);
                                }
                            });
        } else {
            try {
                FileSystemManager fsManager = VFS.getManager();
                FileObject file = fsManager.resolveFile(exConf.getSrc());
                if (!file.exists()) {
                    ExceptionMessageFrmController.showException("File not exist: " + exConf.getSrc());
                } else if (file.getChildren() == null || file.getChildren().length == 0) {
                    new Thread(() -> {
                        try {
                            FileContent content = file.getContent();
                            loadExConfType(new InputStreamReader(content.getInputStream()));
                            content.close();
                        } catch (FileSystemException e) {
                            LOG.error("Problem with read extend config from file: " + exConf.getSrc(), e);
                            ExceptionMessageFrmController.showException(
                                    "Problem with read extend config from file: " + exConf.getSrc(), e);
                        }
                    }).start();
                } else {
                    ExceptionMessageFrmController
                            .showException("The file type isn't supported: " + exConf.getSrc());
                }
            } catch (FileSystemException e) {
                LOG.error("Problem with read extend config from file: " + exConf.getSrc(), e);
                ExceptionMessageFrmController
                        .showException("Problem with read extend config from file: " + exConf.getSrc(), e);
            }
        }
    }
}

From source file:com.ewcms.publication.deploy.provider.DeployOperatorBase.java

@Override
public void copy(byte[] content, String targetPath) throws PublishException {

    String targetFullPath = targetFullPath(builder.getPath(), targetPath);
    logger.debug("Server file's path is {}", targetFullPath);

    FileObject root = null;/*from   w  ww.  jav a  2  s.  com*/
    FileObject target = null;
    try {
        root = getRootFileObject();
        //target = getTargetFileObject(root, targetFullPath);
        //-------------- Windows? ??----------------------
        targetPath = targetPath.replace("\\", "/").replace("//", "/");
        if (targetPath.indexOf("/") == 0) {
            targetPath = targetPath.substring(1);
        }
        target = getTargetFileObject(root, targetPath);
        //--------------------------------------------------------------
        FileContent fileContent = target.getContent();
        OutputStream stream = fileContent.getOutputStream();
        stream.write(content);
        stream.flush();

        stream.close();
        fileContent.close();
        target.close();
        root.close();
    } catch (FileSystemException e) {
        logger.error("Copy {} is error:{}", targetFullPath, e);
        throw new PublishException(e);
    } catch (IOException e) {
        logger.error("Copy {} is error:{}", targetFullPath, e);
        throw new PublishException(e);
    } finally {
        try {
            if (root != null) {
                root.close();
            }
            if (target != null) {
                target.close();
            }
        } catch (Exception e) {
            logger.error("vfs close is error:{}", e.toString());
        }
    }
}

From source file:fulcrum.xml.Parser.java

/**
 * Only provides parsing functions to the "fulcrum.xml" package.
 * // w w  w  .  ja v  a2  s  .co m
 * @see Document
 * 
 * @param fileLocation
 * @param document
 * @throws ParseException
 */
protected void parse(String fileLocation, Document document) throws ParseException {
    FileObject fileObj = null;
    try {
        fileObj = fsManager.resolveFile(fileLocation);

        if (fileObj != null && fileObj.exists() && fileObj.isReadable()) {
            FileContent content = fileObj.getContent();
            InputStream is = content.getInputStream();
            int size = is.available();
            LOGGER.debug("Total File Size: " + size + " bytes");
            byte[] buffer = new byte[size];
            is.read(buffer, 0, size);
            LOGGER.debug("Start parsing");
            parse(buffer, document);
            LOGGER.debug("Finished paring");
            buffer = null;
            content.close();
            fileObj.close();
        }
    } catch (Exception e) {
        throw new ParseException(e);
    }
}

From source file:com.ewcms.publication.deploy.provider.DeployOperatorBase.java

@Override
public void copy(File sourceFile, String targetPath) throws PublishException {
    //TODO ??????
    if (!sourceFile.exists()) {
        logger.debug("Source file path's {} is not exists.", sourceFile.getPath());
        return;/*from  ww w .j  a  v  a2 s  .c  o m*/
    }

    String targetFullPath = targetFullPath(builder.getPath(), targetPath);
    logger.debug("Server file's path is {}", targetFullPath);
    logger.debug("Source file's path is {}  ", sourceFile.getPath());

    FileObject root = null;
    FileObject target = null;
    try {
        root = getRootFileObject();
        //target = getTargetFileObject(root, targetFullPath);
        //-------------- Windows? ??----------------------
        targetPath = targetPath.replace("\\", "/").replace("//", "/");
        if (targetPath.indexOf("/") == 0) {
            targetPath = targetPath.substring(1);
        }
        target = getTargetFileObject(root, targetPath);
        //--------------------------------------------------------------
        FileContent fileContent = target.getContent();

        OutputStream out = fileContent.getOutputStream();
        InputStream in = new FileInputStream(sourceFile);

        byte[] buffer = new byte[1024 * 8];
        int len = 0;
        while ((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }
        out.flush();

        in.close();
        out.close();
        fileContent.close();
    } catch (FileSystemException e) {
        logger.error("Copy {} is error:{}", targetFullPath, e);
        throw new PublishException(e);
    } catch (IOException e) {
        logger.error("Copy {} is error:{}", targetFullPath, e);
        throw new PublishException(e);
    } finally {
        try {
            if (root != null) {
                root.close();
            }
            if (target != null) {
                target.close();
            }
        } catch (Exception e) {
            logger.error("vfs close is error:{}", e.toString());
        }
    }
}

From source file:org.fuin.vfs2.filter.AgeFileFilter.java

/**
 * Tests if the specified <code>File</code> is newer than the specified time
 * reference./*from  w  ww.j  a v a 2 s .c  o  m*/
 * 
 * @param fileObject
 *            the <code>File</code> of which the modification date must be
 *            compared, must not be {@code null}
 * @param timeMillis
 *            the time reference measured in milliseconds since the epoch
 *            (00:00:00 GMT, January 1, 1970)
 * @return true if the <code>File</code> exists and has been modified after
 *         the given time reference.
 * @throws IllegalArgumentException
 *             if the file is {@code null}
 */
private static boolean isFileNewer(final FileObject fileObject, final long timeMillis) {
    if (fileObject == null) {
        throw new IllegalArgumentException("No specified file");
    }
    try {
        if (!fileObject.exists()) {
            return false;
        }
        final FileContent content = fileObject.getContent();
        try {
            final long lastModified = content.getLastModifiedTime();
            return lastModified > timeMillis;
        } finally {
            content.close();
        }
    } catch (final FileSystemException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.fuin.vfs2.filter.EmptyFileFilter.java

/**
 * Checks to see if the file is empty. A non-existing file is also
 * considered empty.//from www . j  a va 2  s  .  c  o m
 * 
 * @param fileInfo
 *            the file or directory to check
 * 
 * @return {@code true} if the file or directory is <i>empty</i>, otherwise
 *         {@code false}.
 */
@Override
public boolean accept(final FileSelectInfo fileInfo) {
    final FileObject file = fileInfo.getFile();
    try {
        if (!file.exists()) {
            return true;
        }
        if (file.getType() == FileType.FOLDER) {
            final FileObject[] files = file.getChildren();
            return files == null || files.length == 0;
        }
        final FileContent content = file.getContent();
        try {
            return content.getSize() == 0;
        } finally {
            content.close();
        }
    } catch (final FileSystemException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.fuin.vfs2.filter.SizeFileFilter.java

/**
 * Checks to see if the size of the file is favorable.
 * <p>//www. j  a  v a 2  s  .  c o  m
 * If size equals threshold and smaller files are required, file <b>IS
 * NOT</b> selected. If size equals threshold and larger files are required,
 * file <b>IS</b> selected.
 * <p>
 * Non-existing files return always false (will never be accepted).
 * 
 * @param fileInfo
 *            the File to check
 * 
 * @return true if the filename matches
 */
@Override
public boolean accept(final FileSelectInfo fileInfo) {
    try {
        final FileObject file = fileInfo.getFile();
        if (!file.exists()) {
            return false;
        }
        final FileContent content = file.getContent();
        try {
            final long length = content.getSize();
            final boolean smaller = length < size;
            return acceptLarger ? !smaller : smaller;
        } finally {
            content.close();
        }
    } catch (final FileSystemException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.mycore.backend.filesystem.MCRCStoreVFS.java

@Override
protected String doStoreContent(MCRFileReader file, MCRContentInputStream source) throws Exception {
    StringBuilder storageId = new StringBuilder();

    String[] slots = buildSlotPath();
    // Recursively create directory name
    for (String slot : slots) {
        storageId.append(slot).append("/");
    }//  w w  w  .j a  v  a2  s . c  om

    String fileId = buildNextID(file);
    storageId.append(fileId);

    FileObject targetObject = fsManager.resolveFile(getBase(), storageId.toString());
    FileContent targetContent = targetObject.getContent();
    try (OutputStream out = targetContent.getOutputStream()) {
        IOUtils.copy(source, out);
    } finally {
        targetContent.close();
    }
    return storageId.toString();
}

From source file:org.mycore.common.content.MCRVFSContent.java

@Override
public InputStream getInputStream() throws IOException {
    final FileContent content = fo.getContent();
    LOGGER.debug(() -> getDebugMessage("{}: returning InputStream of {}"));
    return new FilterInputStream(content.getInputStream()) {
        @Override/*from   w  w w . j  a  v  a  2  s  .  c om*/
        public void close() throws IOException {
            LOGGER.debug(() -> getDebugMessage("{}: closing InputStream of {}"));
            super.close();
            content.close();
        }
    };
}