applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / test / simpleclient / VamsasArchive.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 import java.io.ObjectOutputStream;\r
26 import java.io.RandomAccessFile;\r
27 import java.util.jar.JarFile;\r
28 \r
29 import org.apache.commons.logging.Log;\r
30 import org.apache.commons.logging.LogFactory;\r
31 \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
42 \r
43 public class VamsasArchive {\r
44   /**\r
45    * test the org.vamsas.simpleclient.vamsasArchive class\r
46    */\r
47   static Log log = LogFactory.getLog(VamsasArchive.class);\r
48 \r
49   public static ApplicationData makeDemoAppdata(\r
50       uk.ac.vamsas.client.simpleclient.VamsasArchive va, String apname,\r
51       String userName, String userOrg) {\r
52     if (va == null)\r
53       return null;\r
54     VamsasArchiveReader vread = null;\r
55     try {\r
56       vread = va.getOriginalArchiveReader();\r
57     } catch (Exception e) {\r
58       log.error("Failed to get original archive reader!", e);\r
59       return null;\r
60     }\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
72       try {\r
73         orignalnew = sdoc.getVamsasDocument(vread);\r
74         log.info("*** Dump follows ***");\r
75 \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
80       }\r
81 \r
82       log.info("Reading (and avoiding references to) original data");\r
83       if (vread.getAppdataStream(appdata_ref) != null) {\r
84         // transfer over\r
85         try {\r
86           va.transferAppDataEntry(appdata_ref);\r
87         } catch (Exception e) {\r
88           log.warn("Exception when transferring appdata reference : "\r
89               + appdata_ref, e);\r
90         }\r
91         int i = 0;\r
92         while (vread\r
93             .getAppdataStream(appdata_ref + "/" + Integer.toString(++i)) != null) {\r
94           try {\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
99                 + appdata_ref, e);\r
100           }\r
101         }\r
102         // this one must be unique!\r
103         appdata_ref += "/" + Integer.toString(i);\r
104       }\r
105     }\r
106 \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
113     try {\r
114       ObjectOutputStream ost = new ObjectOutputStream(va\r
115           .getAppDataStream(appdata_ref));\r
116       ost.writeObject(appdata);\r
117       ost.close();\r
118     } catch (Exception e) {\r
119       log.warn("Couldn't write appdata reference " + appdata_ref);\r
120     }\r
121     return appdata;\r
122   }\r
123 \r
124   public static void main(String args[]) {\r
125 \r
126     try {\r
127       File av;\r
128       if (args.length > 0)\r
129         av = new File(args[0]);\r
130       else\r
131         av = new File("test/vamsas.zip");\r
132       try {\r
133         RandomAccessFile raf = new RandomAccessFile(av, "r");\r
134         raf.readByte();\r
135         raf.close();\r
136       } catch (Exception f) {\r
137         log.info("Couldn't random access file archive " + av, f);\r
138       }\r
139       try {\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
143         }\r
144         jf.close();\r
145       } catch (Exception f) {\r
146         log.warn("Couldn't access jar archive with sun jartools: " + av, f);\r
147       }\r
148       try {\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
152         }\r
153         jf.close();\r
154       } catch (Exception f) {\r
155         log.warn("Couldn't access jar archive with apache ziptool: " + av, f);\r
156       }\r
157 \r
158       log.info("Opening archive " + av);\r
159       uk.ac.vamsas.client.simpleclient.VamsasArchive varchive = new uk.ac.vamsas.client.simpleclient.VamsasArchive(\r
160           av, true);\r
161 \r
162       VAMSAS[] roots = (VAMSAS[]) varchive.getOriginalRoots();\r
163 \r
164       if (roots != null) {\r
165         log.info("Report on Original roots in archive:");\r
166         ArchiveReports.rootReport(roots, true, System.out);\r
167       }\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
172                              // be valid\r
173       // do some stuff\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
178       else\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
182       /*\r
183        * if (newf.exists()) { int q=1; do { newf=new\r
184        * File(av.getAbsolutePath()+"_"+q+++"_new.zip"); } while (newf.exists());\r
185        * }\r
186        */\r
187       if (newf.exists()) {\r
188         log.info("Removing existing " + newf);\r
189         newf.delete();\r
190       }\r
191 \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
197         sfile.getLock();\r
198         va = new uk.ac.vamsas.client.simpleclient.VamsasArchive(newf, true,\r
199             true, sfile);\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
208         va.closeArchive();\r
209         sfile.unLock();\r
210       }\r
211       log.info("Dump of new vamsas document :");\r
212       log.info("Testing update: ");\r
213       {\r
214         Lock lock = sfile.getLock();\r
215         if (lock == null)\r
216           while ((lock = sfile.getLock()) == null) {\r
217             log.info("Waiting for lock.");\r
218             Thread.sleep(100);\r
219           }\r
220         VamsasArchiveReader vreader = new VamsasArchiveReader(sfile\r
221             .getVamsasFile());// lock); // cannot do new JarFile on a locked\r
222                               // file. // newf);\r
223         SimpleDocument sdoc = new SimpleDocument("testing new vamsas write");\r
224         ArchiveReports.reportDocument(sdoc.getVamsasDocument(vreader), vreader,\r
225             true, System.out);\r
226         sfile.unLock();\r
227       }\r
228       // backup.delete(); // tidy up\r
229 \r
230       log.info("Now Cancelling write to original archive " + av);\r
231       if (varchive.cancelArchive())\r
232         log.info("Successfully cancelled.");\r
233       else\r
234         log.info("Didn't cancel.");\r
235       long t = System.currentTimeMillis() + 200;\r
236       while (t > System.currentTimeMillis())\r
237         ;\r
238       log.info("Now testing archive update.");\r
239       va = new uk.ac.vamsas.client.simpleclient.VamsasArchive(newf, false,\r
240           true, sfile);\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
248       else\r
249         log.warn("No appdatas were transferred. This is wrong.");\r
250       va.putVamsasDocument(doc);\r
251       va.closeArchive();\r
252       sfile.unLock();\r
253       log.info("Testing update: ");\r
254       {\r
255         Lock lock = sfile.getLock();\r
256         if (lock == null)\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
261 \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
266         else\r
267           log.error("Null Document Read from " + newf);\r
268       }\r
269     } catch (Exception e) {\r
270       e.printStackTrace(System.err);\r
271     }\r
272   }\r
273 }\r