if (filelist.exists()) {
// TODO: see if we need to loop-wait for locks or they just block until
// lock is made...
- // do {
- // listlock = new Lock(filelist); // TODO: wait around if we can't get
- // the lock.
- // } while (!listlock.isLocked());
- listlock = new Lock(filelist);
+ do {
+ listlock = new Lock(filelist); // TODO: wait around if we can't get the lock.
+ } while (!listlock.isLocked());
+ // listlock = new Lock(filelist);
return listlock.isLocked();
}
} else
import java.io.File;
+import org.vamsas.client.SimpleClient;
+
/**
- * @author jim
- * Watches a particular file for its creation, deletion, or modification.
+ * @author jim Watches a particular file for its creation, deletion, or
+ * modification.
*/
public class FileWatcher {
- private File subject=null;
+
+ private File subject = null;
+
private long lastStat;
- boolean exists=false;
+
+ boolean exists = false;
+
/**
- * Make a watcher for a particular file.
- * If the file doesn't exist, the watcher will watch
- * for its creation (and indicate a change of state)
+ * Make a watcher for a particular file. If the file doesn't exist, the
+ * watcher will watch for its creation (and indicate a change of state)
+ * For locked files, the removal of a lock constitutes a change of
+ * state if the file was modified.
+ *
* @param subject
*/
- private boolean check() {
+ private boolean checkLock() {
if (subject!=null) {
+ Lock tl = new Lock(subject);
+ if (tl.isLocked()) {
+ tl.release();
+ return false;
+ } else {
+ return true;
+ }
+ }
+ return false;
+ }
+ private boolean check() {
+ if (subject != null) {
if (!subject.exists()) {
if (exists) {
- exists=false;
- return true;
- }
- return false;
- } else {
- long newStat=subject.lastModified();
- if (exists && lastStat==newStat) {
+ exists = false;
+ return true;
+ }
return false;
+ } else {
+
+ long newStat = subject.lastModified();
+ if (exists && ((lastStat == newStat) || checkLock())) {
+ return false;
+ }
+ lastStat = newStat;
+ exists = true;
+ return true;
}
- lastStat=newStat;
- exists=true;
- return true;
}
- }
return false;
}
-
-
+
public FileWatcher(File subject) {
// TODO Auto-generated constructor stub
this.subject = subject;
check();
}
+
public boolean hasChanged() {
return check();
}
if (!lockfile.createNewFile()) {
return;
}
-
+
lock = (rafile=new RandomAccessFile(lockfile,"rw")).getChannel().tryLock();
} catch (FileNotFoundException e) {
System.err.println("Error! Couldn't create a lockfile at "
}
return false;
}
-
+ public void release() {
+ if (lock!=null) {
+ try {
+ rafile.close();
+ lock.release();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace(System.err);
+ }
+ }
+ }
/* Explicitly release lock (probably don't need to do this!)
* @see java.lang.Object#finalize()
*/
protected void finalize() throws Throwable {
- if (lock!=null)
- lock.release();
+ release();
super.finalize();
}
package org.vamsas.test.simpleclient;
+import java.io.File;
import java.util.Iterator;
import java.util.Vector;
import org.vamsas.client.ClientHandle;
import org.vamsas.client.simpleclient.ClientsFile;
+import org.vamsas.client.simpleclient.FileWatcher;
public class ClientsFileTest {
private static Vector commands;
ClientsFileTest.commands.add(new String("remove"));
ClientsFileTest.commands.add(new String("list"));
ClientsFileTest.commands.add(new String("clear"));
+ ClientsFileTest.commands.add(new String("watch"));
}
private static void complainArgs(int argl, int argpos, String cmd, int argneed, String msg) {
if (argl-argpos<argneed)
e.printStackTrace(System.err);
}
break;
+ case 4:
+ // watch
+ FileWatcher w=new FileWatcher(cf);
+ while (cf.exists()) {
+ if (w.hasChanged()) {
+ System.out.println("-- Watching "+cf.getName());
+ //while (w.hasChanged())
+ // ;
+ ClientHandle[] cl = cfhand.retrieveClientList();
+ if (cl!=null) {
+ for (int chi=0,che=cl.length; chi<che; chi++) {
+ System.out.println("Client "+chi+" ("+cl[chi].getClientName()+" "+cl[chi].getVersion()+" "+cl[chi].getClientUrn()+")");
+ }
+ } else {
+ System.out.println("Client list is empty.");
+ }
+ }
+
+ }
+
}
coms = null;
}