backtrack
[vamsas.git] / src / org / vamsas / test / simpleclient / ArchiveWatcher.java
diff --git a/src/org/vamsas/test/simpleclient/ArchiveWatcher.java b/src/org/vamsas/test/simpleclient/ArchiveWatcher.java
new file mode 100644 (file)
index 0000000..c1c7422
--- /dev/null
@@ -0,0 +1,105 @@
+package org.vamsas.test.simpleclient;
+
+import java.io.File;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.vamsas.client.ClientHandle;
+import org.vamsas.client.simpleclient.FileWatcher;
+import org.vamsas.client.simpleclient.Lock;
+import org.vamsas.client.simpleclient.SimpleDocument;
+import org.vamsas.client.simpleclient.VamsasArchiveReader;
+import org.vamsas.client.simpleclient.VamsasFile;
+import org.vamsas.objects.core.VamsasDocument;
+/**
+ * demo of archive watching process - should mimic the clientsfileTest watcher/monitor process.
+ * @author jimp
+ *
+ */
+public class ArchiveWatcher {
+  private static Log log = LogFactory.getLog(ArchiveWatcher.class);
+  private static CommandProcessor cproc=new CommandProcessor();
+  static {
+    cproc.addCommand("new", 0, "no args");
+    cproc.addCommand("delete", 0, "no args");    
+    cproc.addCommand("watch", 0, "no args");
+    cproc.addCommand("file", 1, "Need vamsas archive as argument.");    
+  }
+  
+  public static void main(String[] args) {
+    try {
+      
+      if (args!=null && args.length>0) {
+        File archive = new File(args[0]);
+        log.info("Watching file "+args[0]);
+        int argc=1;
+        while (argc < args.length) {
+          // vars needed for operations
+          ClientHandle ch;
+          int com = cproc.getCommand(args, argc);
+          argc++;
+          switch (com) {
+          case 0:
+            // new
+            log.info("Doing locked deletion and new-file creation.");
+            {
+              if (!archive.exists())
+                archive.createNewFile();
+              VamsasFile sf = new VamsasFile(archive);
+              Lock l = sf.getLock();
+              archive.delete();
+              archive.createNewFile();
+              sf.unLock();
+            }
+            break;
+          case 1:
+            // delete
+            log.info("Deleting "+archive+" without locking it first.");
+            archive.delete();
+            break;
+          case 2:
+            // watch
+            log.info("Endlessly Watching file "+archive);
+            /*        if (!archive.exists())
+                      archive.createNewFile();
+             */       // watch the new file... - taken straight from ClientsFileTest
+            FileWatcher w = new FileWatcher(archive);
+            while (true) {              
+              // get watcher's lock to ensure state change is fixed for retrieval
+              Lock chlock = w.getChangedState();
+              if (chlock != null) {
+                log.info("Got lock on "+archive+(archive.exists() ? " exists l="+archive.length() : "(non existant)"));
+                if (archive.length()>0) {
+                  VamsasArchiveReader vreader = new VamsasArchiveReader(archive);
+                  SimpleDocument sdoc = new SimpleDocument("testing vamsas watcher");
+                  try {
+                    VamsasDocument d = sdoc.getVamsasDocument(vreader);
+                    if (d!=null) {
+                      ArchiveReports.reportDocument(d, vreader, false, System.out);
+                    }
+                    System.out.println("Update at "+System.currentTimeMillis()+"\n\n********************************************************\n");
+                  } catch (Exception e) {
+                    log.error("Unmarshalling failed.",e);
+                  }
+                  vreader.close();
+                  w.setState();
+                }
+              }
+            }
+            // break;
+          case 3: // set file
+            archive = new File(args[argc++]);
+            break;
+          case 4:
+            break;
+            default:
+              log.warn("Unknown command  + "+args[argc++]);
+          }
+        }
+      }
+    } catch (Exception e) {
+      log.error(e);
+    }
+    
+  }
+}