applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / test / simpleclient / ArchiveWatcher.java
index d784a8e..e0d2f9f 100644 (file)
-package uk.ac.vamsas.test.simpleclient;
-
-import java.io.File;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-import uk.ac.vamsas.client.ClientHandle;
-import uk.ac.vamsas.client.simpleclient.FileWatcher;
-import uk.ac.vamsas.client.simpleclient.Lock;
-import uk.ac.vamsas.client.simpleclient.SimpleDocument;
-import uk.ac.vamsas.client.simpleclient.VamsasArchiveReader;
-import uk.ac.vamsas.client.simpleclient.VamsasFile;
-import uk.ac.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);
-    }
-    
-  }
-}
+/*\r
+ * This file is part of the Vamsas Client version 0.1. \r
+ * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, \r
+ *  Andrew Waterhouse and Dominik Lindner.\r
+ * \r
+ * Earlier versions have also been incorporated into Jalview version 2.4 \r
+ * since 2008, and TOPALi version 2 since 2007.\r
+ * \r
+ * The Vamsas Client is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU Lesser General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ *  \r
+ * The Vamsas Client is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU Lesser General Public License for more details.\r
+ * \r
+ * You should have received a copy of the GNU Lesser General Public License\r
+ * along with the Vamsas Client.  If not, see <http://www.gnu.org/licenses/>.\r
+ */\r
+package uk.ac.vamsas.test.simpleclient;\r
+\r
+import java.io.File;\r
+\r
+import org.apache.commons.logging.Log;\r
+import org.apache.commons.logging.LogFactory;\r
+\r
+import uk.ac.vamsas.client.ClientHandle;\r
+import uk.ac.vamsas.client.simpleclient.FileWatcher;\r
+import uk.ac.vamsas.client.simpleclient.Lock;\r
+import uk.ac.vamsas.client.simpleclient.SimpleDocument;\r
+import uk.ac.vamsas.client.simpleclient.VamsasArchiveReader;\r
+import uk.ac.vamsas.client.simpleclient.VamsasFile;\r
+import uk.ac.vamsas.objects.core.VamsasDocument;\r
+\r
+/**\r
+ * demo of archive watching process - should mimic the clientsfileTest\r
+ * watcher/monitor process.\r
+ * \r
+ * @author jimp\r
+ * \r
+ */\r
+public class ArchiveWatcher {\r
+  private static Log log = LogFactory.getLog(ArchiveWatcher.class);\r
+\r
+  private static CommandProcessor cproc = new CommandProcessor();\r
+  static {\r
+    cproc.addCommand("new", 0, "no args");\r
+    cproc.addCommand("delete", 0, "no args");\r
+    cproc.addCommand("watch", 0, "no args");\r
+    cproc.addCommand("file", 1, "Need vamsas archive as argument.");\r
+  }\r
+\r
+  public static void main(String[] args) {\r
+    try {\r
+\r
+      if (args != null && args.length > 0) {\r
+        File archive = new File(args[0]);\r
+        log.info("Watching file " + args[0]);\r
+        int argc = 1;\r
+        while (argc < args.length) {\r
+          // vars needed for operations\r
+          ClientHandle ch;\r
+          int com = cproc.getCommand(args, argc);\r
+          argc++;\r
+          switch (com) {\r
+          case 0:\r
+            // new\r
+            log.info("Doing locked deletion and new-file creation.");\r
+            {\r
+              if (!archive.exists())\r
+                archive.createNewFile();\r
+              VamsasFile sf = new VamsasFile(archive);\r
+              Lock l = sf.getLock();\r
+              archive.delete();\r
+              archive.createNewFile();\r
+              sf.unLock();\r
+            }\r
+            break;\r
+          case 1:\r
+            // delete\r
+            log.info("Deleting " + archive + " without locking it first.");\r
+            archive.delete();\r
+            break;\r
+          case 2:\r
+            // watch\r
+            log.info("Endlessly Watching file " + archive);\r
+            /*\r
+             * if (!archive.exists()) archive.createNewFile();\r
+             */// watch the new file... - taken straight from ClientsFileTest\r
+            FileWatcher w = new FileWatcher(archive);\r
+            while (true) {\r
+              // get watcher's lock to ensure state change is fixed for\r
+              // retrieval\r
+              Lock chlock = w.getChangedState();\r
+              if (chlock != null) {\r
+                log.info("Got lock on "\r
+                    + archive\r
+                    + (archive.exists() ? " exists l=" + archive.length()\r
+                        : "(non existant)"));\r
+                if (archive.length() > 0) {\r
+                  VamsasArchiveReader vreader = new VamsasArchiveReader(archive);\r
+                  SimpleDocument sdoc = new SimpleDocument(\r
+                      "testing vamsas watcher");\r
+                  try {\r
+                    VamsasDocument d = sdoc.getVamsasDocument(vreader);\r
+                    if (d != null) {\r
+                      ArchiveReports.reportDocument(d, vreader, false,\r
+                          System.out);\r
+                    }\r
+                    System.out\r
+                        .println("Update at "\r
+                            + System.currentTimeMillis()\r
+                            + "\n\n********************************************************\n");\r
+                  } catch (Exception e) {\r
+                    log.error("Unmarshalling failed.", e);\r
+                  }\r
+                  vreader.close();\r
+                  w.setState();\r
+                }\r
+              }\r
+            }\r
+            // break;\r
+          case 3: // set file\r
+            archive = new File(args[argc++]);\r
+            break;\r
+          case 4:\r
+            break;\r
+          default:\r
+            log.warn("Unknown command  + " + args[argc++]);\r
+          }\r
+        }\r
+      }\r
+    } catch (Exception e) {\r
+      log.error(e);\r
+    }\r
+\r
+  }\r
+}\r