2ac7e5ff3cd66eab07961b24b915728847e30f8f
[vamsas.git] / src / org / vamsas / client / simpleclient / VamsasFile.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  * analogous to ClientsFile
22  * This class is not thread safe.
23  * @author jimp
24  *
25  */
26 public class VamsasFile {
27   /**
28    * Locked IO methods for vamsas Document.
29    * 
30    * Vamsas client is intialised with a path to create live session directories. 
31    * This path may contain a vamsas.properties file 
32    * that sets additional parameters (otherwise client 
33    * just uses the one on the classpath).
34    * 
35    * A vamsas session consists of :
36    *  SessionDir - translates to urn of a live session.
37    *  Contains: Vamsas Document (as a jar), Session client list file, 
38    *  both of which may be locked, and additional 
39    *  temporary versions of these files when write 
40    *  operations are taking place.
41    * 
42    * Zip file entries
43    *  - vamsasdocument.xml : core info
44    *  one or more:
45    *  - <applicationname>.version.sessionnumber.raw (string given in vamsasdocument.xml applicationData entry)
46    *  
47    * Lockfile
48    *  - filename given in the vamsasdocument.xml. Should be checked for validity by any client and rewritten if necessary. 
49    *    The lockfile can point to the jar itself.
50    * Mode of operation.
51    * Initially - documentHandler either:
52    *  - creates a zip for a new session for the client
53    *  - connect to an existing session zip 
54    *   1. reads session urn file
55    *   2. waits for lock
56    *   3. examines session - decide whether to create new application data slice or connect to one stored in session.
57    *   4. writes info into session file
58    *   5. releases lock and generates local client events.
59    *   6. Creates Watcher thread to generate events.
60    * 
61    * During the session
62    *  - Update watcher checks for file change - 
63    * 
64    */
65   private File vamsasJar;
66   private Lock newVamsasJarLock;
67   private File newVamsasJar; // file which client application is writing to.
68   private File lockfile;
69   private Lock lock;
70   private File newSessionDir; // where new sessions (or copies of existing sessions) can be safely created
71   private long timeout=1000; // time in milliseconds before we give up getting a lock
72   private long interval=100; // time between attempts to lock
73   private PrintWriter vamsasWriterStream; // non null if there is a write to a new Document in progress. 
74   /**
75    * 
76    * Connect to an existing Vamsas document in a given sessionDir
77    * or create a new one.
78    * 
79    * @param sessionDir
80    * @throws java.io.IOException
81    */
82   public VamsasFile(File sessionDir) throws java.io.IOException {
83       // check if directory exists. if not, create it.
84     // check if vamsas jar exists, if not, create it.
85     
86   }
87   /**
88    * Expand a previously stored session into the sessionDir
89    * @param sessionDir
90    * @param storedSession
91    */
92   public VamsasFile(File sessionDir, JarFile storedSession) throws IOException {
93     // check if sessionDir is live or not
94     
95     // if live - try to merge storedSession with sessionDir
96     //  - will probably fail through duplicate object references needing to be dereferenced.
97     // TODO: think of a way of specifying vorba_id scope for an application's references to allow merging of one vamsasDocument with another.
98     
99   }
100   /**
101    * gets a locked Reader for the vamsas document.
102    * @return reader for vamsasdocument.xml enrty
103    */
104   public java.io.Reader getDocumentReader() {
105     
106     try {
107       JarFile session = new JarFile(vamsasJar);
108       JarEntry vamsasDocument = session.getJarEntry("vamsasDocument.xml");
109       return new InputStreamReader(session.getInputStream(vamsasDocument));
110     } catch (IOException e) {
111       // TODO Auto-generated catch block
112       e.printStackTrace();
113     }
114     return null;
115   }
116   
117   
118 }