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 {
28 * - vamsasdocument.xml : core info
30 * - <applicationname>.version.sessionnumber.raw (string given in vamsasdocument.xml applicationData entry)
32 * - filename given in the vamsasdocument.xml. Should be checked for validity by any client and rewritten if necessary.
33 * The lockfile can point to the jar itself.
35 * Initially - documentHandler either:
36 * - creates a zip for a new session for the client
37 * - connect to an existing session zip
38 * 1. reads session urn file
40 * 3. examines session - decide whether to create new application data slice or connect to one stored in session.
41 * 4. writes info into session file
42 * 5. releases lock and generates local client events.
43 * 6. Creates Watcher thread to generate events.
46 * - Update watcher checks for file change -
49 private File vamsasJar;
50 private Lock newVamsasJarLock;
51 private File newVamsasJar; // file which client application is writing to.
52 private File lockfile;
54 private File newSessionDir; // where new sessions (or copies of existing sessions) can be safely created
55 private long timeout=1000; // time in milliseconds before we give up getting a lock
56 private long interval=100; // time between attempts to lock
57 private PrintWriter vamsasWriterStream; // non null if there is a write to a new Document in progress.
58 public documentHandler(File sessionDir) throws java.io.IOException {
59 newSessionDir = File.createTempFile("vamsasSimple", "session", sessionDir);
62 public documentHandler(File lockfile, File vamsasJar) {
63 this.lockfile = lockfile;
64 this.vamsasJar = vamsasJar;
67 * gets an unlocked stream from the vamsas document.
68 * @return reader for vamsasdocument.xml enrty
70 public java.io.Reader getDocumentReader() {
73 JarFile session = new JarFile(vamsasJar);
74 JarEntry vamsasDocument = session.getJarEntry("vamsasDocument.xml");
75 return new InputStreamReader(session.getInputStream(vamsasDocument));
76 } catch (IOException e) {
77 // TODO Auto-generated catch block
83 * opens a locked UTF-8 stream to the vamsas document.
84 * @return reader for vamsasdocument.xml enrty
86 public java.io.PrintWriter getDocumentWriter() {
87 if (vamsasWriterStream!=null && vamsasWriterStream.checkError()) {
88 return vamsasWriterStream;
93 newVamsasJarLock = new Lock(lockfile);
94 if (newVamsasJarLock = !=null) {
95 JarOutputStream session = new JarOutputStream(new FileOutputStream(vamsasJar));
96 JarEntry vamsasDocument = session.getJarEntry("vamsasDocument.xml");
97 if (vamsasDocument!=null)
98 return new java.io.PrintWriter(OutputStreamWriter(session.Stream(vamsasDocument)), "UTF-8");
100 } catch (IOException e) {
101 // TODO Auto-generated catch block