random bits and pieces whilst in hamburg - beginnings of file handliongimpmementation.
[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    * Vamsas client is intialised with a path to create live session directories. 
28    * This path may contain a vamsas.properties file 
29    * that sets additional parameters (otherwise client 
30    * just uses the one on the classpath).
31    * 
32    * A vamsas session consists of :
33    *  SessionDir - translates to urn of a live session.
34    *  Contains: Vamsas Document (as a jar), Session client list file, 
35    *  both of which may be locked, and additional 
36    *  temporary versions of these files when write 
37    *  operations are taking place.
38    * 
39    * Zip file entries
40    *  - vamsasdocument.xml : core info
41    *  one or more:
42    *  - <applicationname>.version.sessionnumber.raw (string given in vamsasdocument.xml applicationData entry)
43    *  
44    * Lockfile
45    *  - filename given in the vamsasdocument.xml. Should be checked for validity by any client and rewritten if necessary. 
46    *    The lockfile can point to the jar itself.
47    * Mode of operation.
48    * Initially - documentHandler either:
49    *  - creates a zip for a new session for the client
50    *  - connect to an existing session zip 
51    *   1. reads session urn file
52    *   2. waits for lock
53    *   3. examines session - decide whether to create new application data slice or connect to one stored in session.
54    *   4. writes info into session file
55    *   5. releases lock and generates local client events.
56    *   6. Creates Watcher thread to generate events.
57    * 
58    * During the session
59    *  - Update watcher checks for file change - 
60    * 
61    */
62   private File vamsasJar;
63   private Lock newVamsasJarLock;
64   private File newVamsasJar; // file which client application is writing to.
65   private File lockfile;
66   private Lock lock;
67   private File newSessionDir; // where new sessions (or copies of existing sessions) can be safely created
68   private long timeout=1000; // time in milliseconds before we give up getting a lock
69   private long interval=100; // time between attempts to lock
70   private PrintWriter vamsasWriterStream; // non null if there is a write to a new Document in progress. 
71   public documentHandler(File sessionDir) throws java.io.IOException {
72       newSessionDir = File.createTempFile("vamsasSimple", "session", sessionDir);
73   }
74   
75   public documentHandler(File lockfile, File vamsasJar) {
76     this.lockfile = lockfile;
77     this.vamsasJar = vamsasJar;
78   }
79   /**
80    * gets an unlocked stream from the vamsas document.
81    * @return reader for vamsasdocument.xml enrty
82    */
83   public java.io.Reader getDocumentReader() {
84     
85     try {
86       JarFile session = new JarFile(vamsasJar);
87       JarEntry vamsasDocument = session.getJarEntry("vamsasDocument.xml");
88       return new InputStreamReader(session.getInputStream(vamsasDocument));
89     } catch (IOException e) {
90       // TODO Auto-generated catch block
91       e.printStackTrace();
92     }
93     return null;
94   }
95   
96   
97 }