1 package org.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 log.fatal("Failed to create advisory lock file "+_lock,e);
38 throw new Error("Failed to create advisory lock file "+_lock);
40 return advisory.isLocked();
43 * call to clear up a filelock file after its been made
49 advisory.release(true);
50 advisory.target.deleteOnExit();
57 * @param block true means thread blocks until FileLock is obtained.
59 public FileLock(File lockfile, boolean block) {
61 // try and get a lock.
63 _lock = new File(lockfile.getParentFile(), lockfile.getName()+"."+_LockSuffix);
64 if (!ensureLockFile(block)) {
65 log.debug("Couldn't get lock on "+_lock);
69 // create target file ready to be written to if necessary.
70 if (!lockfile.exists())
71 if (!lockfile.createNewFile()) {
72 log.warn("Failed to create locked file "+lockfile);
76 } catch (FileNotFoundException e) {
78 log.debug("FileLock failed with target="+lockfile+" and lockfile suffix of "+_LockSuffix);
79 //log.error("Error! Couldn't create a lockfile at "
80 // + lockfile.getAbsolutePath(), e);
81 } catch (IOException e) {
82 log.error("Error! Problems with IO when creating a lock on "
83 + lockfile.getAbsolutePath(),e);
87 private boolean openRaFile() throws IOException {
90 if (advisory==null || !advisory.isLocked())
92 if (rafile==null || rafile.getFD()==null || !rafile.getFD().valid()) {
93 rafile=new RandomAccessFile(target,"rw");
95 if (log.isDebugEnabled())
96 log.debug("Reusing existing RandomAccessFile on "+target);
98 return (rafile.getChannel()!=null) && rafile.getChannel().isOpen();
101 public boolean isLocked() {
102 if (advisory != null) {
103 if (advisory.isLocked())
106 if (log.isDebugEnabled())
107 log.debug("Lockfile "+_lock+" unexpectedly deleted ?");
112 public void release() {
116 public void release(boolean closeChannel) {
119 if (log.isDebugEnabled())
120 log.debug("Releasing advisory lock on "+target);
125 } catch (Exception e) {
126 log.debug("Unexpected exception whilst closing RandomAccessFile on "+target, e);
133 public FileInputStream getFileInputStream(boolean atStart) throws IOException {
135 log.debug("Don't hold lock on "+target+" to get locked FileInputStream.");
141 return new FileInputStream(rafile.getFD());
145 public FileOutputStream getFileOutputStream(boolean clear) throws IOException {
147 log.debug("Don't hold lock on "+target+" to get locked FileOutputStream.");
155 rafile.seek(rafile.length());
156 return new LockedFileOutputStream(rafile.getFD());
160 public BufferedOutputStream getBufferedOutputStream(boolean clear) throws IOException {
161 log.debug("Getting BufferedOutputStream (clear="+clear+")");
162 FileOutputStream fos = getFileOutputStream(clear);
164 return new BufferedOutputStream(fos);
169 * @see org.vamsas.client.simpleclient.Lock#getLength()
171 public long length() {
173 if (!target.exists()) {
175 target.createNewFile();
176 } catch (Exception e) {
177 log.error("Invalid lock:Failed to create target file "+target);
183 return target.length();
187 protected void finalize() throws Throwable {
188 release(true); // we explicitly lose the lock here.
191 public RandomAccessFile getRaFile() throws IOException {
192 if (isLocked() && openRaFile()) {
195 log.debug("Failed to getRaFile on target "+target);
198 public FileChannel getRaChannel() throws IOException {
199 if (isLocked() && openRaFile()) {
200 return rafile.getChannel();
202 log.debug("Failed to getRaChannel on target "+target);