Example usage for java.nio.channels FileLock release

List of usage examples for java.nio.channels FileLock release

Introduction

In this page you can find the example usage for java.nio.channels FileLock release.

Prototype

public abstract void release() throws IOException;

Source Link

Document

Releases this lock.

Usage

From source file:org.pentaho.di.trans.steps.excelinput.PoiWorkBookIT.java

License:asdf

@Test
public void testResourceFree() throws Exception {
    FileLock lock = null;
    RandomAccessFile randomAccessFile = null;
    try {//from w ww. j  a  v  a2 s.  c o m
        readData(sampleFile, null);
        File fileAfterRead = new File(sampleFile);
        randomAccessFile = new RandomAccessFile(fileAfterRead, "rw");
        FileChannel fileChannel = randomAccessFile.getChannel();
        lock = fileChannel.tryLock();
        // check that we could lock file
        assertTrue(lock.isValid());
    } finally {
        if (lock != null) {
            lock.release();
        }
        if (randomAccessFile != null) {
            randomAccessFile.close();
        }
    }
}

From source file:com.thoughtworks.go.config.GoConfigFileWriter.java

public synchronized void writeToConfigXmlFile(String content) {
    FileChannel channel = null;//from   ww  w  .j  a  va2 s  .com
    FileOutputStream outputStream = null;
    FileLock lock = null;

    try {
        RandomAccessFile randomAccessFile = new RandomAccessFile(fileLocation(), "rw");
        channel = randomAccessFile.getChannel();
        lock = channel.lock();
        randomAccessFile.seek(0);
        randomAccessFile.setLength(0);
        outputStream = new FileOutputStream(randomAccessFile.getFD());

        IOUtils.write(content, outputStream, UTF_8);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (channel != null && lock != null) {
            try {
                lock.release();
                channel.close();
                IOUtils.closeQuietly(outputStream);
            } catch (IOException e) {
                LOGGER.error("Error occured when releasing file lock and closing file.", e);
            }
        }
    }
}

From source file:org.pentaho.di.trans.steps.excelinput.StaxWorkBookTest.java

@Test
public void testRead() throws Exception {
    FileLock lock = null;
    RandomAccessFile randomAccessFile = null;
    try {//from  w w w.  ja v  a  2 s. c om
        readData();
        File fileAfterRead = new File("testfiles/sample-file.xlsx");
        randomAccessFile = new RandomAccessFile(fileAfterRead, "rw");
        FileChannel fileChannel = randomAccessFile.getChannel();
        lock = fileChannel.tryLock();
        // check that we could lock file
        assertTrue(lock.isValid());
    } finally {
        if (lock != null) {
            lock.release();
        }
        if (randomAccessFile != null) {
            randomAccessFile.close();
        }
    }
}

From source file:org.apache.hadoop.hdfs.server.datanode.DataStorage.java

public boolean isConversionNeeded(StorageDirectory sd) throws IOException {
    File oldF = new File(sd.getRoot(), "storage");
    if (!oldF.exists())
        return false;
    // check the layout version inside the storage file
    // Lock and Read old storage file
    RandomAccessFile oldFile = new RandomAccessFile(oldF, "rws");
    FileLock oldLock = oldFile.getChannel().tryLock();
    try {/*from  w  ww  .  j  ava  2s  . c  o  m*/
        oldFile.seek(0);
        int oldVersion = oldFile.readInt();
        if (oldVersion < LAST_PRE_UPGRADE_LAYOUT_VERSION)
            return false;
    } finally {
        oldLock.release();
        oldFile.close();
    }
    return true;
}

From source file:org.pentaho.di.trans.steps.excelinput.StaxWorkBookIT.java

@Test
public void testRead() throws Exception {
    FileLock lock = null;
    RandomAccessFile randomAccessFile = null;
    try {/*from  www  .  j a v a  2s.  c o  m*/
        readData(sample);
        File fileAfterRead = new File(sample);
        randomAccessFile = new RandomAccessFile(fileAfterRead, "rw");
        FileChannel fileChannel = randomAccessFile.getChannel();
        lock = fileChannel.tryLock();
        // check that we could lock file
        assertTrue(lock.isValid());
    } finally {
        if (lock != null) {
            lock.release();
        }
        if (randomAccessFile != null) {
            randomAccessFile.close();
        }
    }
}

From source file:com.openddal.test.BaseTestCase.java

/**
 * Log an error message.//from   w  w w  .ja v  a 2 s  . c o m
 *
 * @param s the message
 * @param e the exception
 */
