2 * This file is part of the Vamsas Client version 0.2.
\r
3 * Copyright 2010 by Jim Procter, Iain Milne, Pierre Marguerite,
\r
4 * Andrew Waterhouse and Dominik Lindner.
\r
6 * Earlier versions have also been incorporated into Jalview version 2.4
\r
7 * since 2008, and TOPALi version 2 since 2007.
\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
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
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
22 package uk.ac.vamsas.test.simpleclient;
\r
24 import java.io.File;
\r
26 import org.apache.commons.logging.Log;
\r
27 import org.apache.commons.logging.LogFactory;
\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
38 * demo of archive watching process - should mimic the clientsfileTest
\r
39 * watcher/monitor process.
\r
44 public class ArchiveWatcher {
\r
45 private static Log log = LogFactory.getLog(ArchiveWatcher.class);
\r
47 private static CommandProcessor cproc = new CommandProcessor();
\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
55 public static void main(String[] args) {
\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
62 while (argc < args.length) {
\r
63 // vars needed for operations
\r
65 int com = cproc.getCommand(args, argc);
\r
70 log.info("Doing locked deletion and new-file creation.");
\r
72 if (!archive.exists())
\r
73 archive.createNewFile();
\r
74 VamsasFile sf = new VamsasFile(archive);
\r
75 Lock l = sf.getLock();
\r
77 archive.createNewFile();
\r
83 log.info("Deleting " + archive + " without locking it first.");
\r
88 log.info("Endlessly Watching file " + archive);
\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
94 // get watcher's lock to ensure state change is fixed for
\r
96 Lock chlock = w.getChangedState();
\r
97 if (chlock != null) {
\r
98 log.info("Got lock on "
\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
107 VamsasDocument d = sdoc.getVamsasDocument(vreader);
\r
109 ArchiveReports.reportDocument(d, vreader, false,
\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
125 case 3: // set file
\r
126 archive = new File(args[argc++]);
\r
131 log.warn("Unknown command + " + args[argc++]);
\r
135 } catch (Exception e) {
\r