use of org.apache.commons.vfs2.FileSystemManager in project wso2-synapse by wso2.
the class VFSUtils method acquireLock.
/**
* Acquires a file item lock before processing the item, guaranteing that
* the file is not processed while it is being uploaded and/or the item is
* not processed by two listeners
*
* @param fsManager
* used to resolve the processing file
* @param fo
* representing the processing file item
* @param fso
* represents file system options used when resolving file from file system manager.
* @return boolean true if the lock has been acquired or false if not
*/
public static synchronized boolean acquireLock(FileSystemManager fsManager, FileObject fo, VFSParamDTO paramDTO, FileSystemOptions fso, boolean isListener) {
String strLockValue = getLockValue();
byte[] lockValue = strLockValue.getBytes();
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 = getFullPath(fo);
lockObject = fsManager.resolveFile(fullPath + LOCK_FILE_SUFFIX, fso);
if (lockObject.exists()) {
log.debug("There seems to be an external lock, aborting the processing of the file " + maskURLPassword(fo.getName().getURI()) + ". This could possibly be due to some other party already " + "processing this file or the file is still being uploaded");
if (paramDTO != null && paramDTO.isAutoLockRelease()) {
releaseLock(lockValue, strLockValue, lockObject, paramDTO.isAutoLockReleaseSameNode(), paramDTO.getAutoLockReleaseInterval());
}
} else {
if (isListener) {
// Check the original file existence before the lock file to handle concurrent access scenario
FileObject originalFileObject = fsManager.resolveFile(fullPath, fso);
if (!originalFileObject.exists()) {
return false;
}
}
if (!createLockFile(lockValue, lockObject, fullPath)) {
return false;
}
// check whether the lock is in place and is it me who holds the lock. This is
// required because it is possible to write the lock file simultaneously by
// two processing parties. It checks whether the lock file content is the same
// as the written random lock value.
// NOTE: this may not be optimal but is sub optimal
FileObject verifyingLockObject = fsManager.resolveFile(fullPath + LOCK_FILE_SUFFIX, fso);
if (verifyingLockObject.exists() && verifyLock(lockValue, verifyingLockObject)) {
return true;
}
}
} catch (FileSystemException fse) {
log.error("Cannot get the lock for the file : " + maskURLPassword(fo.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;
}
use of org.apache.commons.vfs2.FileSystemManager in project pentaho-platform by pentaho.
the class ApacheVFSOutputHandler method getFileOutputContentItem.
@Override
public IContentItem getFileOutputContentItem() {
String contentRef = getContentRef();
try {
// $NON-NLS-1$
String contentName = getHandlerId().substring(4) + ":" + contentRef;
FileSystemManager fsManager = getFileSystemManager();
if (fsManager == null) {
logError(Messages.getInstance().getString("ApacheVFSOutputHandler.ERROR_0001_CANNOT_GET_VFSMGR"));
return null;
}
FileObject file = fsManager.resolveFile(contentName);
if (file == null) {
logError(Messages.getInstance().getString("ApacheVFSOutputHandler.ERROR_0002_CANNOT_GET_VF", contentName));
return null;
}
if (!file.isWriteable()) {
logError(Messages.getInstance().getString("ApacheVFSOutputHandler.ERROR_0003_CANNOT_WRITE", contentName));
return null;
}
FileContent fileContent = file.getContent();
if (fileContent == null) {
logError(Messages.getInstance().getString("ApacheVFSOutputHandler.ERROR_0004_CANNOT_GET_CTX", contentName));
return null;
}
OutputStream outputStream = fileContent.getOutputStream();
SimpleContentItem content = new SimpleContentItem(outputStream);
return content;
} catch (Throwable t) {
Logger.error(ApacheVFSOutputHandler.class.getName(), Messages.getInstance().getString("ApacheVFSOutputHandler.ERROR_0005_CANNOT_GET_HANDLER", contentRef), // $NON-NLS-1$
t);
}
return null;
}
use of org.apache.commons.vfs2.FileSystemManager in project pentaho-platform by pentaho.
the class MondrianCatalogHelper method docAtUrlToString.
protected String docAtUrlToString(final String urlStr, final IPentahoSession pentahoSession) {
// String relPath = getSolutionRepositoryRelativePath(urlStr, pentahoSession);
String res = null;
InputStream in = null;
try {
LocalizingDynamicSchemaProcessor schemaProcessor = new LocalizingDynamicSchemaProcessor();
PropertyList localeInfo = new PropertyList();
// $NON-NLS-1$
localeInfo.put("Locale", getLocale().toString());
FileSystemManager fsManager = VFS.getManager();
FileObject mondrianDS = fsManager.resolveFile(urlStr);
in = mondrianDS.getContent().getInputStream();
res = schemaProcessor.filter(null, localeInfo, in);
} catch (FileNotFoundException fnfe) {
throw new MondrianCatalogServiceException(Messages.getInstance().getErrorString("MondrianCatalogHelper.ERROR_0007_FILE_NOT_FOUND"), // $NON-NLS-1$
fnfe);
} catch (Exception e) {
throw new MondrianCatalogServiceException(Messages.getInstance().getErrorString("MondrianCatalogHelper.ERROR_0006_IO_PROBLEM"), // $NON-NLS-1$
e);
} finally {
IOUtils.closeQuietly(in);
}
return res;
}
Aggregations