applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / VamsasFile.java
1 /*
2  * This file is part of the Vamsas Client version 0.1. 
3  * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, 
4  *  Andrew Waterhouse and Dominik Lindner.
5  * 
6  * Earlier versions have also been incorporated into Jalview version 2.4 
7  * since 2008, and TOPALi version 2 since 2007.
8  * 
9  * The Vamsas Client is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *  
14  * The Vamsas Client is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Lesser General Public License for more details.
18  * 
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with the Vamsas Client.  If not, see <http://www.gnu.org/licenses/>.
21  */
22 package uk.ac.vamsas.client.simpleclient;
23
24 import java.io.File;
25 import java.io.FileOutputStream;
26 import java.io.FileReader;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.InputStreamReader;
30 import java.io.PrintWriter;
31 import java.util.Timer;
32 import java.util.jar.JarEntry;
33 import java.util.jar.JarFile;
34 import java.util.jar.JarInputStream;
35 import java.util.jar.JarOutputStream;
36
37 /**
38  * low level vamsas document management routines analogous to ClientsFile Grew
39  * out of io tests on VamsasArchive class in
40  * uk.ac.vamsas.test.simpleclient.VamsasArchive This class is not thread safe.
41  * 
42  * @author jimp
43  * 
44  */
45 public class VamsasFile extends SessionFile {
46   /**
47    * 
48    * Connect to an existing Vamsas document in a given sessionDir or create a
49    * new one.
50    * 
51    * @param sessionDir
52    * @throws java.io.IOException
53    */
54   public VamsasFile(File sessionFile) throws java.io.IOException {
55     super(sessionFile);
56   }
57
58   /**
59    * 
60    * @return the VamsasFile
61    */
62   public File getVamsasFile() {
63     return sessionFile;
64   }
65
66   /**
67    * Expand a previously stored session into the sessionDir
68    * 
69    * @param sessionDir
70    * @param storedSession
71    *          public VamsasFile(File sessionDir, JarFile storedSession) throws
72    *          IOException { // check if sessionDir is live or not if
73    *          (!sessionDir.exists()) { sessionDir.mkdir(); }
74    * 
75    *          { // check its actually a writable directory }
76    * 
77    *          File sfile = new File(sessionDir, "vamsas.jar");
78    *          VamsasFile(sfile); // if live - try to merge storedSession with
79    *          sessionDir // - will probably fail through duplicate Vobject
80    *          references needing to be dereferenced. // TODO: think of a way of
81    *          specifying vorba_id scope for an application's references to allow
82    *          merging of one vamsasDocument with another.
83    * 
84    *          }
85    */
86   /**
87    * public interface for getting a lock. The lock object is internally
88    * referenced so the lock will persist even after the return value of the
89    * method goes out of scope.
90    * 
91    * @return null if lock couldn't be got or a valid Lock object.
92    */
93   public Lock getLock() {
94     if (lockFile())
95       return fileLock;
96     return null;
97   }
98
99   /**
100    * 
101    * @param extantLock
102    * @return null, extantLock or new Lock.
103    */
104   public Lock getLock(Lock extantLock) {
105     if (lockFile(extantLock))
106       return fileLock;
107     return null;
108   }
109
110   /**
111    * explicitly unlocks vamsas file. if you have called getLock() you *must*
112    * call this to release the lock.
113    */
114   public void unLock() {
115     this.unlockFile();
116   }
117
118 }