List of usage examples for org.apache.commons.vfs2 FileObject getName
FileName getName();
From source file:org.wso2.carbon.transport.filesystem.connector.server.FileSystemConsumer.java
/** * Handle directory with child elements. * * @param children The array containing child elements of a folder *///from w w w.j a v a 2 s .co m private void directoryHandler(FileObject[] children) throws FileSystemServerConnectorException { // Sort the files according to given properties String strSortParam = fileProperties.get(Constants.FILE_SORT_PARAM); // TODO: rethink the way the string constants are handled if (strSortParam != null && !"NONE".equals(strSortParam)) { if (log.isDebugEnabled()) { log.debug("Starting to sort the files in folder: " + FileTransportUtils.maskURLPassword(listeningDirURI)); } String strSortOrder = fileProperties.get(Constants.FILE_SORT_ORDER); boolean bSortOrderAscending = true; if (strSortOrder != null) { bSortOrderAscending = Boolean.parseBoolean(strSortOrder); } if (log.isDebugEnabled()) { log.debug("Sorting the files by : " + strSortOrder + ". (" + bSortOrderAscending + ")"); } switch (strSortParam) { case Constants.FILE_SORT_VALUE_NAME: if (bSortOrderAscending) { Arrays.sort(children, new FileNameAscComparator()); } else { Arrays.sort(children, new FileNameDesComparator()); } break; case Constants.FILE_SORT_VALUE_SIZE: if (bSortOrderAscending) { Arrays.sort(children, new FileSizeAscComparator()); } else { Arrays.sort(children, new FileSizeDesComparator()); } break; case Constants.FILE_SORT_VALUE_LASTMODIFIEDTIMESTAMP: if (bSortOrderAscending) { Arrays.sort(children, new FileLastmodifiedtimestampAscComparator()); } else { Arrays.sort(children, new FileLastmodifiedtimestampDesComparator()); } break; default: log.warn("Invalid value given for " + Constants.FILE_SORT_PARAM + " parameter. " + " Expected one of the values: " + Constants.FILE_SORT_VALUE_NAME + ", " + Constants.FILE_SORT_VALUE_SIZE + " or " + Constants.FILE_SORT_VALUE_LASTMODIFIEDTIMESTAMP + ". Found: " + strSortParam); break; } if (log.isDebugEnabled()) { log.debug("End sorting the files."); } } for (FileObject child : children) { if (fileProcessCount != 0 && processCount > fileProcessCount) { return; } if (child.getName().getBaseName().endsWith(".lock") || child.getName().getBaseName().endsWith(".fail")) { continue; } if (!(fileNamePattern == null || child.getName().getBaseName().matches(fileNamePattern))) { if (log.isDebugEnabled()) { log.debug("File " + FileTransportUtils.maskURLPassword(listeningDir.getName().getBaseName()) + " is not processed because it did not match the specified pattern."); } } else { FileType childType = getFileType(child); if (childType == FileType.FOLDER) { FileObject[] c = null; try { c = child.getChildren(); } catch (FileSystemException ignored) { if (log.isDebugEnabled()) { log.debug("The file does not exist, or is not a folder, or an error " + "has occurred when trying to list the children. File URI : " + FileTransportUtils.maskURLPassword(listeningDirURI), ignored); } } // if this is a file that would translate to a single message if (c == null || c.length == 0) { if (log.isDebugEnabled()) { log.debug("Folder at " + FileTransportUtils.maskURLPassword(child.getName().getURI()) + " is empty."); } } else { directoryHandler(c); } postProcess(child, false); } else { fileHandler(child); } } } }
From source file:org.wso2.carbon.transport.filesystem.connector.server.FileSystemConsumer.java
/** * Process a single file.// ww w .j a v a 2s . c o m * * @param file A single file to be processed */ private void fileHandler(FileObject file) { String uri = file.getName().getURI(); synchronized (this) { if (postProcessAction.equals(Constants.ACTION_NONE) && processed.contains(uri)) { if (log.isDebugEnabled()) { log.debug("The file: " + FileTransportUtils.maskURLPassword(uri) + " is already processed"); } return; } } if (!postProcessAction.equals(Constants.ACTION_NONE) && isFailRecord(file)) { // it is a failed record try { postProcess(file, false); } catch (FileSystemServerConnectorException e) { log.error("File object '" + FileTransportUtils.maskURLPassword(uri) + "'could not complete action " + postProcessAction + ", will remain in \"fail\" state", e); } } else { if (FileTransportUtils.acquireLock(fsManager, file, fso)) { if (log.isInfoEnabled()) { log.info( "Processing file :" + FileTransportUtils.maskURLPassword(file.getName().getBaseName())); } FileSystemProcessor fsp = new FileSystemProcessor(messageProcessor, serviceName, file, timeOutInterval, uri, this, postProcessAction); fsp.startProcessThread(); processCount++; } else { log.warn("Couldn't get the lock for processing the file: " + FileTransportUtils.maskURLPassword(file.getName().toString())); } } }
From source file:org.wso2.carbon.transport.filesystem.connector.server.FileSystemConsumer.java
/** * Do the post processing actions./* w ww. j ava 2 s .c o m*/ * * @param file The file object which needs to be post processed * @param processFailed Whether processing of file failed */ synchronized void postProcess(FileObject file, boolean processFailed) throws FileSystemServerConnectorException { String moveToDirectoryURI = null; FileType fileType = getFileType(file); if (!processFailed) { if (postProcessAction.equals(Constants.ACTION_MOVE)) { moveToDirectoryURI = fileProperties.get(Constants.MOVE_AFTER_PROCESS); } } else { if (postFailureAction.equals(Constants.ACTION_MOVE)) { moveToDirectoryURI = fileProperties.get(Constants.MOVE_AFTER_FAILURE); } } if (moveToDirectoryURI != null) { try { if (getFileType(fsManager.resolveFile(moveToDirectoryURI, fso)) == FileType.FILE) { moveToDirectoryURI = null; if (processFailed) { postFailureAction = Constants.ACTION_NONE; } else { postProcessAction = Constants.ACTION_NONE; } if (log.isDebugEnabled()) { log.debug( "Cannot move file because provided location is not a folder. File is kept at source"); } } } catch (FileSystemException e) { errorHandler .handleError( new FileSystemServerConnectorException( "Error occurred when resolving move destination file: " + FileTransportUtils.maskURLPassword(listeningDirURI), e), null, null); } } try { if (!(moveToDirectoryURI == null || fileType == FileType.FOLDER)) { FileObject moveToDirectory; String relativeName = file.getName().getURI().split(listeningDir.getName().getURI())[1]; int index = relativeName.lastIndexOf(File.separator); moveToDirectoryURI += relativeName.substring(0, index); moveToDirectory = fsManager.resolveFile(moveToDirectoryURI, fso); String prefix; if (fileProperties.get(Constants.MOVE_TIMESTAMP_FORMAT) != null) { prefix = new SimpleDateFormat(fileProperties.get(Constants.MOVE_TIMESTAMP_FORMAT)) .format(new Date()); } else { prefix = ""; } //Forcefully create the folder(s) if does not exists String strForceCreateFolder = fileProperties.get(Constants.FORCE_CREATE_FOLDER); if (strForceCreateFolder != null && strForceCreateFolder.equalsIgnoreCase("true") && !moveToDirectory.exists()) { moveToDirectory.createFolder(); } FileObject dest = moveToDirectory.resolveFile(prefix + file.getName().getBaseName()); if (log.isDebugEnabled()) { log.debug("Moving to file :" + FileTransportUtils.maskURLPassword(dest.getName().getURI())); } if (log.isInfoEnabled()) { log.info("Moving file :" + FileTransportUtils.maskURLPassword(file.getName().getBaseName())); } try { file.moveTo(dest); if (isFailRecord(file)) { releaseFail(file); } } catch (FileSystemException e) { if (!isFailRecord(file)) { markFailRecord(file); } errorHandler.handleError( new FileSystemServerConnectorException( "Error moving file : " + FileTransportUtils.maskURLPassword(file.toString()) + " to " + FileTransportUtils.maskURLPassword(moveToDirectoryURI), e), null, null); } } else { if (log.isDebugEnabled()) { log.debug("Deleting file :" + FileTransportUtils.maskURLPassword(file.getName().getBaseName())); } if (log.isInfoEnabled()) { log.info("Deleting file :" + FileTransportUtils.maskURLPassword(file.getName().getBaseName())); } try { if (!file.delete()) { if (log.isDebugEnabled()) { log.debug("Could not delete file : " + FileTransportUtils.maskURLPassword(file.getName().getBaseName())); } } else { if (isFailRecord(file)) { releaseFail(file); } } } catch (FileSystemException e) { errorHandler.handleError( new FileSystemServerConnectorException("Could not delete file : " + FileTransportUtils.maskURLPassword(file.getName().getBaseName()), e), null, null); } } } catch (FileSystemException e) { if (!isFailRecord(file)) { markFailRecord(file); errorHandler.handleError( new FileSystemServerConnectorException("Error resolving directory to move file : " + FileTransportUtils.maskURLPassword(moveToDirectoryURI), e), null, null); } } }
From source file:org.wso2.carbon.transport.filesystem.connector.server.FileSystemConsumer.java
/** * Determine whether file object is a file or a folder. * * @param fileObject File to get the type of * @return FileType of given file *//*w ww .j av a2 s . co m*/ private FileType getFileType(FileObject fileObject) throws FileSystemServerConnectorException { try { return fileObject.getType(); } catch (FileSystemException e) { errorHandler.handleError( new FileSystemServerConnectorException("Error occurred when determining whether file: " + FileTransportUtils.maskURLPassword(fileObject.getName().getURI()) + " is a file or a folder", e), null, null); } return FileType.IMAGINARY; }
From source file:org.wso2.carbon.transport.filesystem.connector.server.FileSystemConsumer.java
/** * Mark a record as a failed record./*w w w .ja v a 2 s. c o m*/ * * @param fo File to be marked as failed */ private synchronized void markFailRecord(FileObject fo) { String fullPath = fo.getName().getURI(); if (failed.contains(fullPath)) { if (log.isDebugEnabled()) { log.debug("File: " + FileTransportUtils.maskURLPassword(fullPath) + " is already marked as a failed record."); } return; } failed.add(fullPath); }
From source file:org.wso2.carbon.transport.filesystem.connector.server.FileSystemConsumer.java
/** * Determine whether a file is a failed record. * * @param fo File to determine whether failed * @return true if file is a failed file *//*from w w w .j a v a 2 s .c o m*/ private boolean isFailRecord(FileObject fo) { return failed.contains(fo.getName().getURI()); }
From source file:org.wso2.carbon.transport.filesystem.connector.server.FileSystemConsumer.java
/** * Releases a file from its failed state. * * @param fo File to release from failed state *//* ww w. j a v a 2 s . c o m*/ private synchronized void releaseFail(FileObject fo) { String fullPath = fo.getName().getURI(); failed.remove(fullPath); }
From source file:org.wso2.carbon.transport.filesystem.connector.server.util.FileTransportUtils.java
/** * Acquire the file level locking.//from ww w.j a va2 s . c o 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.filesystem.connector.server.util.FileTransportUtils.java
/** * Release a file item lock acquired at the start of processing. * * @param fileObject File that needs the lock to be removed *///from w w w. jav a 2 s . co m public static synchronized void releaseLock(FileObject fileObject) { String fullPath = fileObject.getName().getURI(); processing.remove(fullPath); }
From source file:org.wso2.carbon.transport.remotefilesystem.client.connector.contractimpl.VFSClientConnectorImpl.java
@Override public void send(RemoteFileSystemMessage message) { FtpFileSystemConfigBuilder.getInstance().setPassiveMode(opts, true); String fileURI = connectorConfig.get(Constants.URI); String action = connectorConfig.get(Constants.ACTION); FileType fileType;//from ww w . j a v a 2 s. c om ByteBuffer byteBuffer; InputStream inputStream = null; OutputStream outputStream = null; FileObject path = null; try { FileSystemManager fsManager = VFS.getManager(); path = fsManager.resolveFile(fileURI, opts); fileType = path.getType(); switch (action) { case Constants.CREATE: boolean isFolder = Boolean .parseBoolean(connectorConfig.getOrDefault(Constants.CREATE_FOLDER, "false")); if (path.exists()) { throw new RemoteFileSystemConnectorException("File already exists: " + path.getName().getURI()); } if (isFolder) { path.createFolder(); } else { path.createFile(); } break; case Constants.WRITE: if (!path.exists()) { path.createFile(); path.refresh(); fileType = path.getType(); } if (fileType == FileType.FILE) { byteBuffer = message.getBytes(); byte[] bytes = byteBuffer.array(); if (connectorConfig.get(Constants.APPEND) != null) { outputStream = path.getContent() .getOutputStream(Boolean.parseBoolean(connectorConfig.get(Constants.APPEND))); } else { outputStream = path.getContent().getOutputStream(); } outputStream.write(bytes); outputStream.flush(); } break; case Constants.DELETE: if (path.exists()) { int filesDeleted = path.delete(Selectors.SELECT_ALL); if (logger.isDebugEnabled()) { logger.debug(filesDeleted + " files successfully deleted"); } } else { throw new RemoteFileSystemConnectorException( "Failed to delete file: " + path.getName().getURI() + " not found"); } break; case Constants.COPY: if (path.exists()) { String destination = connectorConfig.get(Constants.DESTINATION); FileObject dest = fsManager.resolveFile(destination, opts); dest.copyFrom(path, Selectors.SELECT_ALL); } else { throw new RemoteFileSystemConnectorException( "Failed to copy file: " + path.getName().getURI() + " not found"); } break; case Constants.MOVE: if (path.exists()) { //TODO: Improve this to fix issue #331 String destination = connectorConfig.get(Constants.DESTINATION); FileObject newPath = fsManager.resolveFile(destination, opts); FileObject parent = newPath.getParent(); if (parent != null && !parent.exists()) { parent.createFolder(); } if (!newPath.exists()) { path.moveTo(newPath); } else { throw new RemoteFileSystemConnectorException("The file at " + newPath.getURL().toString() + " already exists or it is a directory"); } } else { throw new RemoteFileSystemConnectorException( "Failed to move file: " + path.getName().getURI() + " not found"); } break; case Constants.READ: if (path.exists()) { //TODO: Do not assume 'path' always refers to a file inputStream = path.getContent().getInputStream(); byte[] bytes = toByteArray(inputStream); RemoteFileSystemMessage fileContent = new RemoteFileSystemMessage(ByteBuffer.wrap(bytes)); remoteFileSystemListener.onMessage(fileContent); } else { throw new RemoteFileSystemConnectorException( "Failed to read file: " + path.getName().getURI() + " not found"); } break; case Constants.EXISTS: RemoteFileSystemMessage fileContent = new RemoteFileSystemMessage(Boolean.toString(path.exists())); remoteFileSystemListener.onMessage(fileContent); break; default: break; } remoteFileSystemListener.done(); } catch (RemoteFileSystemConnectorException | IOException e) { remoteFileSystemListener.onError(e); } finally { if (path != null) { try { path.close(); } catch (FileSystemException e) { //Do nothing. } } closeQuietly(inputStream); closeQuietly(outputStream); } }