8340413dc01932c528fc82d8f347929a6ed86f60
[vamsas.git] / 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.simpleclient.FileWatcher;
8 import org.vamsas.client.simpleclient.Lock;
9 import org.vamsas.client.simpleclient.SimpleDocument;
10 import org.vamsas.client.simpleclient.VamsasArchiveReader;
11 import org.vamsas.objects.core.VamsasDocument;
12 /**
13  * demo of archive watching process - should mimic the clientsfileTest watcher/monitor process.
14  * @author jimp
15  *
16  */
17 public class ArchiveWatcher {
18   private static Log log = LogFactory.getLog(ArchiveWatcher.class); 
19   public static void main(String[] args) {
20     try {
21       if (args!=null && args.length>0) {
22         
23         File archive = new File(args[0]);
24         log.info("Watching file "+args[0]);
25 /*        if (!archive.exists())
26           archive.createNewFile();
27  */       // watch the new file... - taken straight from ClientsFileTest
28         FileWatcher w = new FileWatcher(archive);
29         while (true) {
30           
31           // get watcher's lock to ensure state change is fixed for retrieval
32           Lock chlock = w.getChangedState();
33           if (chlock != null)
34             if (archive.length()>0) {
35               log.info("Noticed Testing update: ");
36               VamsasArchiveReader vreader = new VamsasArchiveReader(archive);
37               SimpleDocument sdoc = new SimpleDocument("testing vamsas watcher");
38               try {
39                 VamsasDocument d = sdoc.getVamsasDocument(vreader);
40                 if (d!=null) {
41                   ArchiveReports.reportDocument(d, vreader, false, System.out);
42                 }
43                 System.out.println("Update at "+System.currentTimeMillis()+"\n\n********************************************************\n");
44               } catch (Exception e) {
45                 log.error("Unmarshalling failed.",e);
46               }
47               vreader.close();
48             }
49         }
50       }
51     } catch (Exception e) {
52       log.error(e);
53     }
54     
55   }
56 }