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.release(true);
53 advisory.target.deleteOnExit();
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 = new File(lockfile.getParentFile(), lockfile.getName()+"."+_LockSuffix);
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);
91 private boolean openRaFile() throws IOException {
94 if (advisory==null || !advisory.isLocked())
96 if (rafile==null || rafile.getFD()==null || !rafile.getFD().valid()) {
97 rafile=new RandomAccessFile(target,"rw");
99 if (log.isDebugEnabled())
100 log.debug("Reusing existing RandomAccessFile on "+target);
102 return (rafile.getChannel()!=null) && rafile.getChannel().isOpen();
105 public boolean isLocked() {
106 if (advisory != null) {
107 if (advisory.isLocked())
110 if (log.isDebugEnabled())
111 log.debug("Lockfile "+_lock+" unexpectedly deleted ?");
116 public void release() {
120 public void release(boolean closeChannel) {
127 } catch (Exception e) {
128 log.debug("Unexpected exception whilst closing RandomAccessFile on "+target, e);
130 rafile=null; // do not hold reference to rafile anymore either
132 if (log.isDebugEnabled())
133 log.debug("Releasing advisory lock on "+target);
134 // TODO: LATER: verify this change in closeChannel semantics really is correct - ArchiveClient works correctly
139 public FileInputStream getFileInputStream(boolean atStart) throws IOException {
141 log.debug("Don't hold lock on "+target+" to get locked FileInputStream.");
147 return new FileInputStream(rafile.getFD());
151 public FileOutputStream getFileOutputStream(boolean clear) throws IOException {
153 log.debug("Don't hold lock on "+target+" to get locked FileOutputStream.");
161 rafile.seek(rafile.length());
162 return new LockedFileOutputStream(rafile.getFD());
166 public BufferedOutputStream getBufferedOutputStream(boolean clear) throws IOException {
167 log.debug("Getting BufferedOutputStream (clear="+clear+")");
168 FileOutputStream fos = getFileOutputStream(clear);
170 return new BufferedOutputStream(fos);
175 * @see uk.ac.vamsas.client.simpleclient.Lock#getLength()
177 public long length() {
179 if (!target.exists()) {
181 target.createNewFile();
182 } catch (Exception e) {
183 log.error("Invalid lock:Failed to create target file "+target);
189 return target.length();
193 protected void finalize() throws Throwable {
194 release(true); // we explicitly lose the lock here.
197 public RandomAccessFile getRaFile() throws IOException {
198 if (isLocked() && openRaFile()) {
201 log.debug("Failed to getRaFile on target "+target);
204 public FileChannel getRaChannel() throws IOException {
205 if (isLocked() && openRaFile()) {
206 return rafile.getChannel();
208 log.debug("Failed to getRaChannel on target "+target);