1 package org.vamsas.test.simpleclient;
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
8 import uk.ac.vamsas.client.ClientHandle;
9 import uk.ac.vamsas.client.simpleclient.FileWatcher;
10 import uk.ac.vamsas.client.simpleclient.Lock;
11 import uk.ac.vamsas.client.simpleclient.SimpleDocument;
12 import uk.ac.vamsas.client.simpleclient.VamsasArchiveReader;
13 import uk.ac.vamsas.client.simpleclient.VamsasFile;
14 import uk.ac.vamsas.objects.core.VamsasDocument;
16 * demo of archive watching process - should mimic the clientsfileTest watcher/monitor process.
20 public class ArchiveWatcher {
21 private static Log log = LogFactory.getLog(ArchiveWatcher.class);
22 private static CommandProcessor cproc=new CommandProcessor();
24 cproc.addCommand("new", 0, "no args");
25 cproc.addCommand("delete", 0, "no args");
26 cproc.addCommand("watch", 0, "no args");
27 cproc.addCommand("file", 1, "Need vamsas archive as argument.");
30 public static void main(String[] args) {
33 if (args!=null && args.length>0) {
34 File archive = new File(args[0]);
35 log.info("Watching file "+args[0]);
37 while (argc < args.length) {
38 // vars needed for operations
40 int com = cproc.getCommand(args, argc);
45 log.info("Doing locked deletion and new-file creation.");
47 if (!archive.exists())
48 archive.createNewFile();
49 VamsasFile sf = new VamsasFile(archive);
50 Lock l = sf.getLock();
52 archive.createNewFile();
58 log.info("Deleting "+archive+" without locking it first.");
63 log.info("Endlessly Watching file "+archive);
64 /* if (!archive.exists())
65 archive.createNewFile();
66 */ // watch the new file... - taken straight from ClientsFileTest
67 FileWatcher w = new FileWatcher(archive);
69 // get watcher's lock to ensure state change is fixed for retrieval
70 Lock chlock = w.getChangedState();
72 log.info("Got lock on "+archive+(archive.exists() ? " exists l="+archive.length() : "(non existant)"));
73 if (archive.length()>0) {
74 VamsasArchiveReader vreader = new VamsasArchiveReader(archive);
75 SimpleDocument sdoc = new SimpleDocument("testing vamsas watcher");
77 VamsasDocument d = sdoc.getVamsasDocument(vreader);
79 ArchiveReports.reportDocument(d, vreader, false, System.out);
81 System.out.println("Update at "+System.currentTimeMillis()+"\n\n********************************************************\n");
82 } catch (Exception e) {
83 log.error("Unmarshalling failed.",e);
92 archive = new File(args[argc++]);
97 log.warn("Unknown command + "+args[argc++]);
101 } catch (Exception e) {