d784a8ecab2b56c841eb72e3d4b695c3012e56ed
[vamsas.git] / src / uk / ac / vamsas / test / simpleclient / ArchiveWatcher.java
1 package uk.ac.vamsas.test.simpleclient;
2
3 import java.io.File;
4
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7
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;
15 /**
16  * demo of archive watching process - should mimic the clientsfileTest watcher/monitor process.
17  * @author jimp
18  *
19  */
20 public class ArchiveWatcher {
21   private static Log log = LogFactory.getLog(ArchiveWatcher.class);
22   private static CommandProcessor cproc=new CommandProcessor();
23   static {
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.");    
28   }
29   
30   public static void main(String[] args) {
31     try {
32       
33       if (args!=null && args.length>0) {
34         File archive = new File(args[0]);
35         log.info("Watching file "+args[0]);
36         int argc=1;
37         while (argc < args.length) {
38           // vars needed for operations
39           ClientHandle ch;
40           int com = cproc.getCommand(args, argc);
41           argc++;
42           switch (com) {
43           case 0:
44             // new
45             log.info("Doing locked deletion and new-file creation.");
46             {
47               if (!archive.exists())
48                 archive.createNewFile();
49               VamsasFile sf = new VamsasFile(archive);
50               Lock l = sf.getLock();
51               archive.delete();
52               archive.createNewFile();
53               sf.unLock();
54             }
55             break;
56           case 1:
57             // delete
58             log.info("Deleting "+archive+" without locking it first.");
59             archive.delete();
60             break;
61           case 2:
62             // watch
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);
68             while (true) {              
69               // get watcher's lock to ensure state change is fixed for retrieval
70               Lock chlock = w.getChangedState();
71               if (chlock != null) {
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");
76                   try {
77                     VamsasDocument d = sdoc.getVamsasDocument(vreader);
78                     if (d!=null) {
79                       ArchiveReports.reportDocument(d, vreader, false, System.out);
80                     }
81                     System.out.println("Update at "+System.currentTimeMillis()+"\n\n********************************************************\n");
82                   } catch (Exception e) {
83                     log.error("Unmarshalling failed.",e);
84                   }
85                   vreader.close();
86                   w.setState();
87                 }
88               }
89             }
90             // break;
91           case 3: // set file
92             archive = new File(args[argc++]);
93             break;
94           case 4:
95             break;
96             default:
97               log.warn("Unknown command  + "+args[argc++]);
98           }
99         }
100       }
101     } catch (Exception e) {
102       log.error(e);
103     }
104     
105   }
106 }