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) {
74 // channel.close should be called before release() for rigourous locking.
75 if (rafile!=null && rafile.getFD().valid() && rafile.getChannel()!=null && lock.isValid()) {
76 if (closeChannel && rafile.getChannel().isOpen()) {
80 if (lock!=null && lock.isValid())
84 } catch (IOException e) {
85 log.warn("Whilst releasing lock",e);
91 * gets Locked Stream for reading from
92 * @param atStart true to start reading at beginning of file.
93 * @return null if file not locked
96 public FileInputStream getFileInputStream(boolean atStart) throws IOException {
101 return new FileInputStream(rafile.getFD());
105 * gets Locked stream to write to
106 * FileInput always starts at the *end* of the file (after any truncation)
107 * @param clear true means file will be cleared to zero length
108 * @return null if file is not locked
109 * @throws IOException
111 public FileOutputStream getFileOutputStream(boolean clear) throws IOException {
118 rafile.seek(rafile.length());
119 return new LockedFileOutputStream(rafile.getFD());
123 * return buffered output stream to locked file.
124 * @param clear - true means file is truncated to 0 length before writing
127 public BufferedOutputStream getBufferedOutputStream(boolean clear) throws IOException {
128 OutputStream fos = getFileOutputStream(clear);
130 return new BufferedOutputStream(fos);
134 * @see uk.ac.vamsas.client.simpleclient.Lock#finalize()
136 protected void finalize() throws Throwable {
137 release(true); // we explicitly lose the lock here.
138 // log.debug("lock closing through garbage collection ?");
143 * @see uk.ac.vamsas.client.simpleclient.Lock#getLength()
145 public long length() {
148 return rafile.length();
149 } catch (Exception e) {
150 log.debug("getLength exception:",e);
156 public RandomAccessFile getRaFile() throws IOException {
160 log.debug("Failed to getRaFile on "+target);
164 public FileChannel getRaChannel() throws IOException {
166 return rafile.getChannel();
168 log.debug("Failed to getRaChannel on "+target);