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

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

Introduction

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

Prototype

@Override
void close() throws FileSystemException;

Source Link

Document

Closes this file, and its content.

Usage

From source file:io.hops.hopsworks.api.zeppelin.util.ZeppelinResource.java

private String readPid(FileObject file) {
    //pid value can only be extended up to a theoretical maximum of 
    //32768 for 32 bit systems or 4194304 for 64 bit:
    byte[] pid = new byte[8];
    if (file == null) {
        return null;
    }/*from  w w w  .  j a  va  2  s. co  m*/
    try {
        file.getContent().getInputStream().read(pid);
        file.close();
    } catch (FileSystemException ex) {
        return null;
    } catch (IOException ex) {
        return null;
    }
    String s;
    try {
        s = new String(pid, "UTF-8").trim();
    } catch (UnsupportedEncodingException ex) {
        return null;
    }
    return s;
}

From source file:hadoopInstaller.installation.Installer.java

private void loadConfiguration() throws InstallationFatalError {
    getLog().trace("HadoopInstaller.Configure.Start", //$NON-NLS-1$
            InstallerConstants.CONFIGURATION_FILE);
    String localDirectoryName = System.getProperty("user.dir"); //$NON-NLS-1$
    try {//from   www  . ja v  a 2 s  .  c  o  m
        setLocalDirectory(VFS.getManager().resolveFile(localDirectoryName));
        FileObject configurationFile = getLocalDirectory().resolveFile(InstallerConstants.CONFIGURATION_FILE);

        FileObject configurationSchema = getConfigurationSchema();
        setConfig(InstallerConfigurationParser
                .generateConfigurationFrom(XMLDocumentReader.parse(configurationFile, configurationSchema)));
        try {
            configurationFile.close();
        } catch (FileSystemException ex) {
            getLog().warn(ex, "HadoopInstaller.File.CouldNotClose", //$NON-NLS-1$
                    InstallerConstants.CONFIGURATION_FILE);
        }
    } catch (FileSystemException e) {
        throw new InstallationFatalError(e, "HadoopInstaller.Configure.CouldNotFindFile", //$NON-NLS-1$
                InstallerConstants.CONFIGURATION_FILE, localDirectoryName);
    } catch (InstallerConfigurationParseError e) {
        throw new InstallationFatalError(e.getCause(), "HadoopInstaller.Configure.CouldNotReadFile", //$NON-NLS-1$
                InstallerConstants.CONFIGURATION_FILE);
    }
    getLog().info("HadoopInstaller.Configure.Success"); //$NON-NLS-1$
}

From source file:com.web.server.EARDeployer.java

/**
 * This method removes the executor services from the executor services
 * map./*from w  w w.  ja v a  2 s.  c  o  m*/
 * 
 * @param jarFile
 * @param classList
 * @throws FileSystemException
 */
