package org.vamsas.client.simpleclient; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Timer; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.jar.JarInputStream; import java.util.jar.JarOutputStream; import org.vamsas.objects.core.LockFileDescriptor; /** * low level vamsas document management routines * This class is not thread safe. * @author jimp * */ public class documentHandler { /** * Vamsas client is intialised with a path to create live session directories. * This path may contain a vamsas.properties file * that sets additional parameters (otherwise client * just uses the one on the classpath). * * A vamsas session consists of : * SessionDir - translates to urn of a live session. * Contains: Vamsas Document (as a jar), Session client list file, * both of which may be locked, and additional * temporary versions of these files when write * operations are taking place. * * Zip file entries * - vamsasdocument.xml : core info * one or more: * - .version.sessionnumber.raw (string given in vamsasdocument.xml applicationData entry) * * Lockfile * - filename given in the vamsasdocument.xml. Should be checked for validity by any client and rewritten if necessary. * The lockfile can point to the jar itself. * Mode of operation. * Initially - documentHandler either: * - creates a zip for a new session for the client * - connect to an existing session zip * 1. reads session urn file * 2. waits for lock * 3. examines session - decide whether to create new application data slice or connect to one stored in session. * 4. writes info into session file * 5. releases lock and generates local client events. * 6. Creates Watcher thread to generate events. * * During the session * - Update watcher checks for file change - * */ private File vamsasJar; private Lock newVamsasJarLock; private File newVamsasJar; // file which client application is writing to. private File lockfile; private Lock lock; private File newSessionDir; // where new sessions (or copies of existing sessions) can be safely created private long timeout=1000; // time in milliseconds before we give up getting a lock private long interval=100; // time between attempts to lock private PrintWriter vamsasWriterStream; // non null if there is a write to a new Document in progress. public documentHandler(File sessionDir) throws java.io.IOException { newSessionDir = File.createTempFile("vamsasSimple", "session", sessionDir); } public documentHandler(File lockfile, File vamsasJar) { this.lockfile = lockfile; this.vamsasJar = vamsasJar; } /** * gets an unlocked stream from the vamsas document. * @return reader for vamsasdocument.xml enrty */ public java.io.Reader getDocumentReader() { try { JarFile session = new JarFile(vamsasJar); JarEntry vamsasDocument = session.getJarEntry("vamsasDocument.xml"); return new InputStreamReader(session.getInputStream(vamsasDocument)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }