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
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
25 import java.io.ObjectOutputStream;
\r
26 import java.io.RandomAccessFile;
\r
27 import java.util.jar.JarFile;
\r
29 import org.apache.commons.logging.Log;
\r
30 import org.apache.commons.logging.LogFactory;
\r
32 import uk.ac.vamsas.client.simpleclient.Lock;
\r
33 import uk.ac.vamsas.client.simpleclient.SessionFile;
\r
34 import uk.ac.vamsas.client.simpleclient.SimpleDocument;
\r
35 import uk.ac.vamsas.client.simpleclient.VamsasArchiveReader;
\r
36 import uk.ac.vamsas.client.simpleclient.VamsasFile;
\r
37 import uk.ac.vamsas.objects.core.ApplicationData;
\r
38 import uk.ac.vamsas.objects.core.User;
\r
39 import uk.ac.vamsas.objects.core.VAMSAS;
\r
40 import uk.ac.vamsas.objects.core.VamsasDocument;
\r
41 import uk.ac.vamsas.test.objects.Core;
\r
43 public class VamsasArchive {
\r
45 * test the org.vamsas.simpleclient.vamsasArchive class
\r
47 static Log log = LogFactory.getLog(VamsasArchive.class);
\r
49 public static ApplicationData makeDemoAppdata(
\r
50 uk.ac.vamsas.client.simpleclient.VamsasArchive va, String apname,
\r
51 String userName, String userOrg) {
\r
54 VamsasArchiveReader vread = null;
\r
56 vread = va.getOriginalArchiveReader();
\r
57 } catch (Exception e) {
\r
58 log.error("Failed to get original archive reader!", e);
\r
61 ApplicationData appdata = new ApplicationData();
\r
62 appdata.setName("uk.ac.vamsas.test.simpleclient.VamsasArchive");
\r
63 appdata.setData(new String("this is some test data.").getBytes());
\r
64 User apuser = new User();
\r
65 apuser.setFullname(userName);
\r
66 apuser.setOrganization(userOrg);
\r
67 String appdata_ref = "vamsas:" + apname + "/" + apuser.getOrganization()
\r
68 + "/" + apuser.getFullname();
\r
69 SimpleDocument sdoc = new SimpleDocument("test.simpleclient.VamsasArchive");
\r
70 if (vread != null) {
\r
71 VamsasDocument orignalnew;
\r
73 orignalnew = sdoc.getVamsasDocument(vread);
\r
74 log.info("*** Dump follows ***");
\r
76 ArchiveReports.reportDocument(orignalnew, vread, false, System.out);
\r
77 log.info("*** Dump precedes ***");
\r
78 } catch (Exception e) {
\r
79 log.info("makeDemoAppdata: Problems accessing original document");
\r
82 log.info("Reading (and avoiding references to) original data");
\r
83 if (vread.getAppdataStream(appdata_ref) != null) {
\r
86 va.transferAppDataEntry(appdata_ref);
\r
87 } catch (Exception e) {
\r
88 log.warn("Exception when transferring appdata reference : "
\r
93 .getAppdataStream(appdata_ref + "/" + Integer.toString(++i)) != null) {
\r
95 // copy over another duplicate.
\r
96 va.transferAppDataEntry(appdata_ref + "/" + Integer.toString(i));
\r
97 } catch (Exception e) {
\r
98 log.warn("Exception when transferring appdata reference : "
\r
102 // this one must be unique!
\r
103 appdata_ref += "/" + Integer.toString(i);
\r
107 log.info("Adding new data stuff.");
\r
108 log.info("Writing an apdata reference using AppDataStream interface.");
\r
109 apuser.setDataReference(appdata_ref);
\r
110 appdata.addUser(apuser);
\r
111 appdata.setVersion("noggin");
\r
112 // TODO: write instance appdata appdata.setUrn("program:/the.nog/");
\r
114 ObjectOutputStream ost = new ObjectOutputStream(va
\r
115 .getAppDataStream(appdata_ref));
\r
116 ost.writeObject(appdata);
\r
118 } catch (Exception e) {
\r
119 log.warn("Couldn't write appdata reference " + appdata_ref);
\r
124 public static void main(String args[]) {
\r
128 if (args.length > 0)
\r
129 av = new File(args[0]);
\r
131 av = new File("test/vamsas.zip");
\r
133 RandomAccessFile raf = new RandomAccessFile(av, "r");
\r
136 } catch (Exception f) {
\r
137 log.info("Couldn't random access file archive " + av, f);
\r
140 JarFile jf = new JarFile(av, true, JarFile.OPEN_READ);
\r
141 if (jf.getEntry("vamsasDocument.xml") != null) {
\r
142 log.info("Valid archive (sun) " + av);
\r
145 } catch (Exception f) {
\r
146 log.warn("Couldn't access jar archive with sun jartools: " + av, f);
\r
149 org.apache.tools.zip.ZipFile jf = new org.apache.tools.zip.ZipFile(av);
\r
150 if (jf.getEntry("vamsasDocument.xml") != null) {
\r
151 log.info("Valid archive (apache) " + av);
\r
154 } catch (Exception f) {
\r
155 log.warn("Couldn't access jar archive with apache ziptool: " + av, f);
\r
158 log.info("Opening archive " + av);
\r
159 uk.ac.vamsas.client.simpleclient.VamsasArchive varchive = new uk.ac.vamsas.client.simpleclient.VamsasArchive(
\r
162 VAMSAS[] roots = (VAMSAS[]) varchive.getOriginalRoots();
\r
164 if (roots != null) {
\r
165 log.info("Report on Original roots in archive:");
\r
166 ArchiveReports.rootReport(roots, true, System.out);
\r
168 log.info("Getting current vamsas document.");
\r
169 VamsasDocument doc = varchive.getVamsasDocument();
\r
170 ArchiveReports.reportDocument(doc, varchive.getOriginalArchiveReader(),
\r
171 true, System.out); // not modified document so references will still
\r
174 log.info("Retrieving backup");
\r
175 File backup = varchive.backupFile();
\r
176 if (backup == null)
\r
177 log.info(av + " is a New Archive.");
\r
179 log.info(av + " has been backed up as " + backup);
\r
180 File newf = new File(av.getAbsolutePath() + "_new.zip");
\r
181 VamsasFile sfile = new VamsasFile(newf);
\r
183 * if (newf.exists()) { int q=1; do { newf=new
\r
184 * File(av.getAbsolutePath()+"_"+q+++"_new.zip"); } while (newf.exists());
\r
187 if (newf.exists()) {
\r
188 log.info("Removing existing " + newf);
\r
192 log.info("Now writing new Archive into " + newf.getAbsolutePath());
\r
193 uk.ac.vamsas.client.simpleclient.VamsasArchive va = null;
\r
194 { // hold lock over deletion and write of new archive.
\r
195 // Lock wlock = sfile.getLock();
\r
196 // newf.delete(); // clear out old file.
\r
198 va = new uk.ac.vamsas.client.simpleclient.VamsasArchive(newf, true,
\r
200 // open another and...
\r
201 ApplicationData appdata = makeDemoAppdata(va,
\r
202 "uk.ac.vamsas.test.simpleclient.VamsasArchive",
\r
203 "arnold Bugger esq", "disOrganised");
\r
204 log.info("Preparing to write new document.");
\r
205 doc.addApplicationData(appdata);
\r
206 doc.addVAMSAS(Core.getDemoVamsas());
\r
207 va.putVamsasDocument(doc); // gets stream and puts it.
\r
211 log.info("Dump of new vamsas document :");
\r
212 log.info("Testing update: ");
\r
214 Lock lock = sfile.getLock();
\r
216 while ((lock = sfile.getLock()) == null) {
\r
217 log.info("Waiting for lock.");
\r
220 VamsasArchiveReader vreader = new VamsasArchiveReader(sfile
\r
221 .getVamsasFile());// lock); // cannot do new JarFile on a locked
\r
223 SimpleDocument sdoc = new SimpleDocument("testing new vamsas write");
\r
224 ArchiveReports.reportDocument(sdoc.getVamsasDocument(vreader), vreader,
\r
228 // backup.delete(); // tidy up
\r
230 log.info("Now Cancelling write to original archive " + av);
\r
231 if (varchive.cancelArchive())
\r
232 log.info("Successfully cancelled.");
\r
234 log.info("Didn't cancel.");
\r
235 long t = System.currentTimeMillis() + 200;
\r
236 while (t > System.currentTimeMillis())
\r
238 log.info("Now testing archive update.");
\r
239 va = new uk.ac.vamsas.client.simpleclient.VamsasArchive(newf, false,
\r
241 doc = va.getVamsasDocument();
\r
242 doc.addVAMSAS(Core.getDemoVamsas());
\r
243 doc.addApplicationData(makeDemoAppdata(va,
\r
244 "uk.ac.vamsas.test.simpleclient.VamsasArchive",
\r
245 "another old Bugger esq", "rescinded"));
\r
246 if (va.transferRemainingAppDatas())
\r
247 log.info("Remain appdatas were transferred.");
\r
249 log.warn("No appdatas were transferred. This is wrong.");
\r
250 va.putVamsasDocument(doc);
\r
253 log.info("Testing update: ");
\r
255 Lock lock = sfile.getLock();
\r
257 while ((lock = sfile.getLock()) == null)
\r
258 log.info("Waiting for lock.");
\r
259 // VamsasArchiveReader vreader = new VamsasArchiveReader(lock);
\r
260 VamsasArchiveReader vreader = new VamsasArchiveReader(newf);
\r
262 SimpleDocument sdoc = new SimpleDocument("testing vamsas update");
\r
263 VamsasDocument finaldoc = sdoc.getVamsasDocument(vreader);
\r
264 if (finaldoc != null)
\r
265 ArchiveReports.reportDocument(finaldoc, vreader, true, System.out);
\r
267 log.error("Null Document Read from " + newf);
\r
269 } catch (Exception e) {
\r
270 e.printStackTrace(System.err);
\r