1 package org.vamsas.client.simpleclient;
3 import java.io.BufferedInputStream;
4 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.FileLock;
12 import org.apache.commons.logging.LogFactory;
15 * transient object representing a file lock
16 * This lock should hold for all processes interacting in a session.
17 * TODO: currently implemented for local filesystem style locking - need a fallback mechanism for systems without file locks.
23 org.apache.commons.logging.Log log = LogFactory.getLog(Lock.class);
25 RandomAccessFile rafile=null;
27 * creates a valid Lock (test with <method>isLocked</method>)
28 * if a lock could be obtained for <param>lockfile</param>
31 public Lock(java.io.File lockfile) {
32 // try and get a lock.
36 if (!lockfile.exists())
37 if (!lockfile.createNewFile()) {
38 log.warn("Failed to create locked file "+lockfile);
42 lock = (rafile=new RandomAccessFile(lockfile,"rw")).getChannel().tryLock();
43 if (lock==null || !lock.isValid()) {
44 // failed to get lock. Close the file channel
45 log.debug("failed to get lock for "+lockfile);
46 rafile.getChannel().close();
49 } catch (FileNotFoundException e) {
51 log.debug("Lock failed - normal behaviour for windows locking.");
52 //log.error("Error! Couldn't create a lockfile at "
53 // + lockfile.getAbsolutePath(), e);
54 } catch (IOException e) {
55 log.error("Error! Problems with IO when creating a lock on "
56 + lockfile.getAbsolutePath(),e);
61 if (lock != null && lock.isValid()) {
66 public void release() {
68 // TODO: verify that channel.close should be called after release() for rigourous locking.
69 if (rafile!=null && rafile.getChannel()!=null) {
70 if (rafile.getChannel().isOpen()) {
71 if (lock!=null && lock.isValid())
73 if (rafile.getChannel().isOpen())
74 rafile.getChannel().close();
77 } catch (IOException e) {
78 log.warn("Whilst releasing lock",e);
85 * gets Locked Stream for reading from
86 * @param atStart true to start reading at beginning of file.
87 * @return null if file not locked
90 public FileInputStream getFileInputStream(boolean atStart) throws IOException {
95 return new FileInputStream(rafile.getFD());
98 * gets Locked stream to write to
99 * FileInput always starts at the *end* of the file (after any truncation)
100 * @param clear true means file will be cleared to zero length
101 * @return null if file is not locked
102 * @throws IOException
104 public FileOutputStream getFileOutputStream(boolean clear) throws IOException {
109 rafile.seek(rafile.length());
110 return new LockedFileOutputStream(rafile.getFD());
113 * return buffered output stream to locked file.
114 * @param clear - true means file is truncated to 0 length before writing
117 public BufferedOutputStream getBufferedOutputStream(boolean clear) throws IOException {
118 FileOutputStream fos = getFileOutputStream(clear);
120 return new BufferedOutputStream(fos);
125 * return buffered input stream for locked file.
126 * @param atStart - true means read from begining of file
127 * @return null if file is not locked.
129 public BufferedInputStream getBufferedInputStream(boolean atStart) throws IOException {
130 FileInputStream fis = getFileInputStream(atStart);
132 return new BufferedInputStream(fis);
135 /* Explicitly release lock (probably don't need to do this!)
136 * @see java.lang.Object#finalize()
138 protected void finalize() throws Throwable {