1 11 package org.eclipse.update.internal.configurator; 12 13 import java.io.*; 14 import java.nio.channels.FileLock ; 15 import java.nio.channels.OverlappingFileLockException ; 16 17 20 public class Locker_JavaNio implements Locker { 21 private File lockFile; 22 private RandomAccessFile raf; 23 private FileLock fileLock; 24 25 public Locker_JavaNio(File lockFile) { 26 this.lockFile = lockFile; 27 } 28 29 public synchronized boolean lock() throws IOException { 30 raf = new RandomAccessFile(lockFile, "rw"); try{ 32 fileLock = raf.getChannel().tryLock(); 33 } catch(OverlappingFileLockException e) { 34 fileLock = null; 35 } finally { 36 if (fileLock != null) 37 return true; 38 raf.close(); 39 raf = null; 40 } 41 return false; 42 } 43 44 public synchronized void release() { 45 if (fileLock != null) { 46 try { 47 fileLock.release(); 48 } catch (IOException e) { 49 } 51 fileLock = null; 52 } 53 if (raf != null) { 54 try { 55 raf.close(); 56 } catch (IOException e) { 57 } 59 raf = null; 60 } 61 if (lockFile != null) { 62 lockFile.delete(); 63 lockFile = null; 64 } 65 } 66 } 67 | Popular Tags |