List of usage examples for org.apache.commons.vfs2 FileSystemManager closeFileSystem
void closeFileSystem(FileSystem filesystem);
From source file:org.cloudifysource.quality.iTests.test.cli.cloudify.cloud.byon.CleanGSFilesByonTest.java
/** * Checks whether the files or folders exist on a remote host. * The returned value depends on the last parameter - "allMustExist". * If allMustExist is True the returned value is True only if all listed objects exist. * If allMustExist is False, the returned value is True if at least one object exists. * //from ww w.j ava 2s .c o m * @param host The host to connect to * @param username The name of the user that deletes the file/folder * @param password The password of the above user * @param keyFile The key file, if used * @param fileSystemObjects The files or folders to delete * @param fileTransferMode SCP for secure copy in Linux, or CIFS for windows file sharing * @param allMustExist If set to True the function will return True only if all listed objects exist. * If set to False, the function will return True if at least one object exists. * @return depends on allMustExist * @throws IOException Indicates the deletion failed */ public static boolean fileSystemObjectsExist(final String host, final String username, final String password, final String keyFile, final List<String> fileSystemObjects, final FileTransferModes fileTransferMode, final boolean allMustExist) throws IOException { boolean objectsExist; if (allMustExist) { objectsExist = true; } else { objectsExist = false; } if (!fileTransferMode.equals(FileTransferModes.SCP)) { //TODO Support get with CIFS as well throw new IOException("File resolving is currently not supported for this file transfer protocol (" + fileTransferMode + ")"); } final FileSystemOptions opts = new FileSystemOptions(); SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no"); SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, false); if (keyFile != null && !keyFile.isEmpty()) { final File temp = new File(keyFile); if (!temp.isFile()) { throw new FileNotFoundException("Could not find key file: " + temp); } SftpFileSystemConfigBuilder.getInstance().setIdentities(opts, new File[] { temp }); } SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, SFTP_DISCONNECT_DETECTION_TIMEOUT_MILLIS); final FileSystemManager mng = VFS.getManager(); String scpTargetBase, scpTarget; if (password != null && !password.isEmpty()) { scpTargetBase = "sftp://" + username + ':' + password + '@' + host; } else { scpTargetBase = "sftp://" + username + '@' + host; } FileObject remoteDir = null; try { for (final String fileSystemObject : fileSystemObjects) { scpTarget = scpTargetBase + fileSystemObject; remoteDir = mng.resolveFile(scpTarget, opts); if (remoteDir.exists()) { if (!allMustExist) { objectsExist = true; break; } } else { if (allMustExist) { objectsExist = false; break; } } } } finally { if (remoteDir != null) { mng.closeFileSystem(remoteDir.getFileSystem()); } } return objectsExist; }
From source file:org.geoserver.backuprestore.utils.BackupUtils.java
/** * Extracts the archive file {@code archiveFile} to {@code targetFolder}; both shall previously exist. * * @param archiveFile/* ww w . j av a 2s .co m*/ * @param targetFolder * @throws IOException */ public static void extractTo(Resource archiveFile, Resource targetFolder) throws IOException { FileSystemManager manager = VFS.getManager(); String sourceURI = resolveArchiveURI(archiveFile); FileObject source = manager.resolveFile(sourceURI); if (manager.canCreateFileSystem(source)) { source = manager.createFileSystem(source); } FileObject target = manager .createVirtualFileSystem(manager.resolveFile(targetFolder.dir().getAbsolutePath())); FileSelector selector = new AllFileSelector() { @Override public boolean includeFile(FileSelectInfo fileInfo) { LOGGER.fine("Uncompressing " + fileInfo.getFile().getName().getFriendlyURI()); return true; } }; target.copyFrom(source, selector); source.close(); target.close(); manager.closeFileSystem(source.getFileSystem()); }
From source file:org.geoserver.backuprestore.utils.BackupUtils.java
/** * Compress {@code sourceFolder} to the archive file {@code archiveFile}; both shall previously exist. * // ww w .ja v a2 s .c om * @param sourceFolder * @param archiveFile * @throws IOException */ public static void compressTo(Resource sourceFolder, Resource archiveFile) throws IOException { // See https://commons.apache.org/proper/commons-vfs/filesystems.html // for the supported filesystems FileSystemManager manager = VFS.getManager(); FileObject sourceDir = manager .createVirtualFileSystem(manager.resolveFile(sourceFolder.dir().getAbsolutePath())); try { if ("zip".equalsIgnoreCase(FileUtils.getExtension(archiveFile.path()))) { // apache VFS does not support ZIP as writable FileSystem OutputStream fos = archiveFile.out(); // Create access to zip. ZipOutputStream zos = new ZipOutputStream(fos); // add entry/-ies. for (FileObject sourceFile : sourceDir.getChildren()) { writeEntry(zos, sourceFile, null); } // Close streams zos.flush(); zos.close(); fos.close(); } else { // Create access to archive. FileObject zipFile = manager.resolveFile(resolveArchiveURI(archiveFile)); zipFile.createFile(); ZipOutputStream zos = new ZipOutputStream(zipFile.getContent().getOutputStream()); // add entry/-ies. for (FileObject sourceFile : sourceDir.getChildren()) { writeEntry(zos, sourceFile, null); } // Close streams zos.flush(); zos.close(); zipFile.close(); manager.closeFileSystem(zipFile.getFileSystem()); } } finally { manager.closeFileSystem(sourceDir.getFileSystem()); } }
From source file:org.geoserver.importer.RemoteData.java
public ImportData resolve(Importer importer) throws IOException { // prepare the target Directory target = Directory.createNew(importer.getUploadRoot()); FileSystemManager manager = null; FileObject fo = null;//from ww w. j av a 2 s . co m try { manager = VFS.getManager(); if (username != null) { StaticUserAuthenticator auth = new StaticUserAuthenticator(domain, username, password); FileSystemOptions opts = new FileSystemOptions(); DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth); fo = manager.resolveFile(location, opts); } else { fo = manager.resolveFile(location); } target.accept(fo); } finally { if (fo != null) { FileSystem fs = fo.getFileSystem(); fo.close(); manager.closeFileSystem(fs); } } return target; }
From source file:org.wso2.carbon.transport.filesystem.connector.server.util.FileTransportUtils.java
/** * Acquire the file level locking.//from w w w . j ava 2 s. co m * * @param fsManager The file system manager instance * @param fileObject The file object to get the lock from * @param fsOpts The file system options to be used with the file system manager * @return Boolean value whether lock was successful */ public static synchronized boolean acquireLock(FileSystemManager fsManager, FileObject fileObject, FileSystemOptions fsOpts) { String strContext = fileObject.getName().getURI(); // When processing a directory list is fetched initially. Therefore // there is still a chance of file processed by another process. // Need to check the source file before processing. try { String parentURI = fileObject.getParent().getName().getURI(); if (parentURI.contains("?")) { String suffix = parentURI.substring(parentURI.indexOf("?")); strContext += suffix; } FileObject sourceFile = fsManager.resolveFile(strContext, fsOpts); if (!sourceFile.exists()) { return false; } } catch (FileSystemException e) { return false; } FileObject lockObject = null; try { // check whether there is an existing lock for this item, if so it is assumed // to be processed by an another listener (downloading) or a sender (uploading) // lock file is derived by attaching the ".lock" second extension to the file name String fullPath = fileObject.getName().getURI(); int pos = fullPath.indexOf("?"); if (pos != -1) { fullPath = fullPath.substring(0, pos); } lockObject = fsManager.resolveFile(fullPath + ".lock", fsOpts); if (lockObject.exists()) { log.debug("There seems to be an external lock, aborting the processing of the file " + maskURLPassword(fileObject.getName().getURI()) + ". This could possibly be due to some other party already " + "processing this file or the file is still being uploaded"); } else if (processing.contains(fullPath)) { log.debug(maskURLPassword(fileObject.getName().getURI()) + "is already being processed."); } else { //Check the original file existence before the lock file to handle concurrent access scenario FileObject originalFileObject = fsManager.resolveFile(fullPath, fsOpts); if (!originalFileObject.exists()) { return false; } processing.add(fullPath); return true; } } catch (FileSystemException fse) { log.error("Cannot get the lock for the file : " + maskURLPassword(fileObject.getName().getURI()) + " before processing", fse); if (lockObject != null) { try { fsManager.closeFileSystem(lockObject.getParent().getFileSystem()); } catch (FileSystemException e) { log.warn("Unable to close the lockObject parent file system"); } } } return false; }
From source file:org.wso2.carbon.transport.remotefilesystem.server.util.FileTransportUtils.java
/** * Acquire the file level locking./*from w w w . j a v a 2 s . co m*/ * * @param fsManager The file system manager instance * @param fileObject The file object to get the lock from * @param fsOpts The file system options to be used with the file system manager * @return Boolean value whether lock was successful */ public static synchronized boolean acquireLock(FileSystemManager fsManager, FileObject fileObject, FileSystemOptions fsOpts) { String strContext = fileObject.getName().getURI(); // When processing a directory list is fetched initially. Therefore // there is still a chance of file processed by another process. // Need to check the source file before processing. try { String parentURI = fileObject.getParent().getName().getURI(); if (parentURI.contains("?")) { String suffix = parentURI.substring(parentURI.indexOf("?")); strContext += suffix; } FileObject sourceFile = fsManager.resolveFile(strContext, fsOpts); if (!sourceFile.exists()) { return false; } } catch (FileSystemException e) { return false; } FileObject lockObject = null; try { // check whether there is an existing lock for this item, if so it is assumed // to be processed by an another listener (downloading) or a sender (uploading) // lock file is derived by attaching the ".lock" second extension to the file name String fullPath = fileObject.getName().getURI(); int pos = fullPath.indexOf("?"); if (pos != -1) { fullPath = fullPath.substring(0, pos); } lockObject = fsManager.resolveFile(fullPath + ".lock", fsOpts); if (lockObject.exists()) { if (log.isDebugEnabled()) { log.debug("There seems to be an external lock, aborting the processing of the file " + maskURLPassword(fileObject.getName().getURI()) + ". This could possibly be due to some other party already " + "processing this file or the file is still being uploaded"); } } else if (processing.contains(fullPath)) { if (log.isDebugEnabled()) { log.debug(maskURLPassword(fileObject.getName().getURI()) + "is already being processed."); } } else { //Check the original file existence before the lock file to handle concurrent access scenario FileObject originalFileObject = fsManager.resolveFile(fullPath, fsOpts); if (!originalFileObject.exists()) { return false; } processing.add(fullPath); return true; } } catch (FileSystemException fse) { log.error("Cannot get the lock for the file : " + maskURLPassword(fileObject.getName().getURI()) + " before processing", fse); if (lockObject != null) { try { fsManager.closeFileSystem(lockObject.getParent().getFileSystem()); } catch (FileSystemException e) { log.warn("Unable to close the lockObject parent file system"); } } } return false; }