1 package uk.ac.vamsas.client.simpleclient;
3 import java.io.BufferedOutputStream;
5 import java.io.FileInputStream;
6 import java.io.FileNotFoundException;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9 import java.io.RandomAccessFile;
10 import java.nio.channels.FileChannel;
11 import java.nio.channels.ReadableByteChannel;
13 * File based Locking mechanism to get around some bizarre limitations of JarEntry seeking.
14 * Abstract locks have a target file, to which access is controlled when a lock is held. Native locks on WindowsXP seem to conflict with Jar seek operations, so a file lock creates an advisory lock.
16 * A lock file is created, if it doesn't already exist - the naming convention is TargetFile+suffixSeparator+_LockSuffix.
17 * A lock is obtained by locking the lock file with a native lock. The NativeLock is used for this.
21 public class FileLock extends Lock {
22 private File _lock = null;
23 protected static String _LockSuffix="lck";
24 private NativeLock advisory=null;
26 * ensure that the _lock file exists
29 private boolean ensureLockFile(boolean block) {
32 if (advisory!=null && advisory.isLocked())
35 advisory=new NativeLock(_lock, block);
36 } catch (Exception e) {
37 if (!_lock.exists()) {
38 // advisory cannot be created. this is serious.
39 log.fatal("Failed to create advisory lock file "+_lock,e);
40 throw new Error("Failed to create advisory lock file "+_lock);
43 return (advisory!=null) && advisory.isLocked();
46 * call to clear up a filelock file after its been made
51 if ( advisory!=null) {
52 advisory.target.deleteOnExit(); // release will null the target
53 advisory.release(true);
61 * @param block true means thread blocks until FileLock is obtained.
63 public FileLock(File lockfile, boolean block) {
65 // try and get a lock.
67 _lock = make_lockForTarget(lockfile);
68 if (!ensureLockFile(block)) {
69 log.debug("Couldn't get lock on "+_lock);
73 // create target file ready to be written to if necessary.
74 if (!lockfile.exists())
75 if (!lockfile.createNewFile()) {
76 log.warn("Failed to create locked file "+lockfile);
80 } catch (FileNotFoundException e) {
82 log.debug("FileLock failed with target="+lockfile+" and lockfile suffix of "+_LockSuffix);
83 //log.error("Error! Couldn't create a lockfile at "
84 // + lockfile.getAbsolutePath(), e);
85 } catch (IOException e) {
86 log.error("Error! Problems with IO when creating a lock on "
87 + lockfile.getAbsolutePath(),e);
92 * @param lockfile target of lock
93 * @return file object that will be locked for lockfile
95 private File make_lockForTarget(File lockfile) {
96 return new File(lockfile.getParentFile(), lockfile.getName()+"."+_LockSuffix);
98 private boolean openRaFile() throws IOException {
101 if (advisory==null || !advisory.isLocked())
103 if (rafile==null || rafile.getFD()==null || !rafile.getFD().valid()) {
104 rafile=new RandomAccessFile(target,"rw");
106 if (log.isDebugEnabled())
107 log.debug("Reusing existing RandomAccessFile on "+target);
109 return (rafile.getChannel()!=null) && rafile.getChannel().isOpen();
112 public boolean isLocked() {
113 if (advisory != null) {
114 if (advisory.isLocked())
117 if (log.isDebugEnabled())
118 log.debug("Lockfile "+_lock+" unexpectedly deleted ?");
123 public void release() {
127 public void release(boolean closeChannel) {
134 } catch (Exception e) {
135 log.debug("Unexpected exception whilst closing RandomAccessFile on "+target, e);
137 rafile=null; // do not hold reference to rafile anymore either
139 if (log.isDebugEnabled())
140 log.debug("Releasing advisory lock on "+target);
141 // TODO: LATER: verify this change in closeChannel semantics really is correct - ArchiveClient works correctly
146 public FileInputStream getFileInputStream(boolean atStart) throws IOException {
148 log.debug("Don't hold lock on "+target+" to get locked FileInputStream.");
154 return new FileInputStream(rafile.getFD());
158 public FileOutputStream getFileOutputStream(boolean clear) throws IOException {
160 log.debug("Don't hold lock on "+target+" to get locked FileOutputStream.");
168 rafile.seek(rafile.length());
169 return new LockedFileOutputStream(rafile.getFD());
173 public BufferedOutputStream getBufferedOutputStream(boolean clear) throws IOException {
174 log.debug("Getting BufferedOutputStream (clear="+clear+")");
175 FileOutputStream fos = getFileOutputStream(clear);
177 return new BufferedOutputStream(fos);
182 * @see uk.ac.vamsas.client.simpleclient.Lock#getLength()
184 public long length() {
186 if (!target.exists()) {
188 target.createNewFile();
189 } catch (Exception e) {
190 log.error("Invalid lock:Failed to create target file "+target);
196 return target.length();
200 protected void finalize() throws Throwable {
201 release(true); // we explicitly lose the lock here.
204 public RandomAccessFile getRaFile() throws IOException {
205 if (isLocked() && openRaFile()) {
208 log.debug("Failed to getRaFile on target "+target);
211 public FileChannel getRaChannel() throws IOException {
212 if (isLocked() && openRaFile()) {
213 return rafile.getChannel();
215 log.debug("Failed to getRaChannel on target "+target);
218 public boolean isTargetLockFile(File afile) {
221 if (target.equals(afile) || make_lockForTarget(target).equals(afile))