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.exists())
35 if (!lockfile.createNewFile()) {
36 log.warn("Failed to create locked file "+lockfile);
40 rafile=new RandomAccessFile(lockfile,"rw");
42 lock = rafile.getChannel().lock();
44 lock = rafile.getChannel().tryLock();
45 if (lock==null || !lock.isValid()) {
46 // failed to get lock. Close the file channel
47 log.debug("failed to get lock for "+lockfile);
48 rafile.getChannel().close();
51 } catch (FileNotFoundException e) {
53 log.debug("Lock failed - normal behaviour for windows locking.");
54 //log.error("Error! Couldn't create a lockfile at "
55 // + lockfile.getAbsolutePath(), e);
56 } catch (IOException e) {
57 log.error("Error! Problems with IO when creating a lock on "
58 + lockfile.getAbsolutePath(),e);
62 public boolean isLocked() {
63 if (lock != null && lock.isValid()) {
69 public void release() {
73 public void release(boolean closeChannel) {
75 // channel.close should be called before release() for rigourous locking.
76 if (rafile!=null && rafile.getFD().valid() && rafile.getChannel()!=null && lock.isValid()) {
77 if (closeChannel && rafile.getChannel().isOpen()) {
81 if (lock!=null && lock.isValid())
85 } catch (IOException e) {
86 log.warn("Whilst releasing lock",e);
92 * gets Locked Stream for reading from
93 * @param atStart true to start reading at beginning of file.
94 * @return null if file not locked
97 public FileInputStream getFileInputStream(boolean atStart) throws IOException {
102 return new FileInputStream(rafile.getFD());
106 * gets Locked stream to write to
107 * FileInput always starts at the *end* of the file (after any truncation)
108 * @param clear true means file will be cleared to zero length
109 * @return null if file is not locked
110 * @throws IOException
112 public FileOutputStream getFileOutputStream(boolean clear) throws IOException {
119 rafile.seek(rafile.length());
120 return new LockedFileOutputStream(rafile.getFD());
124 * return buffered output stream to locked file.
125 * @param clear - true means file is truncated to 0 length before writing
128 public BufferedOutputStream getBufferedOutputStream(boolean clear) throws IOException {
129 OutputStream fos = getFileOutputStream(clear);
131 return new BufferedOutputStream(fos);
135 * @see uk.ac.vamsas.client.simpleclient.Lock#finalize()
137 protected void finalize() throws Throwable {
138 release(true); // we explicitly lose the lock here.
139 // log.debug("lock closing through garbage collection ?");
144 * @see uk.ac.vamsas.client.simpleclient.Lock#getLength()
146 public long length() {
149 return rafile.length();
150 } catch (Exception e) {
151 log.debug("getLength exception:",e);
157 public RandomAccessFile getRaFile() throws IOException {
161 log.debug("Failed to getRaFile on "+target);
165 public FileChannel getRaChannel() throws IOException {
167 return rafile.getChannel();
169 log.debug("Failed to getRaChannel on "+target);