1 package org.vamsas.client.simpleclient;
3 import java.io.FileNotFoundException;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.RandomAccessFile;
7 import java.nio.channels.FileLock;
10 * transient object representing a file lock
19 RandomAccessFile rafile=null;
21 * creates a valid Lock (test with <method>isLocked</method>)
22 * if a lock could be obtained for <param>lockfile</param>
25 public Lock(java.io.File lockfile) {
26 // try and get a lock.
30 if (!lockfile.exists())
31 if (!lockfile.createNewFile()) {
35 lock = (rafile=new RandomAccessFile(lockfile,"rw")).getChannel().tryLock();
36 } catch (FileNotFoundException e) {
37 System.err.println("Error! Couldn't create a lockfile at "
38 + lockfile.getAbsolutePath());
40 } catch (IOException e) {
41 System.err.println("Error! Problems with IO when creating a lock on "
42 + lockfile.getAbsolutePath());
48 if (lock != null && lock.isValid()) {
53 public void release() {
55 if (lock!=null && lock.isValid())
57 if (rafile!=null && rafile.getChannel().isOpen())
58 rafile.getChannel().close();
59 } catch (IOException e) {
60 // TODO Auto-generated catch block
61 e.printStackTrace(System.err);
67 /* Explicitly release lock (probably don't need to do this!)
68 * @see java.lang.Object#finalize()
70 protected void finalize() throws Throwable {