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 * This class is not thread safe.
25 public class documentHandler {
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).
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.
40 * - vamsasdocument.xml : core info
42 * - <applicationname>.version.sessionnumber.raw (string given in vamsasdocument.xml applicationData entry)
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.
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
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.
59 * - Update watcher checks for file change -
62 private File vamsasJar;
63 private Lock newVamsasJarLock;
64 private File newVamsasJar; // file which client application is writing to.
65 private File lockfile;
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);
75 public documentHandler(File lockfile, File vamsasJar) {
76 this.lockfile = lockfile;
77 this.vamsasJar = vamsasJar;
80 * gets an unlocked stream from the vamsas document.
81 * @return reader for vamsasdocument.xml enrty
83 public java.io.Reader getDocumentReader() {
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