public void deleteExecutorServicesEar(String fileName, StandardFileSystemManager fsManager)
        throws FileSystemException {
    try {
        synchronized (urlClassLoaderMap) {
            ConcurrentHashMap map = (ConcurrentHashMap) filesMap.get(fileName);
            if (map != null) {
                //log.info("ClassLoader Map=\n\n\n\n"+urlClassLoaderMap);
                Set keysinclassloadermap = map.keySet();
                System.out.println(keysinclassloadermap.size());
                Iterator classloaders = keysinclassloadermap.iterator();
                while (classloaders.hasNext()) {
                    String classloader = (String) classloaders.next();
                    System.out.println("ClassLoader=" + classloader);
                    ClassLoader webClassLoader = (ClassLoader) this.urlClassLoaderMap.get(map.get(classloader));
                    this.urlClassLoaderMap.remove(map.get(classloader));
                    ClassLoaderUtil.cleanupJarFileFactory(ClassLoaderUtil.closeClassLoader(webClassLoader));
                    urlClassLoaderMap.remove(map.get(classloader));
                    try {
                        if (webClassLoader instanceof WebClassLoader)
                            ((WebClassLoader) webClassLoader).close();
                        else if (webClassLoader instanceof VFSClassLoader) {
                            FileObject[] fObj = ((VFSClassLoader) webClassLoader).getFileObjects();
                            for (FileObject obj : fObj) {
                                obj.close();
                            }
                        }
                    } catch (Throwable e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    /*if (classloader.contains(deployDirectory +"/"+ fileName)) {
                       System.out.println("removing classloader" + deployDirectory
                      + classloader);
                          File warDir=new File(fileName);
                          if(warDir.exists()){                                    
                      warDeployer.deleteDir(warDir);
                          }
                          //System.out.println("Before Remove class loader\n\n\n\n"+this.urlClassLoaderMap);                           
                          //System.out.println("Remove class loader\n\n\n\n"+this.urlClassLoaderMap);
                    }*/
                }
                System.gc();
                classloaders = keysinclassloadermap.iterator();
                while (classloaders.hasNext()) {
                    String classloader = (String) classloaders.next();
                    if (classloader.endsWith(".war")) {
                        warDeployer.removeServlets((String) map.get(classloader), classloader);
                        File warDir = new File((String) map.get(classloader));
                        if (warDir.exists()) {
                            warDeployer.deleteDir(warDir);
                        }
                    }
                }
                log.info(this.urlClassLoaderMap);
            }
            filesMap.remove(fileName);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

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

@Override
public boolean test() throws PublishException {
    FileObject root = null;
    try {//  www .  j a  v a2s . c o  m
        root = getRootFileObject();
    } catch (FileSystemException e) {
        logger.error("Test is fail:{}", e.toString());
        throw new PublishException("error.output.notconnect");
    }

    try {
        if (!root.exists()) {
            throw new PublishException("error.output.nodir");
        }
        if (!root.isWriteable()) {
            throw new PublishException("error.output.notwrite");
        }
        root.close();
        return true;
    } catch (FileSystemException e) {
        logger.error("Test  is fail:{}", e.toString());
        return false;
    }
}

From source file:hadoopInstaller.installation.Installer.java

private void analyzeBundles() throws InstallationFatalError {
    getLog().trace("HadoopInstaller.InstallationBundles.Start"); //$NON-NLS-1$
    FileObject folder;// www .  j av a2s  .co m
    try {
        folder = getLocalDirectory().resolveFile(InstallerConstants.TGZ_BUNDLES_FOLDER);
        if (!folder.exists()) {
            folder.createFolder();
            getLog().warn("HadoopInstaller.InstallationBundles.FolderDoesntExist", //$NON-NLS-1$
                    InstallerConstants.TGZ_BUNDLES_FOLDER, InstallerConstants.CONFIGURATION_FILE);
        }
    } catch (FileSystemException e) {
        throw new InstallationFatalError(e, "HadoopInstaller.InstallationBundles.FolderCouldNotOpen", //$NON-NLS-1$
                InstallerConstants.TGZ_BUNDLES_FOLDER);
    }
    for (String resource : getConfig().getFiles().keySet()) {
        String fileName = getConfig().getFiles().get(resource);
        try {
            getLog().trace("HadoopInstaller.InstallationBundles.From", resource, //$NON-NLS-1$
                    fileName);
            FileObject bundle = folder.resolveFile(fileName);
            if (!bundle.exists()) {
                throw new InstallationFatalError("HadoopInstaller.InstallationBundles.Missing", //$NON-NLS-1$
                        fileName, folder.getName().getBaseName());
            }
            if (doDeploy()) {
                getBundleHashes(bundle);
            }
            getBundleInstallDirectory(resource, bundle);
            try {
                bundle.close();
            } catch (FileSystemException ex) {
                getLog().warn(ex, "HadoopInstaller.CouldNotClose", //$NON-NLS-1$
                        fileName);
            }
        } catch (FileSystemException e) {
            throw new InstallationFatalError(e, "HadoopInstaller.InstallationBundles.Error", //$NON-NLS-1$
                    resource, fileName);
        }
        getLog().trace("HadoopInstaller.InstallationBundles.Success", //$NON-NLS-1$
                resource, fileName);
    }
    try {
        folder.close();
    } catch (FileSystemException e) {
        getLog().warn(e, "HadoopInstaller.CouldNotClose", //$NON-NLS-1$
                InstallerConstants.TGZ_BUNDLES_FOLDER);
    }
}

From source file:fulcrum.xml.Parser.java

/**
 * Only provides parsing functions to the "fulcrum.xml" package.
 * //from ww w .  j  av a  2  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:fi.mystes.synapse.mediator.vfs.VfsFileTransferUtility.java

/**
 * Helper method to execute file copy operation.
 * // ww w  .  j  av a  2s  . co m
 * @param file
 *            FileObject of the file to be copied
 * @param targetDirectoryPath
 *            FileObject of the target directory where the file will be
 *            copied
 * @return true if give file was a file and it was successfully copied to
 *         destination directory. False if given file is not a file or given
 *         destination directory is not a directory
 * @throws FileSystemException
 *             If file copy operation fails
 */
private boolean copyFile(FileObject file, String targetDirectoryPath, boolean lockEnabled)
        throws FileSystemException {
    if (file.getType() == FileType.FILE) {
        String targetPath = targetDirectoryPath + "/" + file.getName().getBaseName();
        String lockFilePath = null;
        if (lockEnabled) {
            lockFilePath = createLockFile(targetPath);
        }
        FileObject newLocation = resolveFile(targetPath);
        try {
            log.debug("About to copy " + fileObjectNameForDebug(file) + " to "
                    + fileObjectNameForDebug(newLocation));

            if (options.isStreamingTransferEnabled()) {
                streamFromFileToFile(file, resolveFile(targetPath));
            } else {
                newLocation.copyFrom(file, Selectors.SELECT_SELF);
            }

            newLocation.close();
            file.close();
            log.debug("File copied to " + fileObjectNameForDebug(newLocation));
        } finally {
            if (lockFilePath != null) {
                deleteLockFile(lockFilePath);
            }
        }
        return true;
    } else {
        return false;
    }
}

From source file:fi.mystes.synapse.mediator.vfs.VfsFileTransferUtility.java

/**
 * Helper method to execute file move operation.
 * /*from  w ww  .ja v a2 s. co m*/
 * @param file
 *            FileObject of the file to be moved
 * @param toDirectoryPath
 * @return true if give file was a file and it was successfully moved to
 *         destination directory. False if given file is not a file or given
 *         destination directory is not a directory
 * @throws FileSystemException
 *             If file move operation fails
 */
private boolean moveFile(FileObject file, String toDirectoryPath, boolean lockEnabled)
        throws FileSystemException {
    if (file.getType() == FileType.FILE) {
        String targetPath = toDirectoryPath + "/" + file.getName().getBaseName();
        String lockFilePath = null;
        if (lockEnabled) {
            lockFilePath = createLockFile(targetPath);
        }
        FileObject newLocation = resolveFile(targetPath);
        try {
            log.debug("About to move " + fileObjectNameForDebug(file) + " to "
                    + fileObjectNameForDebug(newLocation));

            if (options.isStreamingTransferEnabled()) {
                streamFromFileToFile(file, resolveFile(targetPath));
            } else {
                newLocation.copyFrom(file, Selectors.SELECT_SELF);
            }

            newLocation.close();
            file.delete();
            file.close();
            log.debug("File moved to " + fileObjectNameForDebug(newLocation));
        } finally {
            if (lockFilePath != null) {
                deleteLockFile(lockFilePath);
            }
        }
        return true;
    } else {
        return false;
    }
}

From source file:hadoopInstaller.installation.UploadConfiguration.java

private void uploadConfiguration(FileObject remoteDirectory, Host host) throws InstallationError {
    try {//from w w  w. java2  s. c  o  m
        FileObject configurationDirectory = remoteDirectory.resolveFile("hadoop/etc/hadoop/"); //$NON-NLS-1$
        if (deleteOldFiles) {
            configurationDirectory.delete(new AllFileSelector());
            log.debug("HostInstallation.Upload.DeletingOldFiles", //$NON-NLS-1$
                    host.getHostname());
        } else if (!configurationDirectory.exists()) {
            throw new InstallationError("HostInstallation.Upload.NotDeployed"); //$NON-NLS-1$
        }
        configurationDirectory.copyFrom(filesToUpload, new AllFileSelector());
        modifyEnvShFile(host, configurationDirectory, InstallerConstants.ENV_FILE_HADOOP);
        modifyEnvShFile(host, configurationDirectory, InstallerConstants.ENV_FILE_YARN);
        try {
            configurationDirectory.close();
        } catch (FileSystemException ex) {
            log.warn("HostInstallation.CouldNotClose", //$NON-NLS-1$
                    configurationDirectory.getName().getURI());
        }
    } catch (FileSystemException e) {
        throw new InstallationError(e, "HostInstallation.Upload.Error", //$NON-NLS-1$
                remoteDirectory.getName().getURI());
    }
}

From source file:fi.mystes.synapse.mediator.vfs.VfsFileTransferUtility.java

/**
 * Helper method to create lock file for multithreading.
 * /*from   w w  w .  j  a v a  2 s .c o  m*/
 * @param targetPath
 *            Target file path
 * @return URI of lock file
 * @throws FileSystemException
 *             If lock file creation fails
 */
private String createLockFile(String targetPath) throws FileSystemException {
    FileObject lockFile = resolveFile(lockFilePath(targetPath));
    log.debug("About to create lock file: " + fileObjectNameForDebug(lockFile));
    if (lockFile.exists()) {
        throw new FileSystemException("Lock file " + fileObjectNameForDebug(lockFile)
                + " already exists, refusing to create lock file");
    }
    lockFile.createFile();
    log.debug("Created lock file " + fileObjectNameForDebug(lockFile));

    String uri = lockFile.getName().getURI();

    lockFile.close();

    return uri;
}