List of usage examples for java.nio.channels FileLock release
public abstract void release() throws IOException;
From source file:in.bbat.license.LicenseManager.java
public static void cleanupOlderLicenseFiles() { File localFile1 = new File(""); File[] arrayOfFile1 = localFile1.listFiles(new FilenameFilter() { public boolean accept(File paramAnonymousFile, String paramAnonymousString) { return paramAnonymousString.endsWith(".license.lock"); }//w ww .ja v a 2s . c o m }); for (File localFile2 : arrayOfFile1) { FileLock localFileLock = null; RandomAccessFile localRandomAccessFile = null; try { localRandomAccessFile = new RandomAccessFile(localFile2, "rw"); localFileLock = localRandomAccessFile.getChannel().tryLock(); } catch (Exception localException) { localFileLock = null; } if (localFileLock != null) { try { localFileLock.release(); localRandomAccessFile.close(); } catch (IOException localIOException1) { LOG.error("Error releasing an acquired lock. No problem, continuing!"); } String str = null; try { str = FileUtils.readFileToString(localFile2); returnOlderLicense(str); localFile2.delete(); } catch (IOException localIOException2) { LOG.error("Error returning an older license that is no longer valid. No problem, continuing!"); } } } }
From source file:ubicrypt.core.Utils.java
public static boolean isAppInUse(final Path ubiqFolder) throws IOException { Files.createDirectories(ubiqFolder); final File file = Paths.get(ubiqFolder.toString(), "lock").toFile(); final FileChannel channel = new RandomAccessFile(file, "rw").getChannel(); final FileLock lock; try {/*from w w w . jav a2s . c om*/ lock = channel.tryLock(); } catch (final OverlappingFileLockException e) { return true; } if (lock == null) { return true; } Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { try { lock.release(); channel.close(); } catch (final Exception e) { e.printStackTrace(); } } }); return false; }
From source file:org.ow2.proactive.resourcemanager.updater.RMNodeUpdater.java
private static boolean makeNodeUpToDate() { if (System.getProperty(NODE_URL_PROPERTY) != null) { String jarUrl = System.getProperty(NODE_URL_PROPERTY); String jarFile = SCHEDULER_NODE_JAR; if (System.getProperty(NODE_JAR_PROPERTY) != null) { jarFile = System.getProperty(NODE_JAR_PROPERTY); }/*from ww w .jav a 2s .c o m*/ if (!isLocalJarUpToDate(jarUrl, jarFile)) { System.out.println("Downloading node.jar from " + jarUrl + " to " + jarFile); try { File destination = new File(jarFile); File lockFile = null; FileLock lock = null; if (destination.exists()) { lockFile = new File(System.getProperty(TMP_DIR_PROPERTY) + "/lock"); if (!lockFile.exists()) { lockFile.createNewFile(); } System.out.println("Getting the lock on " + lockFile.getAbsoluteFile()); FileChannel channel = new RandomAccessFile(lockFile, "rw").getChannel(); lock = channel.lock(); if (isLocalJarUpToDate(jarUrl, jarFile)) { System.out.println("Another process downloaded node.jar - don't do it anymore"); System.out.println("Releasing the lock on " + lockFile.getAbsoluteFile()); lock.release(); channel.close(); return false; } } FileUtils.copyURLToFile(new URL(jarUrl), destination); System.out.println("Download finished"); if (lock != null && lockFile != null) { System.out.println("Releasing the lock on " + lockFile.getAbsoluteFile()); lock.release(); } return true; } catch (Exception e) { System.err.println("Cannot download node.jar from " + jarUrl); e.printStackTrace(); } } } else { System.out.println( "No java property " + NODE_URL_PROPERTY + " specified. Do not check for the new version."); } return false; }
From source file:it.geosolutions.tools.io.file.IOUtils.java
/** * Optimize version of copy method for file channels. * /*w ww . j a v a 2 s .co m*/ * @param bufferSize * size of the temp buffer to use for this copy. * @param source * the source {@link ReadableByteChannel}. * @param destination * the destination {@link WritableByteChannel};. * @throws IOException * in case something bad happens. */ public static void copyFileChannel(int bufferSize, FileChannel source, FileChannel destination) throws IOException { Objects.notNull(source, destination); if (!source.isOpen() || !destination.isOpen()) throw new IllegalStateException("Source and destination channels must be open."); FileLock lock = null; try { lock = destination.lock(); final long sourceSize = source.size(); long pos = 0; while (pos < sourceSize) { // read and flip final long remaining = (sourceSize - pos); final int mappedZoneSize = remaining >= bufferSize ? bufferSize : (int) remaining; destination.transferFrom(source, pos, mappedZoneSize); // update zone pos += mappedZoneSize; } } finally { if (lock != null) { try { lock.release(); } catch (Throwable t) { if (LOGGER.isInfoEnabled()) LOGGER.info(t.getLocalizedMessage(), t); } } } }
From source file:gemlite.core.internal.support.system.WorkPathHelper.java
public void unlock(FileLock lock) { if (lock != null) { try {//from w w w. j a v a 2 s .co m lock.release(); } catch (IOException x) { throw new RuntimeException(x); } lock = null; } }
From source file:org.apache.geode.test.concurrent.FileBasedCountDownLatch.java
public FileBasedCountDownLatch(int count) throws IOException { lockFile = File.createTempFile("CountDownLatchLock", ".txt"); dataFile = File.createTempFile("CountDownLatchData", ".txt"); try (FileOutputStream out = new FileOutputStream(lockFile)) { java.nio.channels.FileLock lock = out.getChannel().lock(); try {/*from ww w . j a v a2s . c o m*/ FileUtils.writeStringToFile(dataFile, String.valueOf(count), Charsets.UTF_8); } finally { lock.release(); } } lockFile.deleteOnExit(); }
From source file:fr.acxio.tools.agia.alfresco.ContentFileDeleteWriterTest.java
@Test public void testWriteCannotDelete() throws Exception { ContentFileDeleteWriter aWriter = new ContentFileDeleteWriter(); File aOriginFile = new File("src/test/resources/testFiles/content1.pdf"); File aDestinationFile1 = new File("target/content4.pdf"); FileCopyUtils.copy(aOriginFile, aDestinationFile1); List<NodeList> aData = new ArrayList<NodeList>(); aData.add(createNodeList(aDestinationFile1.getAbsolutePath())); assertTrue(aDestinationFile1.exists()); FileInputStream aInputStream = new FileInputStream(aDestinationFile1); FileLock aLock = aInputStream.getChannel().lock(0L, Long.MAX_VALUE, true); // shared lock aWriter.write(aData);// w w w. ja v a 2 s . co m aLock.release(); aInputStream.close(); assertTrue(aDestinationFile1.exists()); }
From source file:org.apache.geode.test.concurrent.FileBasedCountDownLatch.java
protected int currentValue() throws IOException { try (FileOutputStream out = new FileOutputStream(lockFile)) { java.nio.channels.FileLock lock = out.getChannel().lock(); try {//from w ww .ja va2s .c o m String fileContents = FileUtils.readFileToString(dataFile, Charsets.UTF_8); return Integer.valueOf(fileContents); } finally { lock.release(); } } }
From source file:org.geoserver.rest.util.IOUtils.java
/** * Optimize version of copy method for file channels. * /*from w w w. ja v a 2s .com*/ * @param bufferSize size of the temp buffer to use for this copy. * @param source the source {@link ReadableByteChannel}. * @param destination the destination {@link WritableByteChannel};. * @throws IOException in case something bad happens. */ public static void copyFileChannel(int bufferSize, FileChannel source, FileChannel destination) throws IOException { inputNotNull(source, destination); if (!source.isOpen() || !destination.isOpen()) throw new IllegalStateException("Source and destination channels must be open."); FileLock lock = null; try { lock = destination.lock(); final long sourceSize = source.size(); long pos = 0; while (pos < sourceSize) { // read and flip final long remaining = (sourceSize - pos); final int mappedZoneSize = remaining >= bufferSize ? bufferSize : (int) remaining; destination.transferFrom(source, pos, mappedZoneSize); // update zone pos += mappedZoneSize; } } finally { if (lock != null) { try { lock.release(); } catch (Throwable t) { if (LOGGER.isLoggable(Level.INFO)) LOGGER.log(Level.INFO, t.getLocalizedMessage(), t); } } } }
From source file:com.adaptris.fs.NioWorkerTest.java
@Test public void testLockWhileReading() throws Exception { FsWorker worker = createWorker();// w w w. jav a 2 s.co m File f = File.createTempFile(this.getClass().getSimpleName(), ""); f.delete(); try { worker.put(BYTES, f); RandomAccessFile raf = new RandomAccessFile(f, "rwd"); FileLock lock = raf.getChannel().lock(); try { worker.get(f); fail(); } catch (FsException expected) { assertEquals(OverlappingFileLockException.class, expected.getCause().getClass()); } lock.release(); raf.close(); worker.get(f); } finally { FileUtils.deleteQuietly(f); } }