List of usage examples for org.apache.commons.vfs2 FileObject exists
boolean exists() throws FileSystemException;
From source file:org.apache.synapse.commons.vfs.VFSUtils.java
public synchronized static void markFailRecord(FileSystemManager fsManager, FileObject fo) { // generate a random fail value to ensure that there are no two parties // processing the same file Random random = new Random(); byte[] failValue = (Long.toString((new Date()).getTime())).getBytes(); try {//from w w w . j a va 2 s . co m String fullPath = fo.getName().getURI(); int pos = fullPath.indexOf("?"); if (pos != -1) { fullPath = fullPath.substring(0, pos); } FileObject failObject = fsManager.resolveFile(fullPath + ".fail"); if (!failObject.exists()) { failObject.createFile(); } // write a lock file before starting of the processing, to ensure that the // item is not processed by any other parties OutputStream stream = failObject.getContent().getOutputStream(); try { stream.write(failValue); stream.flush(); stream.close(); } catch (IOException e) { failObject.delete(); log.error("Couldn't create the fail file before processing the file " + maskURLPassword(fullPath), e); } finally { failObject.close(); } } catch (FileSystemException fse) { log.error("Cannot get the lock for the file : " + maskURLPassword(fo.getName().getURI()) + " before processing"); } }
From source file:org.apache.synapse.commons.vfs.VFSUtils.java
public static boolean isFailRecord(FileSystemManager fsManager, FileObject fo) { try {/*from ww w .j a v a2s .c o m*/ String fullPath = fo.getName().getURI(); int pos = fullPath.indexOf("?"); if (pos > -1) { fullPath = fullPath.substring(0, pos); } FileObject failObject = fsManager.resolveFile(fullPath + ".fail"); if (failObject.exists()) { return true; } } catch (FileSystemException e) { log.error("Couldn't release the fail for the file : " + maskURLPassword(fo.getName().getURI())); } return false; }
From source file:org.apache.synapse.commons.vfs.VFSUtils.java
public static void releaseFail(FileSystemManager fsManager, FileObject fo) { try {//w ww . jav a2 s .c o m String fullPath = fo.getName().getURI(); int pos = fullPath.indexOf("?"); if (pos > -1) { fullPath = fullPath.substring(0, pos); } FileObject failObject = fsManager.resolveFile(fullPath + ".fail"); if (failObject.exists()) { failObject.delete(); } } catch (FileSystemException e) { log.error("Couldn't release the fail for the file : " + maskURLPassword(fo.getName().getURI())); } }
From source file:org.apache.synapse.transport.vfs.VFSTransportListener.java
/** * Search for files that match the given regex pattern and create a list * Then process each of these files and update the status of the scan on * the poll table/*from ww w . j av a 2s .c om*/ * @param entry the poll table entry for the scan * @param fileURI the file or directory to be scanned */ private void scanFileOrDirectory(final PollTableEntry entry, String fileURI) { FileSystemOptions fso = null; setFileSystemClosed(false); try { fso = VFSUtils.attachFileSystemOptions(entry.getVfsSchemeProperties(), fsManager); } catch (Exception e) { log.error("Error while attaching VFS file system properties. " + e.getMessage()); } FileObject fileObject = null; //TODO : Trying to make the correct URL out of the malformed one. if (fileURI.contains("vfs:")) { fileURI = fileURI.substring(fileURI.indexOf("vfs:") + 4); } if (log.isDebugEnabled()) { log.debug("Scanning directory or file : " + VFSUtils.maskURLPassword(fileURI)); } boolean wasError = true; int retryCount = 0; int maxRetryCount = entry.getMaxRetryCount(); long reconnectionTimeout = entry.getReconnectTimeout(); 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) { processFailure( "Repeatedly failed to resolve the file URI: " + VFSUtils.maskURLPassword(fileURI), e, entry); closeFileSystem(fileObject); return; } 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); } } } try { if (fileObject.exists() && fileObject.isReadable()) { entry.setLastPollState(PollTableEntry.NONE); FileObject[] children = null; try { children = fileObject.getChildren(); } catch (FileNotFolderException ignored) { } 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) { boolean isFailedRecord = false; if (entry.getMoveAfterMoveFailure() != null) { isFailedRecord = isFailedRecord(fileObject, entry); } if (fileObject.getType() == FileType.FILE && !isFailedRecord) { boolean runPostProcess = true; if (!entry.isFileLockingEnabled() || (entry.isFileLockingEnabled() && acquireLock(fsManager, fileObject, entry, fso))) { try { if (fileObject.getType() == FileType.FILE) { processFile(entry, fileObject); entry.setLastPollState(PollTableEntry.SUCCSESSFUL); metrics.incrementMessagesReceived(); } else { runPostProcess = false; } } catch (AxisFault e) { if (e.getCause() instanceof FileNotFoundException) { log.warn("Error processing File URI : " + VFSUtils.maskURLPassword(fileObject.getName().toString()) + ". This can be due to file moved from another process."); runPostProcess = false; } else { logException("Error processing File URI : " + VFSUtils.maskURLPassword(fileObject.getName().getURI()), e); entry.setLastPollState(PollTableEntry.FAILED); metrics.incrementFaultsReceiving(); } } if (runPostProcess) { try { moveOrDeleteAfterProcessing(entry, fileObject, fso); } catch (AxisFault axisFault) { logException("File object '" + VFSUtils.maskURLPassword(fileObject.getURL().toString()) + "' " + "cloud not be moved", axisFault); entry.setLastPollState(PollTableEntry.FAILED); String timeStamp = VFSUtils .getSystemTime(entry.getFailedRecordTimestampFormat()); addFailedRecord(entry, fileObject, timeStamp); } } if (entry.isFileLockingEnabled()) { VFSUtils.releaseLock(fsManager, fileObject, fso); if (log.isDebugEnabled()) { log.debug("Removed the lock file '" + VFSUtils.maskURLPassword(fileObject.toString()) + ".lock' of the file '" + VFSUtils.maskURLPassword(fileObject.toString())); } } } else if (log.isDebugEnabled()) { log.debug("Couldn't get the lock for processing the file : " + VFSUtils.maskURLPassword(fileObject.getName().getURI())); } else if (isFailedRecord) { if (entry.isFileLockingEnabled()) { VFSUtils.releaseLock(fsManager, fileObject, fso); } // schedule a cleanup task if the file is there if (fsManager.resolveFile(fileObject.getURL().toString(), fso) != null && removeTaskState == STATE_STOPPED && entry.getMoveAfterMoveFailure() != null) { workerPool.execute(new FileRemoveTask(entry, 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 { int failCount = 0; int successCount = 0; int processCount = 0; Integer iFileProcessingInterval = entry.getFileProcessingInterval(); Integer iFileProcessingCount = entry.getFileProcessingCount(); if (log.isDebugEnabled()) { log.debug("File name pattern : " + entry.getFileNamePattern()); } // Sort the files String strSortParam = entry.getFileSortParam(); if (strSortParam != null) { log.debug("Start Sorting the files."); boolean bSortOrderAsscending = entry.isFileSortAscending(); if (log.isDebugEnabled()) { log.debug( "Sorting the files by : " + strSortParam + ". (" + bSortOrderAsscending + ")"); } if (strSortParam.equals(VFSConstants.FILE_SORT_VALUE_NAME) && bSortOrderAsscending) { Arrays.sort(children, new FileNameAscComparator()); } else if (strSortParam.equals(VFSConstants.FILE_SORT_VALUE_NAME) && !bSortOrderAsscending) { Arrays.sort(children, new FileNameDesComparator()); } else if (strSortParam.equals(VFSConstants.FILE_SORT_VALUE_SIZE) && bSortOrderAsscending) { Arrays.sort(children, new FileSizeAscComparator()); } else if (strSortParam.equals(VFSConstants.FILE_SORT_VALUE_SIZE) && !bSortOrderAsscending) { Arrays.sort(children, new FileSizeDesComparator()); } else if (strSortParam.equals(VFSConstants.FILE_SORT_VALUE_LASTMODIFIEDTIMESTAMP) && bSortOrderAsscending) { Arrays.sort(children, new FileLastmodifiedtimestampAscComparator()); } else if (strSortParam.equals(VFSConstants.FILE_SORT_VALUE_LASTMODIFIEDTIMESTAMP) && !bSortOrderAsscending) { Arrays.sort(children, new FileLastmodifiedtimestampDesComparator()); } log.debug("End Sorting the files."); } for (FileObject child : children) { //skipping *.lock file if (child.getName().getBaseName().endsWith(".lock")) { continue; } boolean isFailedRecord = false; if (entry.getMoveAfterMoveFailure() != null) { isFailedRecord = isFailedRecord(child, entry); } if (entry.getFileNamePattern() != null && child.getName().getBaseName().matches(entry.getFileNamePattern())) { //child's file name matches the file name pattern //now we try to get the lock and process if (log.isDebugEnabled()) { log.debug("Matching file : " + child.getName().getBaseName()); } boolean runPostProcess = true; if ((!entry.isFileLockingEnabled() || (entry.isFileLockingEnabled() && VFSUtils.acquireLock(fsManager, child, fso))) && !isFailedRecord) { //process the file try { if (log.isDebugEnabled()) { log.debug("Processing file :" + VFSUtils.maskURLPassword(child.toString())); } processCount++; if (child.getType() == FileType.FILE) { processFile(entry, child); successCount++; // tell moveOrDeleteAfterProcessing() file was success entry.setLastPollState(PollTableEntry.SUCCSESSFUL); metrics.incrementMessagesReceived(); } else { runPostProcess = false; } } catch (Exception e) { if (e.getCause() instanceof FileNotFoundException) { log.warn("Error processing File URI : " + VFSUtils.maskURLPassword(child.getName().toString()) + ". This can be due to file moved from another process."); runPostProcess = false; } else { logException("Error processing File URI : " + VFSUtils.maskURLPassword(child.getName().getURI()), e); failCount++; // tell moveOrDeleteAfterProcessing() file failed entry.setLastPollState(PollTableEntry.FAILED); metrics.incrementFaultsReceiving(); } } //skipping un-locking file if failed to do delete/move after process boolean skipUnlock = false; if (runPostProcess) { try { moveOrDeleteAfterProcessing(entry, child, fso); } catch (AxisFault axisFault) { logException( "File object '" + VFSUtils.maskURLPassword(child.getURL().toString()) + "'cloud not be moved, will remain in \"locked\" state", axisFault); skipUnlock = true; failCount++; entry.setLastPollState(PollTableEntry.FAILED); String timeStamp = VFSUtils .getSystemTime(entry.getFailedRecordTimestampFormat()); addFailedRecord(entry, child, timeStamp); } } // if there is a failure or not we'll try to release the lock if (entry.isFileLockingEnabled() && !skipUnlock) { VFSUtils.releaseLock(fsManager, child, fso); } } } else if (entry.getFileNamePattern() != null && !child.getName().getBaseName().matches(entry.getFileNamePattern())) { //child's file name does not match the file name pattern if (log.isDebugEnabled()) { log.debug("Non-Matching file : " + child.getName().getBaseName()); } } else if (isFailedRecord) { //it is a failed record if (entry.isFileLockingEnabled()) { VFSUtils.releaseLock(fsManager, child, fso); VFSUtils.releaseLock(fsManager, fileObject, fso); } if (fsManager.resolveFile(child.getURL().toString()) != null && removeTaskState == STATE_STOPPED && entry.getMoveAfterMoveFailure() != null) { workerPool.execute(new FileRemoveTask(entry, child, fso)); } if (log.isDebugEnabled()) { log.debug("File '" + VFSUtils.maskURLPassword(fileObject.getURL().toString()) + "' has been marked as a failed record, it will not " + "process"); } } if (iFileProcessingInterval != null && iFileProcessingInterval > 0) { try { if (log.isDebugEnabled()) { log.debug("Put the VFS processor to sleep for : " + iFileProcessingInterval); } Thread.sleep(iFileProcessingInterval); } catch (InterruptedException ie) { log.error("Unable to set the interval between file processors." + ie); } } else if (iFileProcessingCount != null && iFileProcessingCount <= processCount) { break; } } if (failCount == 0 && successCount > 0) { entry.setLastPollState(PollTableEntry.SUCCSESSFUL); } else if (successCount == 0 && failCount > 0) { entry.setLastPollState(PollTableEntry.FAILED); } else { entry.setLastPollState(PollTableEntry.WITH_ERRORS); } } // processing of this poll table entry is complete long now = System.currentTimeMillis(); entry.setLastPollTime(now); entry.setNextPollTime(now + entry.getPollInterval()); } else if (log.isDebugEnabled()) { log.debug("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!")); } onPollCompletion(entry); } catch (FileSystemException e) { processFailure("Error checking for existence and readability : " + VFSUtils.maskURLPassword(fileURI), e, entry); } catch (Exception ex) { processFailure("Un-handled exception thrown when processing the file : ", ex, entry); } finally { closeFileSystem(fileObject); } }
From source file:org.apache.synapse.transport.vfs.VFSTransportListener.java
/** * Take specified action to either move or delete the processed file, depending on the outcome * @param entry the PollTableEntry for the file that has been processed * @param fileObject the FileObject representing the file to be moved or deleted *///from w w w. ja va2 s .c om private void moveOrDeleteAfterProcessing(final PollTableEntry entry, FileObject fileObject, FileSystemOptions fso) throws AxisFault { String moveToDirectoryURI = null; try { switch (entry.getLastPollState()) { case PollTableEntry.SUCCSESSFUL: if (entry.getActionAfterProcess() == PollTableEntry.MOVE) { moveToDirectoryURI = entry.getMoveAfterProcess(); //Postfix the date given timestamp format String strSubfoldertimestamp = entry.getSubfolderTimestamp(); if (strSubfoldertimestamp != null) { try { SimpleDateFormat sdf = new SimpleDateFormat(strSubfoldertimestamp); String strDateformat = sdf.format(new Date()); int iIndex = moveToDirectoryURI.indexOf("?"); if (iIndex > -1) { moveToDirectoryURI = moveToDirectoryURI.substring(0, iIndex) + strDateformat + moveToDirectoryURI.substring(iIndex, moveToDirectoryURI.length()); } else { moveToDirectoryURI += strDateformat; } } catch (Exception e) { log.warn("Error generating subfolder name with date", e); } } } break; case PollTableEntry.FAILED: if (entry.getActionAfterFailure() == PollTableEntry.MOVE) { moveToDirectoryURI = entry.getMoveAfterFailure(); } break; default: return; } if (moveToDirectoryURI != null) { FileObject moveToDirectory = fsManager.resolveFile(moveToDirectoryURI, fso); String prefix; if (entry.getMoveTimestampFormat() != null) { prefix = entry.getMoveTimestampFormat().format(new Date()); } else { prefix = ""; } //Forcefully create the folder(s) if does not exists if (entry.isForceCreateFolder() && !moveToDirectory.exists()) { moveToDirectory.createFolder(); } FileObject dest = moveToDirectory.resolveFile(prefix + fileObject.getName().getBaseName()); if (log.isDebugEnabled()) { log.debug("Moving to file :" + VFSUtils.maskURLPassword(dest.getName().getURI())); } try { fileObject.moveTo(dest); } catch (FileSystemException e) { handleException("Error moving file : " + VFSUtils.maskURLPassword(fileObject.toString()) + " to " + VFSUtils.maskURLPassword(moveToDirectoryURI), e); } finally { try { fileObject.close(); } catch (FileSystemException ignore) { } } } else { try { if (log.isDebugEnabled()) { log.debug("Deleting file :" + VFSUtils.maskURLPassword(fileObject.toString())); } fileObject.close(); if (!fileObject.delete()) { String msg = "Cannot delete file : " + VFSUtils.maskURLPassword(fileObject.toString()); log.error(msg); throw new AxisFault(msg); } } catch (FileSystemException e) { log.error("Error deleting file : " + VFSUtils.maskURLPassword(fileObject.toString()), e); } } } catch (FileSystemException e) { handleException("Error resolving directory to move after processing : " + VFSUtils.maskURLPassword(moveToDirectoryURI), e); } }
From source file:org.apache.synapse.transport.vfs.VFSTransportSender.java
private void writeFile(MessageContext msgCtx, VFSOutTransportInfo vfsOutInfo) throws AxisFault { FileSystemOptions fso = null;//from ww w . jav a 2 s .c o m try { fso = VFSUtils.attachFileSystemOptions(vfsOutInfo.getOutFileSystemOptionsMap(), fsManager); } catch (Exception e) { log.error("Error while attaching VFS file system properties. " + e.getMessage()); } if (vfsOutInfo != null) { FileObject replyFile = null; try { boolean wasError = true; int retryCount = 0; int maxRetryCount = vfsOutInfo.getMaxRetryCount(); long reconnectionTimeout = vfsOutInfo.getReconnectTimeout(); boolean append = vfsOutInfo.isAppend(); while (wasError) { try { retryCount++; replyFile = fsManager.resolveFile(vfsOutInfo.getOutFileURI(), fso); if (replyFile == null) { log.error("replyFile is null"); throw new FileSystemException("replyFile is null"); } wasError = false; } catch (FileSystemException e) { log.error("cannot resolve replyFile", e); if (maxRetryCount <= retryCount) { handleException("cannot resolve replyFile repeatedly: " + e.getMessage(), e); } } if (wasError) { try { Thread.sleep(reconnectionTimeout); } catch (InterruptedException e2) { e2.printStackTrace(); } } } //If the reply folder does not exists create the folder structure if (vfsOutInfo.isForceCreateFolder()) { String strPath = vfsOutInfo.getOutFileURI(); int iIndex = strPath.indexOf("?"); if (iIndex > -1) { strPath = strPath.substring(0, iIndex); } //Need to add a slash otherwise vfs consider this as a file if (!strPath.endsWith("/") || !strPath.endsWith("\\")) { strPath += "/"; } FileObject replyFolder = fsManager.resolveFile(strPath, fso); if (!replyFolder.exists()) { replyFile.createFolder(); } } if (replyFile.exists()) { if (replyFile.getType() == FileType.FOLDER) { // we need to write a file containing the message to this folder FileObject responseFile = fsManager.resolveFile(replyFile, VFSUtils.getFileName(msgCtx, vfsOutInfo)); // if file locking is not disabled acquire the lock // before uploading the file if (vfsOutInfo.isFileLockingEnabled()) { acquireLockForSending(responseFile, vfsOutInfo, fso); if (!responseFile.exists()) { responseFile.createFile(); } populateResponseFile(responseFile, msgCtx, append, true, fso); VFSUtils.releaseLock(fsManager, responseFile, fso); } else { if (!responseFile.exists()) { responseFile.createFile(); } populateResponseFile(responseFile, msgCtx, append, false, fso); } } else if (replyFile.getType() == FileType.FILE) { // if file locking is not disabled acquire the lock // before uploading the file if (vfsOutInfo.isFileLockingEnabled()) { acquireLockForSending(replyFile, vfsOutInfo, fso); populateResponseFile(replyFile, msgCtx, append, true, fso); VFSUtils.releaseLock(fsManager, replyFile, fso); } else { populateResponseFile(replyFile, msgCtx, append, false, fso); } } else { handleException("Unsupported reply file type : " + replyFile.getType() + " for file : " + VFSUtils.maskURLPassword(vfsOutInfo.getOutFileURI())); } } else { // if file locking is not disabled acquire the lock before uploading the file if (vfsOutInfo.isFileLockingEnabled()) { acquireLockForSending(replyFile, vfsOutInfo, fso); replyFile.createFile(); populateResponseFile(replyFile, msgCtx, append, true, fso); VFSUtils.releaseLock(fsManager, replyFile, fso); } else { replyFile.createFile(); populateResponseFile(replyFile, msgCtx, append, false, fso); } } } catch (FileSystemException e) { handleException( "Error resolving reply file : " + VFSUtils.maskURLPassword(vfsOutInfo.getOutFileURI()), e); } finally { if (replyFile != null) { try {/* if (fsManager != null && replyFile.getParent() != null && replyFile.getParent().getFileSystem() != null) { fsManager.closeFileSystem(replyFile.getParent().getFileSystem()); }*/ replyFile.close(); } catch (FileSystemException ignore) { } } } } else { handleException("Unable to determine out transport information to send message"); } }
From source file:org.apache.zeppelin.notebook.repo.OldVFSNotebookRepo.java
protected void setNotebookDirectory(String notebookDirPath) throws IOException { try {// w ww .j av a 2s .c o m LOG.info("Using notebookDir: " + notebookDirPath); if (conf.isWindowsPath(notebookDirPath)) { filesystemRoot = new File(notebookDirPath).toURI(); } else { filesystemRoot = new URI(notebookDirPath); } } catch (URISyntaxException e1) { throw new IOException(e1); } if (filesystemRoot.getScheme() == null) { // it is local path File f = new File(conf.getRelativeDir(filesystemRoot.getPath())); this.filesystemRoot = f.toURI(); } fsManager = VFS.getManager(); FileObject file = fsManager.resolveFile(filesystemRoot.getPath()); if (!file.exists()) { LOG.info("Notebook dir doesn't exist, create on is {}.", file.getName()); file.createFolder(); } }
From source file:org.apache.zeppelin.notebook.repo.OldVFSNotebookRepo.java
private Note getNote(FileObject noteDir) throws IOException { if (!isDirectory(noteDir)) { throw new IOException(noteDir.getName().toString() + " is not a directory"); }//from w ww .ja v a 2 s.c o m FileObject noteJson = noteDir.resolveFile("note.json", NameScope.CHILD); if (!noteJson.exists()) { throw new IOException(noteJson.getName().toString() + " not found"); } FileContent content = noteJson.getContent(); InputStream ins = content.getInputStream(); String json = IOUtils.toString(ins, conf.getString(ConfVars.ZEPPELIN_ENCODING)); ins.close(); return Note.fromJson(json); }
From source file:org.apache.zeppelin.notebook.repo.OldVFSNotebookRepo.java
protected FileObject getRootDir() throws IOException { FileObject rootDir = fsManager.resolveFile(getPath("/")); if (!rootDir.exists()) { throw new IOException("Root path does not exists"); }/*from w ww .java 2 s. c o m*/ if (!isDirectory(rootDir)) { throw new IOException("Root path is not a directory"); } return rootDir; }
From source file:org.apache.zeppelin.notebook.repo.OldVFSNotebookRepo.java
@Override public synchronized void save(Note note, AuthenticationInfo subject) throws IOException { LOG.info("Saving note:" + note.getId()); String json = note.toJson();/*from w w w.ja va 2 s. c om*/ FileObject rootDir = getRootDir(); FileObject noteDir = rootDir.resolveFile(note.getId(), NameScope.CHILD); if (!noteDir.exists()) { noteDir.createFolder(); } if (!isDirectory(noteDir)) { throw new IOException(noteDir.getName().toString() + " is not a directory"); } FileObject noteJson = noteDir.resolveFile(".note.json", NameScope.CHILD); // false means not appending. creates file if not exists OutputStream out = noteJson.getContent().getOutputStream(false); out.write(json.getBytes(conf.getString(ConfVars.ZEPPELIN_ENCODING))); out.close(); noteJson.moveTo(noteDir.resolveFile("note.json", NameScope.CHILD)); }