Java Code Examples for java.nio.channels.FileChannel#tryLock()
The following examples show how to use
java.nio.channels.FileChannel#tryLock() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RrdSafeFileBackend.java From jrobin with GNU Lesser General Public License v2.1 | 8 votes |
private void lockFile(final long lockWaitTime, final long lockRetryPeriod) throws IOException { final long entryTime = System.currentTimeMillis(); final FileChannel channel = file.getChannel(); m_lock = channel.tryLock(0, Long.MAX_VALUE, false); if (m_lock != null) { counters.registerQuickLock(); return; } do { try { Thread.sleep(lockRetryPeriod); } catch (final InterruptedException e) { Thread.currentThread().interrupt(); } m_lock = channel.tryLock(0, Long.MAX_VALUE, false); if (m_lock != null) { counters.registerDelayedLock(); return; } } while (System.currentTimeMillis() - entryTime <= lockWaitTime); counters.registerError(); throw new IOException("Could not obtain exclusive m_lock on file: " + getPath() + "] after " + lockWaitTime + " milliseconds"); }
Example 2
Source File: MainWithNailgun.java From buck with Apache License 2.0 | 6 votes |
/** * To prevent 'buck kill' from deleting resources from underneath a 'live' buckd we hold on to the * FileLock for the entire lifetime of the process. We depend on the fact that on Linux and MacOS * Java FileLock is implemented using the same mechanism as the Python fcntl.lockf method. Should * this not be the case we'll simply have a small race between buckd start and `buck kill`. */ private static void obtainResourceFileLock() { if (resourcesFileLock != null) { return; } String resourceLockFilePath = System.getProperties().getProperty("buck.resource_lock_path"); if (resourceLockFilePath == null) { // Running from ant, no resource lock needed. return; } try { // R+W+A is equivalent to 'a+' in Python (which is how the lock file is opened in Python) // because WRITE in Java does not imply truncating the file. FileChannel fileChannel = FileChannel.open( Paths.get(resourceLockFilePath), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE); resourcesFileLock = fileChannel.tryLock(0L, Long.MAX_VALUE, true); } catch (IOException | OverlappingFileLockException e) { LOG.warn(e, "Error when attempting to acquire resources file lock."); } }
Example 3
Source File: DBUtil.java From FairEmail with GNU General Public License v3.0 | 6 votes |
/** * Reads the user version number out of the database header from the given file. * * @param databaseFile the database file. * @return the database version * @throws IOException if something goes wrong reading the file, such as bad database header or * missing permissions. * * @see <a href="https://www.sqlite.org/fileformat.html#user_version_number">User Version * Number</a>. */ public static int readVersion(@NonNull File databaseFile) throws IOException { FileChannel input = null; try { ByteBuffer buffer = ByteBuffer.allocate(4); input = new FileInputStream(databaseFile).getChannel(); input.tryLock(60, 4, true); input.position(60); int read = input.read(buffer); if (read != 4) { throw new IOException("Bad database header, unable to read 4 bytes at offset 60"); } buffer.rewind(); return buffer.getInt(); // ByteBuffer is big-endian by default } finally { if (input != null) { input.close(); } } }
Example 4
Source File: RootDirectorySystem.java From baratine with GNU General Public License v2.0 | 6 votes |
synchronized boolean lock() { if (_lock != null) { return false; } try { FileChannel osFile = (FileChannel) Files.newByteChannel(_path); _lock = osFile.tryLock(); if (_lock == null) { osFile.close(); return false; } _osFile = osFile; return true; } catch (Exception e) { log.log(Level.FINER, e.toString(), e); return false; } }
Example 5
Source File: Locks.java From OpenModsLib with MIT License | 6 votes |
public static Optional<ExclusiveLock> tryExclusiveLock(File file) throws IOException { Closer closer = Closer.create(); try { final RandomAccessFile s = closer.register(new RandomAccessFile(file, "rw")); final FileChannel ch = closer.register(s.getChannel()); final FileLock lock = ch.tryLock(); if (lock != null) return Optional.of(new ExclusiveLock(file, ch, lock)); } catch (FileNotFoundException e) { // just let it skip, user has to retry Log.log(Level.DEBUG, e, "Failed to create or lock file %s, possible permission issue", file); } catch (Throwable t) { throw closer.rethrow(t); } closer.close(); return Optional.absent(); }
Example 6
Source File: ExecUtil.java From ctsms with GNU Lesser General Public License v2.1 | 6 votes |
public static void lockProcess(String lockFileName) { if (!CommonUtil.isEmptyString(lockFileName)) { // https://stackoverflow.com/questions/7036108/prevent-launching-multiple-instances-of-a-java-application File file = new File(lockFileName); try { FileChannel fc = FileChannel.open(file.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE); FileLock lock = fc.tryLock(); if (lock == null) { throw new RuntimeException("another instance is running"); } } catch (IOException e) { throw new RuntimeException(e); } } }
Example 7
Source File: IOUtils.java From pumpernickel with MIT License | 5 votes |
/** * Return a FileLock for a given file, or null if the lock cannot be * obtained. * * @param file * the file to obtain the lock for. It is assumed the file * already exists when this is called. * @param writeData * if true then 8 bytes of zeroes will be written to the file. * @return a FileLock for the given File, or null if the lock couldn't be * obtained. * @throws IOException */ public static FileLock getFileLock(File file, boolean writeData) throws IOException { Path path = Paths.get(file.getAbsolutePath()); FileChannel channel = FileChannel.open(path, StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE); FileLock lock = channel.tryLock(); if (writeData) { // unfortunately: I forget exactly why this was added... channel.write(ByteBuffer.wrap(new byte[8])); channel.force(false); } return lock; }
Example 8
Source File: CheckZombieLockTest.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
/** * Setup all the files and directories needed for the tests * * @return writable directory created that needs to be deleted when done * @throws RuntimeException */ private static File setup() throws RuntimeException { // First do some setup in the temporary directory (using same logic as // FileHandler for %t pattern) String tmpDir = System.getProperty("java.io.tmpdir"); // i.e. %t if (tmpDir == null) { tmpDir = System.getProperty("user.home"); } File tmpOrHomeDir = new File(tmpDir); // Create a writable directory here (%t/writable-lockfile-dir) File writableDir = new File(tmpOrHomeDir, WRITABLE_DIR); if (!createFile(writableDir, true)) { throw new RuntimeException("Test setup failed: unable to create" + " writable working directory " + writableDir.getAbsolutePath() ); } // try to determine whether file locking is supported final String uniqueFileName = UUID.randomUUID().toString()+".lck"; try { FileChannel fc = FileChannel.open(Paths.get(writableDir.getAbsolutePath(), uniqueFileName), StandardOpenOption.CREATE_NEW, StandardOpenOption.APPEND, StandardOpenOption.DELETE_ON_CLOSE); try { fc.tryLock(); } catch(IOException x) { supportsLocking = false; } finally { fc.close(); } } catch (IOException t) { // should not happen System.err.println("Failed to create new file " + uniqueFileName + " in " + writableDir.getAbsolutePath()); throw new RuntimeException("Test setup failed: unable to run test", t); } return writableDir; }
Example 9
Source File: CheckZombieLockTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Setup all the files and directories needed for the tests * * @return writable directory created that needs to be deleted when done * @throws RuntimeException */ private static File setup() throws RuntimeException { // First do some setup in the temporary directory (using same logic as // FileHandler for %t pattern) String tmpDir = System.getProperty("java.io.tmpdir"); // i.e. %t if (tmpDir == null) { tmpDir = System.getProperty("user.home"); } File tmpOrHomeDir = new File(tmpDir); // Create a writable directory here (%t/writable-lockfile-dir) File writableDir = new File(tmpOrHomeDir, WRITABLE_DIR); if (!createFile(writableDir, true)) { throw new RuntimeException("Test setup failed: unable to create" + " writable working directory " + writableDir.getAbsolutePath() ); } // try to determine whether file locking is supported final String uniqueFileName = UUID.randomUUID().toString()+".lck"; try { FileChannel fc = FileChannel.open(Paths.get(writableDir.getAbsolutePath(), uniqueFileName), StandardOpenOption.CREATE_NEW, StandardOpenOption.APPEND, StandardOpenOption.DELETE_ON_CLOSE); try { fc.tryLock(); } catch(IOException x) { supportsLocking = false; } finally { fc.close(); } } catch (IOException t) { // should not happen System.err.println("Failed to create new file " + uniqueFileName + " in " + writableDir.getAbsolutePath()); throw new RuntimeException("Test setup failed: unable to run test", t); } return writableDir; }
Example 10
Source File: FileLock.java From embedded-cassandra with Apache License 2.0 | 5 votes |
@Nullable private static java.nio.channels.FileLock lock(FileChannel fileChannel) throws IOException { try { return fileChannel.tryLock(); } catch (OverlappingFileLockException ex) { return null; } }
Example 11
Source File: FileServiceTest.java From onboard with Apache License 2.0 | 5 votes |
@Ignore public void readFileWithIOException() throws UnsupportedEncodingException, IOException { // IOException String filename = "test_read.txt"; File file = new File(rootPath + File.separator + filename); RandomAccessFile raf = new RandomAccessFile(file, "rw"); FileChannel fc = raf.getChannel(); FileLock fl = fc.tryLock(); byte[] bytes = fileService.readFile(filename); Assert.assertNull(bytes); fl.release(); raf.close(); }
Example 12
Source File: CheckZombieLockTest.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 5 votes |
/** * Setup all the files and directories needed for the tests * * @return writable directory created that needs to be deleted when done * @throws RuntimeException */ private static File setup() throws RuntimeException { // First do some setup in the temporary directory (using same logic as // FileHandler for %t pattern) String tmpDir = System.getProperty("java.io.tmpdir"); // i.e. %t if (tmpDir == null) { tmpDir = System.getProperty("user.home"); } File tmpOrHomeDir = new File(tmpDir); // Create a writable directory here (%t/writable-lockfile-dir) File writableDir = new File(tmpOrHomeDir, WRITABLE_DIR); if (!createFile(writableDir, true)) { throw new RuntimeException("Test setup failed: unable to create" + " writable working directory " + writableDir.getAbsolutePath() ); } // try to determine whether file locking is supported final String uniqueFileName = UUID.randomUUID().toString()+".lck"; try { FileChannel fc = FileChannel.open(Paths.get(writableDir.getAbsolutePath(), uniqueFileName), StandardOpenOption.CREATE_NEW, StandardOpenOption.APPEND, StandardOpenOption.DELETE_ON_CLOSE); try { fc.tryLock(); } catch(IOException x) { supportsLocking = false; } finally { fc.close(); } } catch (IOException t) { // should not happen System.err.println("Failed to create new file " + uniqueFileName + " in " + writableDir.getAbsolutePath()); throw new RuntimeException("Test setup failed: unable to run test", t); } return writableDir; }
Example 13
Source File: Launcher.java From xyTalk-pc with GNU Affero General Public License v3.0 | 5 votes |
/** * 通过文件锁来判断程序是否正在运行 * * @return 如果正在运行返回true,否则返回false */ @SuppressWarnings("resource") private static boolean isApplicationRunning() { boolean rv = false; try { String path = appFilesBasePath + System.getProperty("file.separator") + "appLock"; File dir = new File(path); if (!dir.exists()) { dir.mkdirs(); } File lockFile = new File(path + System.getProperty("file.separator") + "appLaunch.lock"); if (!lockFile.exists()) { lockFile.createNewFile(); } //程序名称 RandomAccessFile fis = new RandomAccessFile(lockFile.getAbsolutePath(), "rw"); FileChannel fileChannel = fis.getChannel(); FileLock fileLock = fileChannel.tryLock(); if (fileLock == null) { System.out.println("程序已在运行."); rv = true; } } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return rv; }
Example 14
Source File: RedisLockFactory.java From RedisDirectory with Apache License 2.0 | 5 votes |
@Override public Lock obtainLock(@NonNull Directory dir, String lockName) throws IOException { if (!(dir instanceof RedisDirectory)) { throw new IllegalArgumentException("Expect argument of type [" + RedisDirectory.class.getName() + "]!"); } Path lockFile = lockFileDirectory.resolve(lockName); try { Files.createFile(lockFile); log.debug("Lock file path = {}", lockFile.toFile().getAbsolutePath()); } catch (IOException ignore) { //ignore log.debug("Lock file already exists!"); } final Path realPath = lockFile.toRealPath(); final FileTime creationTime = Files.readAttributes(realPath, BasicFileAttributes.class).creationTime(); if (LOCK_HELD.add(realPath.toString())) { FileChannel fileChannel = null; FileLock lock = null; try { fileChannel = FileChannel.open(realPath, StandardOpenOption.CREATE, StandardOpenOption.WRITE); lock = fileChannel.tryLock(); if (lock != null) { return new RedisLock(lock, fileChannel, realPath, creationTime); } else { throw new LockObtainFailedException("Lock held by another program: " + realPath); } } finally { if (lock == null) { IOUtils.closeQuietly(fileChannel); clearLockHeld(realPath); } } } else { throw new LockObtainFailedException("Lock held by this virtual machine: " + realPath); } }
Example 15
Source File: FileLockBasedLockChecker.java From Java-Thread-Affinity with Apache License 2.0 | 5 votes |
private boolean isLockFree(File file, int id) { //if no file exists - nobody has the lock for sure if (!file.exists()) { return true; } //do we have the lock already? LockReference existingLock = locks[id]; if (existingLock != null) { return false; } //does another process have the lock? try { FileChannel fc = FileChannel.open(file.toPath(), WRITE); FileLock fileLock = fc.tryLock(); if (fileLock == null) { return false; } } catch (IOException | OverlappingFileLockException e) { LOGGER.error(String.format("Exception occurred whilst trying to check lock on file %s : %s%n", file.getAbsolutePath(), e)); } //file is present but nobody has it locked - delete it LOGGER.info(String.format("Deleting %s as nobody has the lock", file.getAbsolutePath())); file.delete(); return true; }
Example 16
Source File: CheckZombieLockTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
private static void testFileHandlerReuse(File writableDir) throws IOException { List<File> before = listLocks(writableDir, true); System.out.println("before: " + before.size() + " locks found"); try { if (!before.isEmpty()) { throw new RuntimeException("Expected no lock file! Found: " + before); } } finally { before.stream().forEach(CheckZombieLockTest::delete); } FileHandler handler1 = createFileHandler(writableDir); System.out.println("handler created: " + handler1); List<File> after = listLocks(writableDir, true); System.out.println("after creating handler: " + after.size() + " locks found"); if (after.size() != 1) { throw new RuntimeException("Unexpected number of lock files found for " + handler1 + ": " + after); } final File lock = after.get(0); after.clear(); handler1.close(); after = listLocks(writableDir, true); System.out.println("after closing handler: " + after.size() + " locks found"); if (!after.isEmpty()) { throw new RuntimeException("Unexpected number of lock files found for " + handler1 + ": " + after); } if (!createFile(lock, false)) { throw new IOException("Can't create fake lock file: " + lock); } try { before = listLocks(writableDir, true); System.out.println("before: " + before.size() + " locks found"); if (before.size() != 1) { throw new RuntimeException("Unexpected number of lock files found: " + before + " expected [" + lock + "]."); } FileHandler handler2 = createFileHandler(writableDir); System.out.println("handler created: " + handler2); after = listLocks(writableDir, true); System.out.println("after creating handler: " + after.size() + " locks found"); after.removeAll(before); if (!after.isEmpty()) { throw new RuntimeException("Unexpected lock file found: " + after + "\n\t" + lock + " should have been reused"); } handler2.close(); System.out.println("handler closed: " + handler2); List<File> afterClose = listLocks(writableDir, true); System.out.println("after closing handler: " + afterClose.size() + " locks found"); if (!afterClose.isEmpty()) { throw new RuntimeException("Zombie lock file detected: " + afterClose); } if (supportsLocking) { FileChannel fc = FileChannel.open(Paths.get(lock.getAbsolutePath()), StandardOpenOption.CREATE_NEW, StandardOpenOption.APPEND, StandardOpenOption.WRITE); try { if (fc.tryLock() != null) { System.out.println("locked: " + lock); handler2 = createFileHandler(writableDir); System.out.println("handler created: " + handler2); after = listLocks(writableDir, true); System.out.println("after creating handler: " + after.size() + " locks found"); after.removeAll(before); if (after.size() != 1) { throw new RuntimeException("Unexpected lock files found: " + after + "\n\t" + lock + " should not have been reused"); } } else { throw new RuntimeException("Failed to lock: " + lock); } } finally { delete(lock); } } } finally { List<File> finalLocks = listLocks(writableDir, false); System.out.println("end: " + finalLocks.size() + " locks found"); delete(writableDir); } }
Example 17
Source File: CheckZombieLockTest.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 4 votes |
private static void testFileHandlerReuse(File writableDir) throws IOException { List<File> before = listLocks(writableDir, true); System.out.println("before: " + before.size() + " locks found"); try { if (!before.isEmpty()) { throw new RuntimeException("Expected no lock file! Found: " + before); } } finally { before.stream().forEach(CheckZombieLockTest::delete); } FileHandler handler1 = createFileHandler(writableDir); System.out.println("handler created: " + handler1); List<File> after = listLocks(writableDir, true); System.out.println("after creating handler: " + after.size() + " locks found"); if (after.size() != 1) { throw new RuntimeException("Unexpected number of lock files found for " + handler1 + ": " + after); } final File lock = after.get(0); after.clear(); handler1.close(); after = listLocks(writableDir, true); System.out.println("after closing handler: " + after.size() + " locks found"); if (!after.isEmpty()) { throw new RuntimeException("Unexpected number of lock files found for " + handler1 + ": " + after); } if (!createFile(lock, false)) { throw new IOException("Can't create fake lock file: " + lock); } try { before = listLocks(writableDir, true); System.out.println("before: " + before.size() + " locks found"); if (before.size() != 1) { throw new RuntimeException("Unexpected number of lock files found: " + before + " expected [" + lock + "]."); } FileHandler handler2 = createFileHandler(writableDir); System.out.println("handler created: " + handler2); after = listLocks(writableDir, true); System.out.println("after creating handler: " + after.size() + " locks found"); after.removeAll(before); if (!after.isEmpty()) { throw new RuntimeException("Unexpected lock file found: " + after + "\n\t" + lock + " should have been reused"); } handler2.close(); System.out.println("handler closed: " + handler2); List<File> afterClose = listLocks(writableDir, true); System.out.println("after closing handler: " + afterClose.size() + " locks found"); if (!afterClose.isEmpty()) { throw new RuntimeException("Zombie lock file detected: " + afterClose); } if (supportsLocking) { FileChannel fc = FileChannel.open(Paths.get(lock.getAbsolutePath()), StandardOpenOption.CREATE_NEW, StandardOpenOption.APPEND, StandardOpenOption.WRITE); try { if (fc.tryLock() != null) { System.out.println("locked: " + lock); handler2 = createFileHandler(writableDir); System.out.println("handler created: " + handler2); after = listLocks(writableDir, true); System.out.println("after creating handler: " + after.size() + " locks found"); after.removeAll(before); if (after.size() != 1) { throw new RuntimeException("Unexpected lock files found: " + after + "\n\t" + lock + " should not have been reused"); } } else { throw new RuntimeException("Failed to lock: " + lock); } } finally { delete(lock); } } } finally { List<File> finalLocks = listLocks(writableDir, false); System.out.println("end: " + finalLocks.size() + " locks found"); delete(writableDir); } }
Example 18
Source File: AbstractRegistry.java From dubbo-2.6.5 with Apache License 2.0 | 4 votes |
public void doSaveProperties(long version) { if (version < lastCacheChanged.get()) { return; } if (file == null) { return; } // Save try { File lockfile = new File(file.getAbsolutePath() + ".lock"); if (!lockfile.exists()) { lockfile.createNewFile(); } RandomAccessFile raf = new RandomAccessFile(lockfile, "rw"); try { FileChannel channel = raf.getChannel(); try { FileLock lock = channel.tryLock(); if (lock == null) { throw new IOException("Can not lock the registry cache file " + file.getAbsolutePath() + ", ignore and retry later, maybe multi java process use the file, please config: dubbo.registry.file=xxx.properties"); } // Save try { if (!file.exists()) { file.createNewFile(); } FileOutputStream outputFile = new FileOutputStream(file); try { properties.store(outputFile, "Dubbo Registry Cache"); } finally { outputFile.close(); } } finally { lock.release(); } } finally { channel.close(); } } finally { raf.close(); } } catch (Throwable e) { if (version < lastCacheChanged.get()) { return; } else { registryCacheExecutor.execute(new SaveProperties(lastCacheChanged.incrementAndGet())); } logger.warn("Failed to save registry store file, cause: " + e.getMessage(), e); } }
Example 19
Source File: CheckZombieLockTest.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
private static void testFileHandlerReuse(File writableDir) throws IOException { List<File> before = listLocks(writableDir, true); System.out.println("before: " + before.size() + " locks found"); try { if (!before.isEmpty()) { throw new RuntimeException("Expected no lock file! Found: " + before); } } finally { before.stream().forEach(CheckZombieLockTest::delete); } FileHandler handler1 = createFileHandler(writableDir); System.out.println("handler created: " + handler1); List<File> after = listLocks(writableDir, true); System.out.println("after creating handler: " + after.size() + " locks found"); if (after.size() != 1) { throw new RuntimeException("Unexpected number of lock files found for " + handler1 + ": " + after); } final File lock = after.get(0); after.clear(); handler1.close(); after = listLocks(writableDir, true); System.out.println("after closing handler: " + after.size() + " locks found"); if (!after.isEmpty()) { throw new RuntimeException("Unexpected number of lock files found for " + handler1 + ": " + after); } if (!createFile(lock, false)) { throw new IOException("Can't create fake lock file: " + lock); } try { before = listLocks(writableDir, true); System.out.println("before: " + before.size() + " locks found"); if (before.size() != 1) { throw new RuntimeException("Unexpected number of lock files found: " + before + " expected [" + lock + "]."); } FileHandler handler2 = createFileHandler(writableDir); System.out.println("handler created: " + handler2); after = listLocks(writableDir, true); System.out.println("after creating handler: " + after.size() + " locks found"); after.removeAll(before); if (!after.isEmpty()) { throw new RuntimeException("Unexpected lock file found: " + after + "\n\t" + lock + " should have been reused"); } handler2.close(); System.out.println("handler closed: " + handler2); List<File> afterClose = listLocks(writableDir, true); System.out.println("after closing handler: " + afterClose.size() + " locks found"); if (!afterClose.isEmpty()) { throw new RuntimeException("Zombie lock file detected: " + afterClose); } if (supportsLocking) { FileChannel fc = FileChannel.open(Paths.get(lock.getAbsolutePath()), StandardOpenOption.CREATE_NEW, StandardOpenOption.APPEND, StandardOpenOption.WRITE); try { if (fc.tryLock() != null) { System.out.println("locked: " + lock); handler2 = createFileHandler(writableDir); System.out.println("handler created: " + handler2); after = listLocks(writableDir, true); System.out.println("after creating handler: " + after.size() + " locks found"); after.removeAll(before); if (after.size() != 1) { throw new RuntimeException("Unexpected lock files found: " + after + "\n\t" + lock + " should not have been reused"); } } else { throw new RuntimeException("Failed to lock: " + lock); } } finally { delete(lock); } } } finally { List<File> finalLocks = listLocks(writableDir, false); System.out.println("end: " + finalLocks.size() + " locks found"); delete(writableDir); } }
Example 20
Source File: LogLockingTests.java From xodus with Apache License 2.0 | 4 votes |
private static boolean canWrite(File xdLockFile) throws IOException { boolean can = xdLockFile.canWrite(); if (can) { FileOutputStream stream = null; try { stream = new FileOutputStream(xdLockFile); stream.write(42); stream.flush(); stream.close(); } catch (IOException ex) { // xdLockFile.canWrite() returns true, because of Java cannot recognize tha file is locked can = false; } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { can = false; } } } } // If it didn't help for some reasons (saw that it doesn't work on Solaris 10) if (can) { RandomAccessFile file = null; FileChannel channel = null; try { file = new RandomAccessFile(xdLockFile, "rw"); channel = file.getChannel(); FileLock lock = channel.tryLock(); if (lock != null) { lock.release(); } else { can = false; } file.close(); } catch (Throwable ignore) { can = false; } finally { if (channel != null) channel.close(); if (file != null) file.close(); } } return can; }