com.wabacus.util.FileLockTools.java Source code

Java tutorial

Introduction

Here is the source code for com.wabacus.util.FileLockTools.java

Source

/* 
 * Copyright (C) 2010---2014 (wuweixing)<349446658@qq.com>
 * 
 * This file is part of Wabacus 
 * 
 * Wabacus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.wabacus.util;

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class FileLockTools {
    private static Log log = LogFactory.getLog(FileLockTools.class);

    public static Object lock(String lockfile, int waitsec, int maxtimes) {
        Object lockresource = lock(lockfile);
        Random random = new Random();
        int times = 0;
        while (lockresource == null) {
            if (times++ >= maxtimes) {
                log.debug("??" + lockfile + "?" + maxtimes + "?");
                break;
            }
            try {
                Thread.sleep(random.nextInt(waitsec) * 1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            lockresource = lock(lockfile);
        }
        return lockresource;
    }

    public static Object lock(String lockfile) {
        RandomAccessFile raf = null;
        FileChannel fc = null;
        boolean islocked = true;
        try {
            raf = new RandomAccessFile(new File(lockfile), "rw");
            fc = raf.getChannel();
            FileLock fl = fc.tryLock();
            if (fl != null && fl.isValid()) {
                Map mapResources = new HashMap();
                mapResources.put("RAF", raf);
                mapResources.put("FC", fc);
                mapResources.put("FL", fl);
                return mapResources;
            } else {
                islocked = false;
                return null;
            }
        } catch (Exception e) {
            log.error("?" + lockfile + "?", e);
            return null;
        } finally {
            if (!islocked) {
                release(fc);
                release(raf);

            }
        }

    }

    public static boolean unlock(String lockfile, Object resource) {
        try {
            if (resource != null && resource instanceof Map) {
                Map mResources = (Map) resource;
                RandomAccessFile raf = (RandomAccessFile) mResources.get("RAF");
                FileChannel fc = (FileChannel) mResources.get("FC");
                FileLock fl = (FileLock) mResources.get("FL");
                release(fl);
                release(fc);
                release(raf);
                resource = null;
            }
        } catch (Exception e) {
            log.error("" + lockfile + "?");
            return false;
        }
        return true;
    }

    private static boolean release(RandomAccessFile raf) {
        try {
            if (raf != null) {
                raf.close();
                raf = null;
            }
            return true;
        } catch (Exception e) {
            System.out.println("RandomAccessFile");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean deleteLockFile(String lockfile) {
        try {
            File f = new File(lockfile);
            if (f != null && f.isFile()) {
                f.delete();
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean createLockFile(String lockfile) {
        try {
            File f = new File(lockfile);
            if (f != null && f.isFile()) {
                f.delete();
            }
            return f.createNewFile();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    private static boolean release(FileChannel fc) {
        try {
            if (fc != null) {
                fc.close();
                fc = null;
            }
            return true;
        } catch (Exception e) {
            System.out.println("FileChannel");
            e.printStackTrace();
            return false;
        }
    }

    private static boolean release(FileLock fl) {
        try {
            if (fl != null) {
                fl.release();
                fl = null;
            }
            return true;
        } catch (Exception e) {
            System.out.println("FileLock");
            e.printStackTrace();
            return false;
        }
    }

}