List of usage examples for org.apache.commons.vfs2 FileObject close
@Override void close() throws FileSystemException;
From source file:org.wso2.carbon.connector.FileArchiveConnector.java
/** * Add the all files into List./*from ww w . j av a 2 s. c om*/ * * @param dir The source file/folder directory. * @param fileList List for adding the files. */ private void addAllFilesToList(FileObject dir, List<FileObject> fileList) { try { FileObject[] children = dir.getChildren(); for (FileObject child : children) { fileList.add(child); if (FileType.FOLDER.equals(child.getType())) { addAllFilesToList(child, fileList); } } } catch (FileSystemException e) { throw new SynapseException("Unable to add all files into List", e); } finally { try { dir.close(); } catch (FileSystemException e) { log.error("Error while closing the FileObject", e); } } }
From source file:org.wso2.carbon.connector.FileArchiveConnector.java
/** * Extract all files to add the zip directory. * * @param fileObj Source fileObject. * @param directoryToZip Destination fileObject. * @param fileList List of files to be compressed. * @throws FileSystemException When get the OutputStream, get file type. */// ww w. j a va 2 s .co m private void writeZipFiles(FileObject fileObj, FileObject directoryToZip, List<FileObject> fileList) throws FileSystemException { ZipOutputStream zos = null; try { zos = new ZipOutputStream(directoryToZip.getContent().getOutputStream()); for (FileObject file : fileList) { if (FileType.FILE.equals(file.getType())) { addToZip(fileObj, file, zos); } } } catch (FileSystemException e) { throw new SynapseException("Error occurs in writing files", e); } finally { try { if (zos != null) { zos.close(); } } catch (IOException e) { log.error("Error while closing the ZipOutputStream"); } try { fileObj.close(); directoryToZip.close(); } catch (FileSystemException e) { log.error("Error while closing the FileObject", e); } } }
From source file:org.wso2.carbon.connector.FileArchiveConnector.java
/** * Add the file to zip directory./*from w w w . j ava 2 s .com*/ * * @param fileObject The fileObject where zip entry data needs to be written. * @param file The fileObject which needs to be archived. * @param outputStream ZipOutputStream. */ private void addToZip(FileObject fileObject, FileObject file, ZipOutputStream outputStream) { InputStream fin = null; try { fin = file.getContent().getInputStream(); String name = file.getName().toString(); String entry = name.substring(fileObject.getName().toString().length() + 1, name.length()); ZipEntry zipEntry = new ZipEntry(entry); outputStream.putNextEntry(zipEntry); int length; while ((length = fin.read(bytes)) != -1) { outputStream.write(bytes, 0, length); } } catch (IOException e) { throw new SynapseException("Unable to write an array of bytes to the ZIP entry data", e); } finally { try { outputStream.closeEntry(); if (fin != null) { fin.close(); } } catch (IOException e) { log.error("Error while closing InputStream", e); } try { fileObject.close(); file.close(); } catch (FileSystemException e) { log.error("Error while closing the FileObject", e); } } }
From source file:org.wso2.carbon.connector.FileCopyConnector.java
/** * Copy the file or folder from source to destination. * * @param messageContext The message context that is generated for processing the file. * @param opts FileSystemOptions. * @return return true, if file/folder is successfully copied. *///from w w w . j a v a2s. c o m private boolean copyFile(String source, MessageContext messageContext, FileSystemOptions opts) { String destination = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.NEW_FILE_LOCATION); String filePattern = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.FILE_PATTERN); String includeParentDir = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.INCLUDE_PARENT_DIRECTORY); boolean includeParentDirectory = FileConstants.DEFAULT_INCLUDE_PARENT_DIRECTORY; if (StringUtils.isNotEmpty(includeParentDir)) { includeParentDirectory = Boolean.parseBoolean(includeParentDir); } boolean resultStatus = false; StandardFileSystemManager manager = FileConnectorUtils.getManager(); FileObject souFile = null; FileObject destFile = null; try { souFile = manager.resolveFile(source, opts); destFile = manager.resolveFile(destination, opts); if (!souFile.exists()) { log.error("The File Location does not exist."); return false; } if (StringUtils.isNotEmpty(filePattern)) { FileObject[] children = souFile.getChildren(); for (FileObject child : children) { if (FileType.FILE.equals(child.getType())) { copy(child, destination, filePattern, opts, manager); } else if (FileType.FOLDER.equals(child.getType())) { String newSource = source + File.separator + child.getName().getBaseName(); copyFile(newSource, messageContext, opts); } } } else { if (FileType.FILE.equals(souFile.getType())) { String name = souFile.getName().getBaseName(); FileObject outFile = manager.resolveFile(destination + File.separator + name, opts); outFile.copyFrom(souFile, Selectors.SELECT_ALL); } else if (FileType.FOLDER.equals(souFile.getType())) { if (includeParentDirectory) { destFile = manager .resolveFile(destination + File.separator + souFile.getName().getBaseName(), opts); destFile.createFolder(); } destFile.copyFrom(souFile, Selectors.SELECT_ALL); } if (log.isDebugEnabled()) { log.debug("File copying completed from " + source + "to" + destination); } } } catch (FileSystemException e) { throw new SynapseException("Unable to copy a file/folder", e); } finally { manager.close(); } try { souFile.close(); destFile.close(); } catch (FileSystemException e) { log.error("Error while closing the FileObject", e); } return true; }
From source file:org.wso2.carbon.connector.FileCreate.java
/** * Create a file with Apache commons//from w w w. j a v a2s.c o m * * @param source Location of the file/folder * @param content Content in a file * @param encoding Encoding type * @param messageContext The message context that is generated for processing the file * @return Return the status */ private boolean createFile(String source, String content, String encoding, MessageContext messageContext) { boolean resultStatus = false; StandardFileSystemManager manager; try { OutputStream out = null; manager = FileConnectorUtils.getManager(); if (manager != null) { FileObject sourceFile = manager.resolveFile(source, FileConnectorUtils.init(messageContext)); try { if (FileConnectorUtils.isFolder(sourceFile)) { sourceFile.createFolder(); } else { if (StringUtils.isEmpty(content)) { sourceFile.createFile(); } else { FileContent fileContent = sourceFile.getContent(); out = fileContent.getOutputStream(true); if (StringUtils.isEmpty(encoding)) { IOUtils.write(content, out, DEFAULT_ENCODING); } else { IOUtils.write(content, out, encoding); } } } resultStatus = true; if (log.isDebugEnabled()) { log.debug("File creation completed with " + source); } } finally { try { if (sourceFile != null) { sourceFile.close(); } } catch (FileSystemException e) { log.error("Error while closing FileObject: " + e.getMessage(), e); } try { if (out != null) { out.close(); } } catch (IOException e) { log.error("Error while closing OutputStream: " + e.getMessage(), e); } manager.close(); } } } catch (IOException e) { handleException("Unable to create a file/folder.", e, messageContext); } return resultStatus; }
From source file:org.wso2.carbon.connector.FileCreateConnector.java
/** * Create a new file/folder./*from ww w . j av a 2s.c o m*/ * * @param messageContext The message context that is generated for processing the file. * @return true, if the file/folder is successfully created. */ private boolean createFile(MessageContext messageContext) throws FileSystemException { String source = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.FILE_LOCATION); String content = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.CONTENT); String encoding = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.ENCODING); if (StringUtils.isEmpty(encoding)) { encoding = FileConstants.DEFAULT_ENCODING; } StandardFileSystemManager manager = FileConnectorUtils.getManager(); FileObject sourceFile = manager.resolveFile(source, FileConnectorUtils.init(messageContext)); if (FileConnectorUtils.isFolder(sourceFile)) { sourceFile.createFolder(); } else { if (StringUtils.isEmpty(content)) { sourceFile.createFile(); } else { OutputStream out = sourceFile.getContent().getOutputStream(); try { IOUtils.write(content, out, encoding); } catch (IOException e) { throw new SynapseException("Error while writing the file content", e); } finally { try { // close the file object sourceFile.close(); } catch (FileSystemException e) { log.error("Error while closing FileObject", e); } try { if (out != null) { out.close(); } } catch (IOException e) { log.error("Error while closing OutputStream", e); } // close the StandardFileSystemManager manager.close(); } } if (log.isDebugEnabled()) { log.debug("File creation completed with " + source); } } return true; }
From source file:org.wso2.carbon.connector.FileDeleteConnector.java
/** * Delete an existing file/folder./* www. ja v a 2 s . c o m*/ * * @param messageContext The message context that is generated for processing the file. * @return true, if the file/folder is successfully deleted. */ private boolean deleteFile(MessageContext messageContext) { String source = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.FILE_LOCATION); StandardFileSystemManager manager = FileConnectorUtils.getManager(); FileObject remoteFile = null; try { // create remote fileObject remoteFile = manager.resolveFile(source, FileConnectorUtils.init(messageContext)); if (!remoteFile.exists()) { log.error("The file does not exist."); return false; } if (FileType.FILE.equals(remoteFile.getType())) { // delete a file remoteFile.delete(); } else if (FileType.FOLDER.equals(remoteFile.getType())) { String filePattern = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.FILE_PATTERN); if (StringUtils.isNotEmpty(filePattern) && !"*".equals(filePattern)) { FileObject[] children = remoteFile.getChildren(); for (FileObject child : children) { if (child.getName().getBaseName().matches(filePattern)) { child.delete(); } } } else { // delete folder remoteFile.delete(Selectors.SELECT_ALL); } } if (log.isDebugEnabled()) { log.debug("File delete completed with " + source); } } catch (FileSystemException e) { throw new SynapseException("Error while deleting file/folder", e); } finally { // close the StandardFileSystemManager manager.close(); try { if (remoteFile != null) { // close the file object remoteFile.close(); } } catch (FileSystemException e) { log.error("Error while closing the FileObject", e); } } return true; }
From source file:org.wso2.carbon.connector.FileExistConnector.java
/** * Determine if file/folder exists./* ww w. j a v a 2 s . c o m*/ * * @param messageContext The message context that is generated for processing the file. * @return true, if the file/folder exists. */ private boolean isFileExist(MessageContext messageContext) { String source = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.FILE_LOCATION); StandardFileSystemManager manager = FileConnectorUtils.getManager(); FileObject remoteFile = null; try { FileSystemOptions opt = FileConnectorUtils.init(messageContext); // create remote fileObject remoteFile = manager.resolveFile(source, opt); if (!remoteFile.exists()) { return false; } if (log.isDebugEnabled()) { log.debug("File exist completed with. " + source); } } catch (FileSystemException e) { throw new SynapseException("Error while processing a file.", e); } finally { // close the StandardFileSystemManager manager.close(); try { if (remoteFile != null) { // close the file object remoteFile.close(); } } catch (FileSystemException e) { log.error("Error while closing the FileObject", e); } } return true; }
From source file:org.wso2.carbon.connector.FileListZipConnector.java
/** * List all the files inside zip file./*from w w w.j ava2s .com*/ * * @param messageContext The message context that is generated for processing the file. */ private void listAllFiles(MessageContext messageContext) throws FileSystemException { String source = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.FILE_LOCATION); StandardFileSystemManager manager = FileConnectorUtils.getManager(); FileObject remoteFile = manager.resolveFile(source, FileConnectorUtils.init(messageContext)); if (!remoteFile.exists()) { log.error("Zip file location does not exist."); } // open the zip file InputStream input = remoteFile.getContent().getInputStream(); ZipInputStream zip = new ZipInputStream(input); OMFactory factory = OMAbstractFactory.getOMFactory(); OMNamespace ns = factory.createOMNamespace(FileConstants.FILECON, FileConstants.NAMESPACE); OMElement result = factory.createOMElement(FileConstants.RESULT, ns); ZipEntry zipEntry; try { while ((zipEntry = zip.getNextEntry()) != null && !zipEntry.isDirectory()) { // add the entries String outputResult = zipEntry.getName(); OMElement messageElement = factory.createOMElement(FileConstants.FILE, ns); messageElement.setText(outputResult); result.addChild(messageElement); } } catch (IOException e) { throw new SynapseException("Error while reading the next ZIP file entry", e); } finally { try { zip.close(); } catch (IOException e) { log.error("Error while closing ZipInputStream"); } try { remoteFile.close(); } catch (FileSystemException e) { log.error("Error while closing the FileObject", e); } // close the StandardFileSystemManager manager.close(); } ResultPayloadCreator.preparePayload(messageContext, result); if (log.isDebugEnabled()) { log.debug("The envelop body with the read files path is " + messageContext.getEnvelope().getBody().toString()); } }
From source file:org.wso2.carbon.connector.FileMoveConnector.java
/** * Move the file/folder from source to destination directory. * * @param messageContext The message context that is generated for processing the move operation. * @return true, if the file/folder is successfully moved. *//*from ww w . j av a2s . c o m*/ private boolean move(MessageContext messageContext) throws FileSystemException { String source = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.FILE_LOCATION); String destination = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.NEW_FILE_LOCATION); String filePattern = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.FILE_PATTERN); String includeParentDirectory = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.INCLUDE_PARENT_DIRECTORY); boolean includeParentDirectoryParameter = FileConstants.DEFAULT_INCLUDE_PARENT_DIRECTORY; if (StringUtils.isNotEmpty(includeParentDirectory)) { includeParentDirectoryParameter = Boolean.parseBoolean(includeParentDirectory); } StandardFileSystemManager manager = FileConnectorUtils.getManager(); FileSystemOptions opts = FileConnectorUtils.init(messageContext); // Create remote object FileObject remoteFile = manager.resolveFile(source, opts); try { if (!remoteFile.exists()) { log.error("The file/folder location does not exist."); return false; } if (FileType.FILE.equals(remoteFile.getType())) { moveFile(destination, remoteFile, manager, opts); } else { moveFolder(source, destination, filePattern, includeParentDirectoryParameter, manager, opts); } if (log.isDebugEnabled()) { log.debug("File move completed from " + source + " to " + destination); } } catch (FileSystemException e) { throw new SynapseException("Unable to move a file/folder.", e); } finally { // close the StandardFileSystemManager manager.close(); try { remoteFile.close(); } catch (FileSystemException e) { log.error("Error while closing the FileObject", e); } } return true; }