public static void logError(String s, Throwable e) {
    if (e == null) {
        e = new Exception(s);
    }
    System.out.flush();
    System.err.println("ERROR: " + s + " " + e.toString() + " ------------------------------");
    e.printStackTrace();
    // synchronize on this class, because file locks are only visible to
    // other JVMs
    synchronized (BaseTestCase.class) {
        try {
            // lock
            FileChannel fc = FilePath.get("error.lock").open("rw");
            FileLock lock;
            while (true) {
                lock = fc.tryLock();
                if (lock != null) {
                    break;
                }
                Thread.sleep(10);
            }
            // append
            FileWriter fw = new FileWriter("error.txt", true);
            PrintWriter pw = new PrintWriter(fw);
            e.printStackTrace(pw);
            pw.close();
            fw.close();
            // unlock
            lock.release();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
    System.err.flush();
}

From source file:fr.acxio.tools.agia.tasks.FileCopyTaskletTest.java

@Test
public void testCannotEmptyOrigin() throws Exception {
    FileCopyTasklet aTasklet = new FileCopyTasklet();
    aTasklet.setOrigin(new FileSystemResource("src/test/resources/testFiles/input.csv"));
    aTasklet.setDestination(new FileSystemResource("target/input-copy8.csv"));
    aTasklet.execute(null, null);//from   w  ww .j  av  a 2 s.  c o m

    File aOrigin = aTasklet.getDestination().getFile();
    assertTrue(aOrigin.exists());
    FileInputStream aInputStream = new FileInputStream(aOrigin);
    FileLock aLock = aInputStream.getChannel().lock(0L, Long.MAX_VALUE, true); // shared lock

    aTasklet.setEmptyOrigin(true);
    aTasklet.setOrigin(new FileSystemResource("target/input-copy8.csv"));
    aTasklet.setDestination(new FileSystemResource("target/input-copy9.csv"));
    try {
        aTasklet.execute(null, null);
        fail("Must throw a FileCopyException");
    } catch (FileCopyException e) {
        // Fallthrough
    } finally {
        aLock.release();
        aInputStream.close();
    }
}

From source file:fr.acxio.tools.agia.tasks.FileCopyTaskletTest.java

@Test
public void testCannotDeleteOrigin() throws Exception {
    FileCopyTasklet aTasklet = new FileCopyTasklet();
    aTasklet.setOrigin(new FileSystemResource("src/test/resources/testFiles/input.csv"));
    aTasklet.setDestination(new FileSystemResource("target/input-copy4.csv"));
    aTasklet.execute(null, null);/*  w ww. j a  va2s  . co m*/

    File aOrigin = aTasklet.getDestination().getFile();
    assertTrue(aOrigin.exists());
    FileInputStream aInputStream = new FileInputStream(aOrigin);
    FileLock aLock = aInputStream.getChannel().lock(0L, Long.MAX_VALUE, true); // shared lock

    aTasklet.setDeleteOrigin(true);
    aTasklet.setOrigin(new FileSystemResource("target/input-copy4.csv"));
    aTasklet.setDestination(new FileSystemResource("target/input-copy5.csv"));
    try {
        aTasklet.execute(null, null);
        fail("Must throw a FileCopyException");
    } catch (FileCopyException e) {
        // Fallthrough
    } finally {
        aLock.release();
        aInputStream.close();
    }
}

From source file:org.apache.hadoop.yarn.server.nodemanager.containermanager.ContainerSecurityUpdaterTask.java

private void writeInternal(Path target, ByteBuffer data) throws IOException {
    try (FileChannel fc = FileChannel.open(target, StandardOpenOption.WRITE, StandardOpenOption.CREATE,
            StandardOpenOption.TRUNCATE_EXISTING)) {
        int numOfRetries = 0;
        FileLock lock = null;
        while (lock == null && numOfRetries < 5) {
            try {
                lock = fc.tryLock();/*from   ww  w .  j  ava2 s  .com*/
                fc.write(data);
            } catch (OverlappingFileLockException ex) {
                lock = null;
                numOfRetries++;
                try {
                    TimeUnit.MILLISECONDS.sleep(100);
                } catch (InterruptedException iex) {
                    throw new IOException(iex);
                }
            } finally {
                if (lock != null) {
                    lock.release();
                }
            }
        }
    }
}

From source file:io.github.eternalbits.compactvd.CompactVD.java

private void compact(File file, int options) throws IOException {
    long mtime = file.lastModified();
    task = DiskImageProgress.COMPACT;/*from   w ww.j  a  va2s.c  o  m*/
    try (RandomAccessFile check = new RandomAccessFile(file, "r")) { // is file?
        try (DiskImage image = DiskImages.open(file, "rw")) {
            FileLock fileLock = image.tryLock();
            verboseProgress(SEARCHING_SPACE);
            image.addObserver(this, false);
            image.optimize(options);
            image.removeObserver(this);
            if (!isCancelled()) {
                verboseProgress("Compacting " + file.getName());
                image.addObserver(this, false);
                image.compact();
                image.removeObserver(this);
            }
            fileLock.release();
        }
    } finally {
        System.out.println(String.format(mtime != file.lastModified() ? IMAGE_CHANGED : IMAGE_NOT_CHANGED,
                file.getName()));
    }
}