Example usage for java.nio.channels FileChannel tryLock

List of usage examples for java.nio.channels FileChannel tryLock

Introduction

In this page you can find the example usage for java.nio.channels FileChannel tryLock.

Prototype

public final FileLock tryLock() throws IOException 

Source Link

Document

Attempts to acquire an exclusive lock on this channel's file.

Usage

From source file:org.mule.transport.file.FileMessageReceiver.java

/**
 * Try to acquire a lock on a file and release it immediately. Usually used as a
 * quick check to see if another process is still holding onto the file, e.g. a
 * large file (more than 100MB) is still being written to.
 *
 * @param sourceFile file to check// ww  w. j a  v  a 2 s .co m
 * @return <code>true</code> if the file can be locked
 */
protected boolean attemptFileLock(final File sourceFile) throws MuleException {
    // check if the file can be processed, be sure that it's not still being
    // written
    // if the file can't be locked don't process it yet, since creating
    // a new FileInputStream() will throw an exception
    FileLock lock = null;
    FileChannel channel = null;
    boolean fileCanBeLocked = false;
    try {
        channel = new RandomAccessFile(sourceFile, "rw").getChannel();

        // Try acquiring the lock without blocking. This method returns
        // null or throws an exception if the file is already locked.
        lock = channel.tryLock();
    } catch (FileNotFoundException fnfe) {
        throw new DefaultMuleException(FileMessages.fileDoesNotExist(sourceFile.getName()));
    } catch (IOException e) {
        // Unable to create a lock. This exception should only be thrown when
        // the file is already locked. No sense in repeating the message over
        // and over.
    } finally {
        if (lock != null) {
            // if lock is null the file is locked by another process
            fileCanBeLocked = true;
            try {
                // Release the lock
                lock.release();
            } catch (IOException e) {
                // ignore
            }
        }

        if (channel != null) {
            try {
                // Close the file
                channel.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }

    return fileCanBeLocked;
}

From source file:org.panbox.core.crypto.io.EncRandomAccessFile.java

/**
 * tries to lock the underlying {@link RandomAccessFile}, if opened
 * /*from ww  w.ja v a2 s . co  m*/
 * @param blocking
 * @return
 * @throws IOException
 */
public synchronized FileLock lock(boolean blocking) throws IOException {
    if (isOpen()) {
        FileChannel channel = backingRandomAccessFile.getChannel();
        FileLock ret;
        if (blocking) {
            ret = channel.lock();
        } else {
            ret = channel.tryLock();
        }

        return ret;
    } else {
        throw new IOException("Can't acquire lock for closed file!");
    }
}

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

License:asdf

@Test
public void testResourceFree() throws Exception {
    FileLock lock = null;/*from  ww w. j  a  v a 2 s.c  o  m*/
    RandomAccessFile randomAccessFile = null;
    try {
        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:org.pentaho.di.trans.steps.excelinput.PoiWorkBookTest.java

@Test
public void testResourceFree() throws Exception {
    FileLock lock = null;//  w  w  w .  java2  s. c  o m
    RandomAccessFile randomAccessFile = null;
    try {
        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.pentaho.di.trans.steps.excelinput.StaxWorkBookIT.java

@Test
public void testRead() throws Exception {
    FileLock lock = null;//from   ww w  .j av a  2  s  .com
    RandomAccessFile randomAccessFile = null;
    try {
        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:org.pentaho.di.trans.steps.excelinput.StaxWorkBookTest.java

@Test
public void testRead() throws Exception {
    FileLock lock = null;/*  ww w  .  ja v  a 2 s .  com*/
    RandomAccessFile randomAccessFile = null;
    try {
        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: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 {//www .  j  a  va 2  s.  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;
}