2 package org.vamsas.client.simpleclient;
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;
17 import org.vamsas.objects.core.LockFileDescriptor;
20 * low level vamsas document management routines
21 * analogous to ClientsFile
22 * This class is not thread safe.
26 public class VamsasFile {
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).
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.
41 * - vamsasdocument.xml : core info
43 * - <applicationname>.version.sessionnumber.raw (string given in vamsasdocument.xml applicationData entry)
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.
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
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.
60 * - Update watcher checks for file change -
63 private File vamsasJar;
64 private Lock newVamsasJarLock;
65 private File newVamsasJar; // file which client application is writing to.
66 private File lockfile;
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.
74 * Connect to an existing Vamsas document in a given sessionDir
75 * or create a new one.
78 * @throws java.io.IOException
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.
86 * Expand a previously stored session into the sessionDir
88 * @param storedSession
90 public VamsasFile(File sessionDir, JarFile storedSession) throws IOException {
91 // check if sessionDir is live or not
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.
99 * gets a locked Reader for the vamsas document.
100 * @return reader for vamsasdocument.xml enrty
102 public java.io.Reader getDocumentReader() {
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