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);
56 * @param block true means thread blocks until FileLock is obtained.
58 public FileLock(File lockfile, boolean block) {
60 // try and get a lock.
62 _lock = new File(lockfile.getParentFile(), lockfile.getName()+"."+_LockSuffix);
63 if (!ensureLockFile(block)) {
64 log.debug("Couldn't get lock on "+_lock);
68 // create target file ready to be written to if necessary.
69 if (!lockfile.exists())
70 if (!lockfile.createNewFile()) {
71 log.warn("Failed to create locked file "+lockfile);
75 } catch (FileNotFoundException e) {
77 log.debug("FileLock failed with target="+lockfile+" and lockfile suffix of "+_LockSuffix);
78 //log.error("Error! Couldn't create a lockfile at "
79 // + lockfile.getAbsolutePath(), e);
80 } catch (IOException e) {
81 log.error("Error! Problems with IO when creating a lock on "
82 + lockfile.getAbsolutePath(),e);
86 private boolean openRaFile() throws IOException {
89 if (advisory==null || !advisory.isLocked())
91 if (rafile==null || rafile.getFD()==null || !rafile.getFD().valid()) {
92 rafile=new RandomAccessFile(target,"rw");
94 if (log.isDebugEnabled())
95 log.debug("Reusing existing RandomAccessFile on "+target);
97 return (rafile.getChannel()!=null) && rafile.getChannel().isOpen();
100 public boolean isLocked() {
101 if (advisory != null) {
102 if (advisory.isLocked())
105 if (log.isDebugEnabled())
106 log.debug("Lockfile "+_lock+" unexpectedly deleted ?");
111 public void release() {
115 public void release(boolean closeChannel) {
118 if (log.isDebugEnabled())
119 log.debug("Releasing advisory lock on "+target);
124 } catch (Exception e) {
125 log.debug("Unexpected exception whilst closing RandomAccessFile on "+target, e);
132 public FileInputStream getFileInputStream(boolean atStart) throws IOException {
134 log.debug("Don't hold lock on "+target+" to get locked FileInputStream.");
140 return new FileInputStream(rafile.getFD());
144 public FileOutputStream getFileOutputStream(boolean clear) throws IOException {
146 log.debug("Don't hold lock on "+target+" to get locked FileOutputStream.");
154 rafile.seek(rafile.length());
155 return new LockedFileOutputStream(rafile.getFD());
159 public BufferedOutputStream getBufferedOutputStream(boolean clear) throws IOException {
160 log.debug("Getting BufferedOutputStream (clear="+clear+")");
161 FileOutputStream fos = getFileOutputStream(clear);
163 return new BufferedOutputStream(fos);
168 * @see org.vamsas.client.simpleclient.Lock#getLength()
170 public long length() {
172 if (!target.exists()) {
174 target.createNewFile();
175 } catch (Exception e) {
176 log.error("Invalid lock:Failed to create target file "+target);
182 return target.length();
186 protected void finalize() throws Throwable {
187 release(true); // we explicitly lose the lock here.
190 public RandomAccessFile getRaFile() throws IOException {
191 if (isLocked() && openRaFile()) {
194 log.debug("Failed to getRaFile on target "+target);
197 public FileChannel getRaChannel() throws IOException {
198 if (isLocked() && openRaFile()) {
199 return rafile.getChannel();
201 log.debug("Failed to getRaChannel on target "+target);