Java FileLock tryLock(File location)

Here you can find the source of tryLock(File location)

Description

try Lock

License

Open Source License

Declaration

public synchronized static final boolean tryLock(File location) 

Method Source Code

//package com.java2s;
/*/*from  ww w .j a v  a2s. co m*/
 *    Copyright (C) 2008-2010 Igor Kriznar
 *    
 *    This file is part of GTD-Free.
 *    
 *    GTD-Free is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *    
 *    GTD-Free is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *    
 *    You should have received a copy of the GNU General Public License
 *    along with GTD-Free.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

import java.io.RandomAccessFile;

import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

public class Main {
    private static File dataFile;
    private static File dataFolder;
    private static FileLock exclusiveLock;
    public static final String DATA_PROPERTY = "gtd-free.data";
    public static final String LOCK_FILE_NAME = "gtd-free.lock";
    public static final String DEFAULT_DATA_FOLDER_NAME = ".gtd-free";

    public synchronized static final boolean tryLock(File location) {
        if (location == null) {
            location = getDataFolder();
        }
        if (exclusiveLock != null) {
            return false;
        }
        try {
            FileChannel lock = new RandomAccessFile(new File(location, LOCK_FILE_NAME), "rw").getChannel(); //$NON-NLS-1$
            exclusiveLock = lock.tryLock();
        } catch (Exception e) {
            return false;
        }
        return exclusiveLock != null;
    }

    public static final File getDataFolder() {
        if (dataFolder == null) {

            String s = System.getProperty(DATA_PROPERTY);

            if (s != null) {
                File f = new File(s);
                if (!f.exists()) {
                    f.mkdirs();
                }
                if (f.isDirectory()) {
                    dataFolder = f;
                } else {
                    dataFolder = f.getParentFile();
                    dataFile = f;
                }
            }

            if (dataFolder == null) {
                dataFolder = new File(System.getProperty("user.home")); //$NON-NLS-1$
                dataFolder = new File(dataFolder, DEFAULT_DATA_FOLDER_NAME);

                System.getProperties().setProperty(DATA_PROPERTY, dataFolder.toString());
            }
            if (!dataFolder.exists()) {
                dataFolder.mkdirs();
            }
        }
        return dataFolder;
    }
}

Related

  1. releaseQuietly(final FileLock lock)
  2. releaseSilent(FileLock fileLock)
  3. setNonBlockingFD(Channel c, boolean blocking)
  4. tryLock(File file)
  5. trylock(File file, boolean shared)
  6. unblockSocket(SelectableChannel... channels)
  7. unlock(File file)
  8. unlock(FileLock lock)
  9. unlockFile()