String fullPath = fo.getURL().toString();
int pos = fullPath.indexOf("?");
if (pos != -1) {
fullPath = fullPath.substring(0, pos);
}
FileObject lockObject = fsManager.resolveFile(fullPath + ".lock");
if (lockObject.exists()) {
log.debug("There seems to be an external lock, aborting the processing of the file "
+ fo.getName() + ". This could possibly be due to some other party already "
+ "processing this file or the file is still being uploaded");
} else {
// write a lock file before starting of the processing, to ensure that the
// item is not processed by any other parties
lockObject.createFile();
OutputStream stream = lockObject.getContent().getOutputStream();
try {
stream.write(lockValue);
stream.flush();
stream.close();
} catch (IOException e) {
lockObject.delete();
log.error("Couldn't create the lock file before processing the file "
+ fullPath, e);
return false;
} finally {
lockObject.close();
}
// 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");
if (verifyingLockObject.exists() && verifyLock(lockValue, verifyingLockObject)) {
return true;
}
}
} catch (FileSystemException fse) {
log.error("Cannot get the lock for the file : " + maskURLPassword(fo.getName().getURI())