use of java.nio.channels.OverlappingFileLockException in project helios by spotify.
the class TemporaryPorts method lock.
private AllocatedPort lock(final int port, final String name) {
final Path path = lockDirectory.resolve(String.valueOf(port));
try {
final FileChannel file = FileChannel.open(path, CREATE, WRITE);
final FileLock lock = file.tryLock();
if (lock == null) {
return null;
}
file.write(ByteBuffer.wrap(format("%d %s%n", port, name).getBytes(UTF_8)));
file.force(true);
return new AllocatedPort(port, path, file, lock);
} catch (OverlappingFileLockException e) {
return null;
} catch (IOException e) {
log.error("Failed to take port lock: {}", path, e);
throw Throwables.propagate(e);
}
}
use of java.nio.channels.OverlappingFileLockException in project jdk8u_jdk by JetBrains.
the class FileHandler method openFiles.
/**
* Open the set of output files, based on the configured
* instance variables.
*/
private void openFiles() throws IOException {
LogManager manager = LogManager.getLogManager();
manager.checkPermission();
if (count < 1) {
throw new IllegalArgumentException("file count = " + count);
}
if (limit < 0) {
limit = 0;
}
// We register our own ErrorManager during initialization
// so we can record exceptions.
InitializationErrorManager em = new InitializationErrorManager();
setErrorManager(em);
// Create a lock file. This grants us exclusive access
// to our set of output files, as long as we are alive.
int unique = -1;
for (; ; ) {
unique++;
if (unique > MAX_LOCKS) {
throw new IOException("Couldn't get lock for " + pattern);
}
// Generate a lock file name from the "unique" int.
lockFileName = generate(pattern, 0, unique).toString() + ".lck";
// if we ourself already have the file locked.
synchronized (locks) {
if (locks.contains(lockFileName)) {
// object. Try again.
continue;
}
final Path lockFilePath = Paths.get(lockFileName);
FileChannel channel = null;
int retries = -1;
boolean fileCreated = false;
while (channel == null && retries++ < 1) {
try {
channel = FileChannel.open(lockFilePath, CREATE_NEW, WRITE);
fileCreated = true;
} catch (FileAlreadyExistsException ix) {
// but not too frequently.
if (Files.isRegularFile(lockFilePath, LinkOption.NOFOLLOW_LINKS) && isParentWritable(lockFilePath)) {
try {
channel = FileChannel.open(lockFilePath, WRITE, APPEND);
} catch (NoSuchFileException x) {
// the sequence.
continue;
} catch (IOException x) {
// try the next name in the sequence
break;
}
} else {
// break and try the next name in the sequence.
break;
}
}
}
// try the next name;
if (channel == null)
continue;
lockFileChannel = channel;
boolean available;
try {
available = lockFileChannel.tryLock() != null;
// We got the lock OK.
// At this point we could call File.deleteOnExit().
// However, this could have undesirable side effects
// as indicated by JDK-4872014. So we will instead
// rely on the fact that close() will remove the lock
// file and that whoever is creating FileHandlers should
// be responsible for closing them.
} catch (IOException ix) {
// We got an IOException while trying to get the lock.
// This normally indicates that locking is not supported
// on the target directory. We have to proceed without
// getting a lock. Drop through, but only if we did
// create the file...
available = fileCreated;
} catch (OverlappingFileLockException x) {
// someone already locked this file in this VM, through
// some other channel - that is - using something else
// than new FileHandler(...);
// continue searching for an available lock.
available = false;
}
if (available) {
// We got the lock. Remember it.
locks.add(lockFileName);
break;
}
// We failed to get the lock. Try next file.
lockFileChannel.close();
}
}
files = new File[count];
for (int i = 0; i < count; i++) {
files[i] = generate(pattern, i, unique);
}
// Create the initial log file.
if (append) {
open(files[0], true);
} else {
rotate();
}
// Did we detect any exceptions during initialization?
Exception ex = em.lastException;
if (ex != null) {
if (ex instanceof IOException) {
throw (IOException) ex;
} else if (ex instanceof SecurityException) {
throw (SecurityException) ex;
} else {
throw new IOException("Exception: " + ex);
}
}
// Install the normal default ErrorManager.
setErrorManager(new ErrorManager());
}
use of java.nio.channels.OverlappingFileLockException in project carbondata by apache.
the class LocalFileLock method lock.
/**
* Lock API for locking of the file channel of the lock file.
*
* @return
*/
@Override
public boolean lock() {
try {
if (!FileFactory.isFileExist(location, FileFactory.getFileType(tmpPath))) {
FileFactory.mkdirs(location, FileFactory.getFileType(tmpPath));
}
lockFilePath = location + CarbonCommonConstants.FILE_SEPARATOR + lockFile;
if (!FileFactory.isFileExist(lockFilePath, FileFactory.getFileType(location))) {
FileFactory.createNewLockFile(lockFilePath, FileFactory.getFileType(location));
}
fileOutputStream = new FileOutputStream(lockFilePath);
channel = fileOutputStream.getChannel();
try {
fileLock = channel.tryLock();
} catch (OverlappingFileLockException e) {
return false;
}
if (null != fileLock) {
return true;
} else {
return false;
}
} catch (IOException e) {
LOGGER.error(e, e.getMessage());
return false;
}
}
Aggregations