applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / test / simpleclient / ArchiveWatcher.java
1 /*\r
2  * This file is part of the Vamsas Client version 0.1. \r
3  * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, \r
4  *  Andrew Waterhouse and Dominik Lindner.\r
5  * \r
6  * Earlier versions have also been incorporated into Jalview version 2.4 \r
7  * since 2008, and TOPALi version 2 since 2007.\r
8  * \r
9  * The Vamsas Client is free software: you can redistribute it and/or modify\r
10  * it under the terms of the GNU Lesser General Public License as published by\r
11  * the Free Software Foundation, either version 3 of the License, or\r
12  * (at your option) any later version.\r
13  *  \r
14  * The Vamsas Client is distributed in the hope that it will be useful,\r
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
17  * GNU Lesser General Public License for more details.\r
18  * \r
19  * You should have received a copy of the GNU Lesser General Public License\r
20  * along with the Vamsas Client.  If not, see <http://www.gnu.org/licenses/>.\r
21  */\r
22 package uk.ac.vamsas.test.simpleclient;\r
23 \r
24 import java.io.File;\r
25 \r
26 import org.apache.commons.logging.Log;\r
27 import org.apache.commons.logging.LogFactory;\r
28 \r
29 import uk.ac.vamsas.client.ClientHandle;\r
30 import uk.ac.vamsas.client.simpleclient.FileWatcher;\r
31 import uk.ac.vamsas.client.simpleclient.Lock;\r
32 import uk.ac.vamsas.client.simpleclient.SimpleDocument;\r
33 import uk.ac.vamsas.client.simpleclient.VamsasArchiveReader;\r
34 import uk.ac.vamsas.client.simpleclient.VamsasFile;\r
35 import uk.ac.vamsas.objects.core.VamsasDocument;\r
36 \r
37 /**\r
38  * demo of archive watching process - should mimic the clientsfileTest\r
39  * watcher/monitor process.\r
40  * \r
41  * @author jimp\r
42  * \r
43  */\r
44 public class ArchiveWatcher {\r
45   private static Log log = LogFactory.getLog(ArchiveWatcher.class);\r
46 \r
47   private static CommandProcessor cproc = new CommandProcessor();\r
48   static {\r
49     cproc.addCommand("new", 0, "no args");\r
50     cproc.addCommand("delete", 0, "no args");\r
51     cproc.addCommand("watch", 0, "no args");\r
52     cproc.addCommand("file", 1, "Need vamsas archive as argument.");\r
53   }\r
54 \r
55   public static void main(String[] args) {\r
56     try {\r
57 \r
58       if (args != null && args.length > 0) {\r
59         File archive = new File(args[0]);\r
60         log.info("Watching file " + args[0]);\r
61         int argc = 1;\r
62         while (argc < args.length) {\r
63           // vars needed for operations\r
64           ClientHandle ch;\r
65           int com = cproc.getCommand(args, argc);\r
66           argc++;\r
67           switch (com) {\r
68           case 0:\r
69             // new\r
70             log.info("Doing locked deletion and new-file creation.");\r
71             {\r
72               if (!archive.exists())\r
73                 archive.createNewFile();\r
74               VamsasFile sf = new VamsasFile(archive);\r
75               Lock l = sf.getLock();\r
76               archive.delete();\r
77               archive.createNewFile();\r
78               sf.unLock();\r
79             }\r
80             break;\r
81           case 1:\r
82             // delete\r
83             log.info("Deleting " + archive + " without locking it first.");\r
84             archive.delete();\r
85             break;\r
86           case 2:\r
87             // watch\r
88             log.info("Endlessly Watching file " + archive);\r
89             /*\r
90              * if (!archive.exists()) archive.createNewFile();\r
91              */// watch the new file... - taken straight from ClientsFileTest\r
92             FileWatcher w = new FileWatcher(archive);\r
93             while (true) {\r
94               // get watcher's lock to ensure state change is fixed for\r
95               // retrieval\r
96               Lock chlock = w.getChangedState();\r
97               if (chlock != null) {\r
98                 log.info("Got lock on "\r
99                     + archive\r
100                     + (archive.exists() ? " exists l=" + archive.length()\r
101                         : "(non existant)"));\r
102                 if (archive.length() > 0) {\r
103                   VamsasArchiveReader vreader = new VamsasArchiveReader(archive);\r
104                   SimpleDocument sdoc = new SimpleDocument(\r
105                       "testing vamsas watcher");\r
106                   try {\r
107                     VamsasDocument d = sdoc.getVamsasDocument(vreader);\r
108                     if (d != null) {\r
109                       ArchiveReports.reportDocument(d, vreader, false,\r
110                           System.out);\r
111                     }\r
112                     System.out\r
113                         .println("Update at "\r
114                             + System.currentTimeMillis()\r
115                             + "\n\n********************************************************\n");\r
116                   } catch (Exception e) {\r
117                     log.error("Unmarshalling failed.", e);\r
118                   }\r
119                   vreader.close();\r
120                   w.setState();\r
121                 }\r
122               }\r
123             }\r
124             // break;\r
125           case 3: // set file\r
126             archive = new File(args[argc++]);\r
127             break;\r
128           case 4:\r
129             break;\r
130           default:\r
131             log.warn("Unknown command  + " + args[argc++]);\r
132           }\r
133         }\r
134       }\r
135     } catch (Exception e) {\r
136       log.error(e);\r
137     }\r
138 \r
139   }\r
140 }\r