1 11 package org.eclipse.core.runtime.adaptor; 12 13 import java.io.*; 14 import java.nio.channels.FileLock ; 15 import org.eclipse.osgi.framework.log.FrameworkLogEntry; 16 17 20 public class Locker_JavaNio implements Locker { 21 private File lockFile; 22 private FileLock fileLock; 23 private FileOutputStream fileStream; 24 25 public Locker_JavaNio(File lockFile) { 26 this.lockFile = lockFile; 27 } 28 29 public synchronized boolean lock() throws IOException { 30 fileStream = new FileOutputStream(lockFile, true); 31 try { 32 fileLock = fileStream.getChannel().tryLock(); 33 } catch (IOException ioe) { 34 if (BasicLocation.DEBUG) { 36 String basicMessage = EclipseAdaptorMsg.formatter.getString("location.cannotLock", lockFile); FrameworkLogEntry basicEntry = new FrameworkLogEntry(EclipseAdaptor.FRAMEWORK_SYMBOLICNAME, basicMessage, 0, ioe, null); 38 EclipseAdaptor.getDefault().getFrameworkLog().log(basicEntry); 39 } 40 String specificMessage = EclipseAdaptorMsg.formatter.getString("location.cannotLockNIO", new Object [] {lockFile, ioe.getMessage(), BasicLocation.PROP_OSGI_LOCKING}); throw new IOException(specificMessage); 43 } 44 if (fileLock != null) 45 return true; 46 fileStream.close(); 47 fileStream = null; 48 return false; 49 } 50 51 public synchronized void release() { 52 if (fileLock != null) { 53 try { 54 fileLock.release(); 55 } catch (IOException e) { 56 } 58 fileLock = null; 59 } 60 if (fileStream != null) { 61 try { 62 fileStream.close(); 63 } catch (IOException e) { 64 } 66 fileStream = null; 67 } 68 } 69 } | Popular Tags |