List of usage examples for org.apache.commons.vfs2 FileSystemException getMessage
@Override
public String getMessage()
From source file:org.wso2.carbon.connector.FileCopy.java
/** * Copy files/*w ww. j a va 2s. c om*/ * * @param source Location of the file * @param destination new file location * @param filePattern pattern of the file * @param messageContext The message context that is generated for processing the file * @param opts FileSystemOptions * @return return a resultStatus */ private boolean copyFile(String source, String destination, String filePattern, MessageContext messageContext, FileSystemOptions opts) { boolean resultStatus = false; StandardFileSystemManager manager = null; try { manager = FileConnectorUtils.getManager(); FileObject souFile = manager.resolveFile(source, opts); FileObject destFile = manager.resolveFile(destination, opts); if (StringUtils.isNotEmpty(filePattern)) { FileObject[] children = souFile.getChildren(); for (FileObject child : children) { if (child.getType() == FileType.FILE) { copy(source, destination, filePattern, opts); } else if (child.getType() == FileType.FOLDER) { String newSource = source + File.separator + child.getName().getBaseName(); copyFile(newSource, destination, filePattern, messageContext, opts); } } resultStatus = true; } else { if (souFile.exists()) { if (souFile.getType() == FileType.FILE) { InputStream fileIn = null; OutputStream fileOut = null; try { String name = souFile.getName().getBaseName(); FileObject outFile = manager.resolveFile(destination + File.separator + name, opts); //TODO make parameter sense fileIn = souFile.getContent().getInputStream(); fileOut = outFile.getContent().getOutputStream(); IOUtils.copyLarge(fileIn, fileOut); resultStatus = true; } catch (FileSystemException e) { log.error("Error while copying a file " + e.getMessage()); } finally { try { if (fileOut != null) { fileOut.close(); } } catch (Exception e) { log.error("Error while closing OutputStream: " + e.getMessage(), e); } try { if (fileIn != null) { fileIn.close(); } } catch (Exception e) { log.error("Error while closing InputStream: " + e.getMessage(), e); } } } else if (souFile.getType() == FileType.FOLDER) { destFile.copyFrom(souFile, Selectors.SELECT_ALL); resultStatus = true; } if (log.isDebugEnabled()) { log.debug("File copying completed from " + source + "to" + destination); } } else { log.error("The File Location does not exist."); resultStatus = false; } } return resultStatus; } catch (IOException e) { handleException("Unable to copy a file/folder", e, messageContext); } finally { if (manager != null) { manager.close(); } } return resultStatus; }
From source file:org.wso2.carbon.connector.FileCreate.java
/** * Create a file with Apache commons//from www . ja va2s . com * * @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.FileSearchConnector.java
/** * List the all files of given pattern./*ww w. j a va 2 s. co m*/ * * @param messageContext The message context that is generated for processing the file. */ private void searchFile(MessageContext messageContext) { String source = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.FILE_LOCATION); String filePattern = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.FILE_PATTERN); boolean enableRecursiveSearch = false; String recursiveSearchParameter = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.RECURSIVE_SEARCH); if (StringUtils.isNotEmpty(recursiveSearchParameter)) { enableRecursiveSearch = Boolean.parseBoolean(recursiveSearchParameter); } StandardFileSystemManager manager = FileConnectorUtils.getManager(); if (StringUtils.isEmpty(filePattern)) { throw new SynapseException("FilePattern should not be null"); } try { FileSystemOptions opt = FileConnectorUtils.init(messageContext); FileObject remoteFile = manager.resolveFile(source, opt); if (!remoteFile.exists()) { throw new SynapseException("File location does not exist"); } FileObject[] children = remoteFile.getChildren(); OMFactory factory = OMAbstractFactory.getOMFactory(); OMNamespace ns = factory.createOMNamespace(FileConstants.FILECON, FileConstants.NAMESPACE); OMElement result = factory.createOMElement(FileConstants.RESULT, ns); ResultPayloadCreator.preparePayload(messageContext, result); FilePattenMatcher fpm = new FilePattenMatcher(filePattern); for (FileObject child : children) { try { if (FileType.FILE.equals(child.getType()) && fpm.validate(child.getName().getBaseName())) { String outputResult = child.getName().getPath(); OMElement messageElement = factory.createOMElement(FileConstants.FILE, ns); messageElement.setText(outputResult); result.addChild(messageElement); } else if (FileType.FOLDER.equals(child.getType()) && enableRecursiveSearch) { searchSubFolders(child, filePattern, factory, result, ns); } } catch (FileSystemException e) { throw new SynapseException("Unable to search a file.", e); } finally { try { if (child != null) { child.close(); } } catch (IOException e) { log.error("Error while closing Directory: " + e.getMessage(), e); } } } messageContext.getEnvelope().getBody().addChild(result); } catch (FileSystemException e) { throw new SynapseException("Unable to search a file for a given pattern.", e); } finally { manager.close(); } }
From source file:org.wso2.carbon.connector.FileSearchConnector.java
/** * Search the files of given pattern inside the sub directory. * * @param child Child folder./*from ww w . j a v a 2s . com*/ * @param filePattern Pattern of the file to be searched. * @param factory OMFactory. * @param result OMElement. * @param ns OMNamespace. * @throws FileSystemException On error getting file type. */ private void searchSubFolders(FileObject child, String filePattern, OMFactory factory, OMElement result, OMNamespace ns) throws FileSystemException { List<FileObject> fileList = new ArrayList<>(); Collections.addAll(fileList, child.getChildren()); FilePattenMatcher fpm = new FilePattenMatcher(filePattern); String outputResult; try { for (FileObject file : fileList) { if (FileType.FILE.equals(file.getType())) { if (fpm.validate(file.getName().getBaseName().toLowerCase())) { outputResult = file.getName().getPath(); OMElement messageElement = factory.createOMElement(FileConstants.FILE, ns); messageElement.setText(outputResult); result.addChild(messageElement); } } else if (FileType.FOLDER.equals(file.getType())) { searchSubFolders(file, filePattern, factory, result, ns); } } } catch (FileSystemException e) { throw new SynapseException("Unable to search files in sub folder", e); } finally { try { child.close(); } catch (IOException e) { log.error("Error while closing Directory: " + e.getMessage(), e); } } }
From source file:org.wso2.carbon.connector.util.FileConnectorUtils.java
public static StandardFileSystemManager getManager() { StandardFileSystemManager fsm = null; try {/*from ww w .ja v a2s . com*/ fsm = new StandardFileSystemManager(); fsm.init(); } catch (FileSystemException e) { log.error("Unable to get FileSystemManager: " + e.getMessage(), e); } return fsm; }
From source file:org.wso2.carbon.inbound.endpoint.protocol.file.FilePollingConsumer.java
/** * //from w ww . j a va 2 s .co m * Do the file processing operation for the given set of properties. Do the * checks and pass the control to processFile method * * */ public FileObject poll() { if (fileURI == null || fileURI.trim().equals("")) { log.error("Invalid file url. Check the inbound endpoint configuration. Endpoint Name : " + name + ", File URL : " + VFSUtils.maskURLPassword(fileURI)); return null; } if (log.isDebugEnabled()) { log.debug("Start : Scanning directory or file : " + VFSUtils.maskURLPassword(fileURI)); } if (!initFileCheck()) { // Unable to read from the source location provided. return null; } // If file/folder found proceed to the processing stage try { lastCycle = 0; if (fileObject.exists() && fileObject.isReadable()) { FileObject[] children = null; try { children = fileObject.getChildren(); } catch (FileNotFolderException ignored) { if (log.isDebugEnabled()) { log.debug("No Folder found. Only file found on : " + VFSUtils.maskURLPassword(fileURI)); } } catch (FileSystemException ex) { log.error(ex.getMessage(), ex); } // if this is a file that would translate to a single message if (children == null || children.length == 0) { // Fail record is a one that is processed but was not moved // or deleted due to an error. boolean isFailedRecord = VFSUtils.isFailRecord(fsManager, fileObject); if (!isFailedRecord) { fileHandler(); if (injectHandler == null) { return fileObject; } } else { try { lastCycle = 2; moveOrDeleteAfterProcessing(fileObject); } catch (SynapseException synapseException) { log.error("File object '" + VFSUtils.maskURLPassword(fileObject.getURL().toString()) + "' " + "cloud not be moved after first attempt", synapseException); } if (fileLock) { // TODO: passing null to avoid build break. Fix properly VFSUtils.releaseLock(fsManager, fileObject, fso); } if (log.isDebugEnabled()) { log.debug("File '" + VFSUtils.maskURLPassword(fileObject.getURL().toString()) + "' has been marked as a failed" + " record, it will not process"); } } } else { FileObject fileObject = directoryHandler(children); if (fileObject != null) { return fileObject; } } } else { log.warn("Unable to access or read file or directory : " + VFSUtils.maskURLPassword(fileURI) + "." + " Reason: " + (fileObject.exists() ? (fileObject.isReadable() ? "Unknown reason" : "The file can not be read!") : "The file does not exists!")); return null; } } catch (FileSystemException e) { log.error("Error checking for existence and readability : " + VFSUtils.maskURLPassword(fileURI), e); return null; } catch (Exception e) { log.error("Error while processing the file/folder in URL : " + VFSUtils.maskURLPassword(fileURI), e); return null; } finally { try { if (fsManager != null) { fsManager.closeFileSystem(fileObject.getParent().getFileSystem()); } fileObject.close(); } catch (Exception e) { log.error("Unable to close the file system. " + e.getMessage()); log.error(e); } } if (log.isDebugEnabled()) { log.debug("End : Scanning directory or file : " + VFSUtils.maskURLPassword(fileURI)); } return null; }
From source file:org.wso2.carbon.inbound.endpoint.protocol.file.FilePollingConsumer.java
/** * Check if the file/folder exists before proceeding and retrying *//*w w w . j a va 2s. co m*/ private boolean initFileCheck() { boolean wasError = true; int retryCount = 0; fileObject = null; while (wasError) { try { retryCount++; fileObject = fsManager.resolveFile(fileURI, fso); if (fileObject == null) { log.error("fileObject is null"); throw new FileSystemException("fileObject is null"); } wasError = false; } catch (FileSystemException e) { if (retryCount >= maxRetryCount) { log.error("Repeatedly failed to resolve the file URI: " + VFSUtils.maskURLPassword(fileURI), e); return false; } else { log.warn("Failed to resolve the file URI: " + VFSUtils.maskURLPassword(fileURI) + ", in attempt " + retryCount + ", " + e.getMessage() + " Retrying in " + reconnectionTimeout + " milliseconds."); } } if (wasError) { try { Thread.sleep(reconnectionTimeout); } catch (InterruptedException e2) { log.error("Thread was interrupted while waiting to reconnect.", e2); } } } return true; }
From source file:org.wso2.carbon.mediation.connector.pmode.PModeRepository.java
@Override public void fileCreated(FileChangeEvent fileChangeEvent) throws Exception { try {/*from www .j av a 2 s. c o m*/ if (isProcessingRequired(fileChangeEvent)) { InputStream inputStream = fileChangeEvent.getFile().getContent().getInputStream(); PMode pMode = (PMode) this.pModeUnmarshaller.unmarshal(inputStream); validatePMode(pMode); addUpdateRemovePMode(Operation.ADD, fileChangeEvent.getFile().getName().getPathDecoded(), pMode); } } catch (FileSystemException e) { log.warn("File system exception occurred while dynamically updating PModes, " + e.getMessage(), e); } catch (JAXBException e) { log.warn("JAXB exception occurred while dynamically updating PModes, " + e.getMessage(), e); } catch (AxisFault e) { log.warn("SynapseException occurred while dynamically updating PModes, " + e.getMessage(), e); } catch (Exception e) { log.warn("exception occurred while dynamically updating PModes, " + e.getMessage(), e); } }
From source file:org.wso2.carbon.mediation.connector.pmode.PModeRepository.java
@Override public void fileChanged(FileChangeEvent fileChangeEvent) throws Exception { try {/*from ww w . j a v a 2 s . co m*/ if (isProcessingRequired(fileChangeEvent)) { InputStream inputStream = fileChangeEvent.getFile().getContent().getInputStream(); PMode pMode = (PMode) this.pModeUnmarshaller.unmarshal(inputStream); validatePMode(pMode); addUpdateRemovePMode(Operation.UPDATE, fileChangeEvent.getFile().getName().getPathDecoded(), pMode); } } catch (FileSystemException e) { log.warn("File system exception occurred while dynamically updating PModes, " + e.getMessage(), e); } catch (JAXBException e) { log.warn("JAXB exception occurred while dynamically updating PModes, " + e.getMessage(), e); } catch (AxisFault e) { log.warn("AxisFault occurred while dynamically updating PModes, " + e.getMessage(), e); } catch (Exception e) { log.warn("Exception occurred while dynamically updating PModes, " + e.getMessage(), e); } }
From source file:org.wso2.carbon.transport.filesystem.connector.server.FileSystemProcessor.java
/** * The runnable implementation which is invoked when file processing started. *///from w w w. j a v a 2s . co m @Override public void run() { String uri = file.getName().getURI(); uri = uri.startsWith("file://") ? uri.replace("file://", "") : uri; TextCarbonMessage message = new TextCarbonMessage(uri); try { String protocol = file.getURL().getProtocol(); // Since there is a separate module for File Server Connector, if the protocol is file, mark it as fs if (Constants.PROTOCOL_FILE.equals(protocol)) { protocol = Constants.PROTOCOL_FILE_SYSTEM; } message.setProperty(org.wso2.carbon.messaging.Constants.PROTOCOL, protocol); } catch (FileSystemException e) { logger.error("Exception occurred while retrieving the file protocol", e); message.setProperty(org.wso2.carbon.messaging.Constants.PROTOCOL, Constants.PROTOCOL_FILE_SYSTEM); } message.setProperty(Constants.FILE_TRANSPORT_PROPERTY_SERVICE_NAME, serviceName); try { message.setProperty(Constants.META_FILE_SIZE, file.getContent().getSize()); message.setProperty(Constants.META_FILE_LAST_MODIFIED_TIME, file.getContent().getLastModifiedTime()); } catch (FileSystemException e) { logger.error("Failed to set meta data for file: " + file.getName().getURI(), e); } boolean processFailed = false; FileSystemServerConnectorCallback callback = new FileSystemServerConnectorCallback(); try { messageProcessor.receive(message, callback); } catch (Exception e) { logger.warn("Failed to send stream from file: " + FileTransportUtils.maskURLPassword(fileURI) + " to message processor. ", e); } try { callback.waitTillDone(timeOutInterval, fileURI); } catch (InterruptedException e) { logger.warn("Interrupted while waiting for message processor to consume" + " the file input stream. Aborting processing of file: " + FileTransportUtils.maskURLPassword(fileURI), e); } catch (FileSystemServerConnectorException e) { logger.warn(e.getMessage()); processFailed = true; } if (postProcessAction.equals(Constants.ACTION_NONE)) { fileSystemConsumer.markProcessed(fileURI); } else { try { fileSystemConsumer.postProcess(file, processFailed); } catch (FileSystemServerConnectorException e) { logger.error("File object '" + FileTransportUtils.maskURLPassword(file.getName().toString()) + "' " + "could not be moved", e); } } FileTransportUtils.releaseLock(file); if (logger.isDebugEnabled()) { logger.debug("Released the lock file '" + FileTransportUtils.maskURLPassword(file.toString()) + ".lock' of the file '" + FileTransportUtils.maskURLPassword(file.toString())); } //close the file system after processing try { file.close(); } catch (FileSystemException e) { logger.warn("Could not close the file: " + file.getName().getPath(), e); } }