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