1 package uk.ac.vamsas.test.simpleclient;
4 import java.io.ObjectOutputStream;
5 import java.io.RandomAccessFile;
6 import java.util.jar.JarFile;
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
11 import uk.ac.vamsas.client.simpleclient.Lock;
12 import uk.ac.vamsas.client.simpleclient.SessionFile;
13 import uk.ac.vamsas.client.simpleclient.SimpleDocument;
14 import uk.ac.vamsas.client.simpleclient.VamsasArchiveReader;
15 import uk.ac.vamsas.client.simpleclient.VamsasFile;
16 import uk.ac.vamsas.objects.core.ApplicationData;
17 import uk.ac.vamsas.objects.core.User;
18 import uk.ac.vamsas.objects.core.VAMSAS;
19 import uk.ac.vamsas.objects.core.VamsasDocument;
20 import uk.ac.vamsas.test.objects.Core;
22 public class VamsasArchive {
24 * test the org.vamsas.simpleclient.vamsasArchive class
26 static Log log = LogFactory.getLog(VamsasArchive.class);
27 public static ApplicationData makeDemoAppdata(uk.ac.vamsas.client.simpleclient.VamsasArchive va, String apname, String userName, String userOrg) {
30 VamsasArchiveReader vread=null;
32 vread = va.getOriginalArchiveReader();
35 log.error("Failed to get original archive reader!",e);
38 ApplicationData appdata = new ApplicationData();
39 appdata.setName("uk.ac.vamsas.test.simpleclient.VamsasArchive");
40 appdata.setData(new String("this is some test data.").getBytes());
41 User apuser = new User();
42 apuser.setFullname(userName);
43 apuser.setOrganization(userOrg);
44 String appdata_ref = "vamsas:"+apname+"/"+apuser.getOrganization()+"/"+apuser.getFullname();
45 SimpleDocument sdoc = new SimpleDocument("test.simpleclient.VamsasArchive");
47 VamsasDocument orignalnew;
49 orignalnew = sdoc.getVamsasDocument(vread);
50 log.info("*** Dump follows ***");
52 ArchiveReports.reportDocument(orignalnew, vread, false, System.out);
53 log.info("*** Dump precedes ***");
54 } catch (Exception e) {
55 log.info("makeDemoAppdata: Problems accessing original document");
58 log.info("Reading (and avoiding references to) original data");
59 if (vread.getAppdataStream(appdata_ref)!=null) {
62 va.transferAppDataEntry(appdata_ref);
63 } catch (Exception e) {
64 log.warn("Exception when transferring appdata reference : "+appdata_ref, e);
67 while (vread.getAppdataStream(appdata_ref+"/"+Integer.toString(++i))!=null) {
69 // copy over another duplicate.
70 va.transferAppDataEntry(appdata_ref+"/"+Integer.toString(i));
71 } catch (Exception e) {
72 log.warn("Exception when transferring appdata reference : "+appdata_ref, e);
75 // this one must be unique!
76 appdata_ref+="/"+Integer.toString(i);
80 log.info("Adding new data stuff.");
81 log.info("Writing an apdata reference using AppDataStream interface.");
82 apuser.setDataReference(appdata_ref);
83 appdata.addUser(apuser);
84 appdata.setVersion("noggin");
85 //TODO: write instance appdata appdata.setUrn("program:/the.nog/");
87 ObjectOutputStream ost = new ObjectOutputStream(va.getAppDataStream(appdata_ref));
88 ost.writeObject(appdata);
90 } catch (Exception e) {
91 log.warn("Couldn't write appdata reference "+appdata_ref);
95 public static void main(String args[]) {
100 av = new File(args[0]);
102 av = new File("test/vamsas.zip");
104 RandomAccessFile raf = new RandomAccessFile(av, "r");
107 } catch (Exception f)
109 log.info("Couldn't random access file archive "+av, f);
112 JarFile jf = new JarFile(av, true, JarFile.OPEN_READ);
113 if (jf.getEntry("vamsasDocument.xml")!=null)
115 log.info("Valid archive (sun) "+av);
118 } catch (Exception f)
120 log.warn("Couldn't access jar archive with sun jartools: "+av, f);
123 org.apache.tools.zip.ZipFile jf = new org.apache.tools.zip.ZipFile(av);
124 if (jf.getEntry("vamsasDocument.xml")!=null)
126 log.info("Valid archive (apache) "+av);
129 } catch (Exception f)
131 log.warn("Couldn't access jar archive with apache ziptool: "+av,f);
134 log.info("Opening archive "+av);
135 uk.ac.vamsas.client.simpleclient.VamsasArchive varchive = new uk.ac.vamsas.client.simpleclient.VamsasArchive(av, true);
137 VAMSAS[] roots = (VAMSAS[]) varchive.getOriginalRoots();
140 log.info("Report on Original roots in archive:");
141 ArchiveReports.rootReport(roots, true, System.out);
143 log.info("Getting current vamsas document.");
144 VamsasDocument doc = varchive.getVamsasDocument();
145 ArchiveReports.reportDocument(doc, varchive.getOriginalArchiveReader(), true, System.out); // not modified document so references will still be valid
147 log.info("Retrieving backup");
148 File backup = varchive.backupFile();
150 log.info(av+" is a New Archive.");
152 log.info(av+" has been backed up as "+backup);
153 File newf=new File(av.getAbsolutePath()+"_new.zip");
154 VamsasFile sfile = new VamsasFile(newf);
155 /* if (newf.exists()) {
158 newf=new File(av.getAbsolutePath()+"_"+q+++"_new.zip");
160 while (newf.exists());
163 log.info("Removing existing "+newf);
167 log.info("Now writing new Archive into "+newf.getAbsolutePath());
168 uk.ac.vamsas.client.simpleclient.VamsasArchive va=null;
169 { // hold lock over deletion and write of new archive.
170 //Lock wlock = sfile.getLock();
171 //newf.delete(); // clear out old file.
173 va = new uk.ac.vamsas.client.simpleclient.VamsasArchive(newf, true, true, sfile);
174 // open another and...
175 ApplicationData appdata = makeDemoAppdata(va,
176 "uk.ac.vamsas.test.simpleclient.VamsasArchive", "arnold Bugger esq", "disOrganised");
177 log.info("Preparing to write new document.");
178 doc.addApplicationData(appdata);
179 doc.addVAMSAS(Core.getDemoVamsas());
180 va.putVamsasDocument(doc); // gets stream and puts it.
184 log.info("Dump of new vamsas document :");
185 log.info("Testing update: ");
187 Lock lock=sfile.getLock();
189 while ((lock=sfile.getLock())==null) {
190 log.info("Waiting for lock.");
193 VamsasArchiveReader vreader = new VamsasArchiveReader(sfile.getVamsasFile());// lock); // cannot do new JarFile on a locked file. // newf);
194 SimpleDocument sdoc = new SimpleDocument("testing new vamsas write");
195 ArchiveReports.reportDocument(sdoc.getVamsasDocument(vreader), vreader, true, System.out);
198 // backup.delete(); // tidy up
200 log.info("Now Cancelling write to original archive "+av);
201 if (varchive.cancelArchive())
202 log.info("Successfully cancelled.");
204 log.info("Didn't cancel.");
205 long t=System.currentTimeMillis()+200; while (t>System.currentTimeMillis());
206 log.info("Now testing archive update.");
207 va = new uk.ac.vamsas.client.simpleclient.VamsasArchive(newf, false, true, sfile);
208 doc = va.getVamsasDocument();
209 doc.addVAMSAS(Core.getDemoVamsas());
210 doc.addApplicationData(makeDemoAppdata(va,
211 "uk.ac.vamsas.test.simpleclient.VamsasArchive", "another old Bugger esq", "rescinded"));
212 if (va.transferRemainingAppDatas())
213 log.info("Remain appdatas were transferred.");
215 log.warn("No appdatas were transferred. This is wrong.");
216 va.putVamsasDocument(doc);
219 log.info("Testing update: ");
221 Lock lock=sfile.getLock();
223 while ((lock=sfile.getLock())==null)
224 log.info("Waiting for lock.");
225 // VamsasArchiveReader vreader = new VamsasArchiveReader(lock);
226 VamsasArchiveReader vreader = new VamsasArchiveReader(newf);
228 SimpleDocument sdoc = new SimpleDocument("testing vamsas update");
229 VamsasDocument finaldoc = sdoc.getVamsasDocument(vreader);
231 ArchiveReports.reportDocument(finaldoc, vreader, true, System.out);
233 log.error("Null Document Read from "+newf);
235 } catch (Exception e) {
236 e.printStackTrace(System.err);