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