1 package uk.ac.vamsas.client.simpleclient;
3 import java.io.BufferedInputStream;
4 import java.io.BufferedOutputStream;
6 import java.io.FileInputStream;
7 import java.io.FileNotFoundException;
8 import java.io.FileOutputStream;
9 import java.io.IOException;
10 import java.io.OutputStream;
11 import java.io.RandomAccessFile;
12 import java.nio.channels.FileChannel;
13 import java.nio.channels.FileLock;
14 import java.nio.channels.ReadableByteChannel;
20 public class NativeLock extends Lock {
22 protected FileLock lock = null;
26 * @param block true means thread will block until a lock is obtained.
28 public NativeLock(File lockfile, boolean block) {
30 // try and get a lock.
34 /*if (!lockfile.createNewFile()) {
35 log.warn("Failed to create locked file "+lockfile);
39 rafile=new RandomAccessFile(lockfile,"rw");
41 lock = rafile.getChannel().lock();
43 lock = rafile.getChannel().tryLock();
44 if (lock==null || !lock.isValid()) {
45 // failed to get lock. Close the file channel
46 log.debug("failed to get lock for "+lockfile);
47 rafile.getChannel().close();
50 } catch (FileNotFoundException e) {
52 log.debug("Lock failed - normal behaviour for windows locking.");
53 //log.error("Error! Couldn't create a lockfile at "
54 // + lockfile.getAbsolutePath(), e);
55 } catch (IOException e) {
56 log.error("Error! Problems with IO when creating a lock on "
57 + lockfile.getAbsolutePath(),e);
58 } // catch (Exception )
61 public boolean isLocked() {
62 if (lock != null && lock.isValid()) {
68 public void release() {
72 public void release(boolean closeChannel) {
76 // channel.close should be called before release() for rigourous locking.
77 if (lock.isValid() && rafile!=null && rafile.getFD().valid() && rafile.getChannel()!=null) {
78 if (closeChannel && rafile.getChannel().isOpen()) {
82 // just release the lock without doing anything to the channe;l
83 if (lock!=null && lock.isValid())
87 } catch (IOException e) {
88 log.warn("Whilst releasing lock",e);
94 * gets Locked Stream for reading from
95 * @param atStart true to start reading at beginning of file.
96 * @return null if file not locked
99 public FileInputStream getFileInputStream(boolean atStart) throws IOException {
104 return new FileInputStream(rafile.getFD());
108 * gets Locked stream to write to
109 * FileInput always starts at the *end* of the file (after any truncation)
110 * @param clear true means file will be cleared to zero length
111 * @return null if file is not locked
112 * @throws IOException
114 public FileOutputStream getFileOutputStream(boolean clear) throws IOException {
121 rafile.seek(rafile.length());
122 return new LockedFileOutputStream(rafile.getFD());
126 * return buffered output stream to locked file.
127 * @param clear - true means file is truncated to 0 length before writing
130 public BufferedOutputStream getBufferedOutputStream(boolean clear) throws IOException {
131 OutputStream fos = getFileOutputStream(clear);
133 return new BufferedOutputStream(fos);
137 * @see uk.ac.vamsas.client.simpleclient.Lock#finalize()
139 protected void finalize() throws Throwable {
140 release(true); // we explicitly lose the lock here.
141 // log.debug("lock closing through garbage collection ?");
146 * @see uk.ac.vamsas.client.simpleclient.Lock#getLength()
148 public long length() {
151 return rafile.length();
152 } catch (Exception e) {
153 log.debug("getLength exception:",e);
159 public RandomAccessFile getRaFile() throws IOException {
163 log.debug("Failed to getRaFile on "+target);
167 public FileChannel getRaChannel() throws IOException {
169 return rafile.getChannel();
171 log.debug("Failed to getRaChannel on "+target);
175 public boolean isTargetLockFile(File afile) {
176 if (isLocked() && target.equals(afile))