refactored schema (SequenceSet to DataSet and SequenceSetAnnotation to DataSetAnnotat...
[vamsas.git] / src / org / vamsas / client / simpleclient / documentHandler.java
1
2 package org.vamsas.client.simpleclient;
3
4 import java.io.File;
5 import java.io.FileOutputStream;
6 import java.io.FileReader;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.InputStreamReader;
10 import java.io.PrintWriter;
11 import java.util.Timer;
12 import java.util.jar.JarEntry;
13 import java.util.jar.JarFile;
14 import java.util.jar.JarInputStream;
15 import java.util.jar.JarOutputStream;
16
17 import org.vamsas.objects.core.LockFileDescriptor;
18
19 /**
20  * low level vamsas document management routines
21  * This class is not thread safe.
22  * @author jimp
23  *
24  */
25 public class documentHandler {
26   /**
27    * Zip file entries
28    *  - vamsasdocument.xml : core info
29    *  one or more:
30    *  - <applicationname>.version.sessionnumber.raw (string given in vamsasdocument.xml applicationData entry)
31    * Lockfile
32    *  - filename given in the vamsasdocument.xml. Should be checked for validity by any client and rewritten if necessary. 
33    *    The lockfile can point to the jar itself.
34    * Mode of operation.
35    * Initially - documentHandler either:
36    *  - creates a zip for a new session for the client
37    *  - connect to an existing session zip 
38    *   1. reads session urn file
39    *   2. waits for lock
40    *   3. examines session - decide whether to create new application data slice or connect to one stored in session.
41    *   4. writes info into session file
42    *   5. releases lock and generates local client events.
43    *   6. Creates Watcher thread to generate events.
44    * 
45    * During the session
46    *  - Update watcher checks for file change - 
47    * 
48    */
49   private File vamsasJar;
50   private Lock newVamsasJarLock;
51   private File newVamsasJar; // file which client application is writing to.
52   private File lockfile;
53   private Lock lock;
54   private File newSessionDir; // where new sessions (or copies of existing sessions) can be safely created
55   private long timeout=1000; // time in milliseconds before we give up getting a lock
56   private long interval=100; // time between attempts to lock
57   private PrintWriter vamsasWriterStream; // non null if there is a write to a new Document in progress. 
58   public documentHandler(File sessionDir) throws java.io.IOException {
59       newSessionDir = File.createTempFile("vamsasSimple", "session", sessionDir);
60   }
61   
62   public documentHandler(File lockfile, File vamsasJar) {
63     this.lockfile = lockfile;
64     this.vamsasJar = vamsasJar;
65   }
66   /**
67    * gets an unlocked stream from the vamsas document.
68    * @return reader for vamsasdocument.xml enrty
69    */
70   public java.io.Reader getDocumentReader() {
71     
72     try {
73       JarFile session = new JarFile(vamsasJar);
74       JarEntry vamsasDocument = session.getJarEntry("vamsasDocument.xml");
75       return new InputStreamReader(session.getInputStream(vamsasDocument));
76     } catch (IOException e) {
77       // TODO Auto-generated catch block
78       e.printStackTrace();
79     }
80     return null;
81   }
82   /**
83    * opens a locked UTF-8 stream to the vamsas document.
84    * @return reader for vamsasdocument.xml enrty
85    */
86   public java.io.PrintWriter getDocumentWriter() {
87     if (vamsasWriterStream!=null && vamsasWriterStream.checkError()) {
88       return vamsasWriterStream;
89     }
90     
91     try {
92       
93       newVamsasJarLock = new Lock(lockfile);
94       if (newVamsasJarLock = !=null) {
95         JarOutputStream session = new JarOutputStream(new FileOutputStream(vamsasJar));
96         JarEntry vamsasDocument = session.getJarEntry("vamsasDocument.xml");
97         if (vamsasDocument!=null)
98           return new java.io.PrintWriter(OutputStreamWriter(session.Stream(vamsasDocument)), "UTF-8");
99       } 
100     } catch (IOException e) {
101       // TODO Auto-generated catch block
102       e.printStackTrace();
103     }
104     return null;
105   }
106   
107 }