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