1 package uk.ac.vamsas.client.simpleclient;
3 import java.io.BufferedInputStream;
4 import java.io.BufferedOutputStream;
6 import java.io.FileInputStream;
7 import java.io.FileNotFoundException;
8 import java.io.FileOutputStream;
9 import java.io.IOException;
10 import java.io.RandomAccessFile;
11 import java.nio.channels.ReadableByteChannel;
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
17 * Basic methods for classes handling locked IO on files
18 * monitored by all (simpleclient) clients in a vamsas session.
20 *TODO: support non nio file locking capable systems
22 public class SessionFile {
23 private static Log log = LogFactory.getLog(SessionFile.class);
24 protected File sessionFile;
25 protected Lock fileLock = null;
27 protected SessionFile(File file) {
32 protected boolean lockFile(Lock extantlock) {
33 if (fileLock!=null && !fileLock.isLocked()) {
34 fileLock.release();// tidy up invalid lock
37 if (extantlock!=null && extantlock.isLocked())
38 fileLock=extantlock; // THIS IS BROKEN - lockFile then nulls the lock.
41 private boolean ensureSessionFile() {
42 if (sessionFile != null) {
43 if (!sessionFile.exists()) {
46 if (!sessionFile.createNewFile()) {
47 log.error("Failed to create file prior to locking: "+sessionFile);
50 } catch (IOException e) {
51 log.error("Exception when trying to create file "+sessionFile, e);
57 log.error("ensureSessionFile called for non-initialised SessionFile!");
61 * Get a lock for the SessionFile
63 * @return true if lock was made
65 protected boolean lockFile() {
66 if (fileLock != null) {
67 if (fileLock.isLocked()) {
68 if (!ensureSessionFile())
72 // lock failed for some reason.
74 log.info("Unexpected session file lock failure. Trying to get it again.");
78 if (!ensureSessionFile())
80 // TODO: see if we need to loop-wait for locks or they just block until
85 if (fileLock==null || !fileLock.isLocked()) {
86 //try { Thread.sleep(1); } catch (Exception e) {};
87 fileLock = LockFactory.getLock(sessionFile,true); // TODO: wait around if we can't get the lock.
89 } while (tries>0 && !fileLock.isLocked());
90 if (!fileLock.isLocked())
91 log.error("Failed to get lock for "+sessionFile);
92 // fileLock = new Lock(sessionFile);
93 return fileLock.isLocked();
97 * Explicitly release the SessionFile's lock.
99 * @return true if lock was released.
101 protected void unlockFile() {
102 if (fileLock != null) {
109 * Makes a backup of the sessionFile.
110 * @return Backed up SessionFile or null if failed to make backup.
112 protected File backupSessionFile() {
113 return backupSessionFile(fileLock, sessionFile.getName(),".old", sessionFile.getParentFile());
116 protected File backupSessionFile(Lock extantLock, String backupPrefix, String backupSuffix, File backupDir) {
118 if (lockFile(extantLock)) {
120 tempfile = File.createTempFile(backupPrefix, backupSuffix, backupDir);
121 if (fileLock.length()>0) {
122 FileOutputStream tos = new FileOutputStream(tempfile);
123 ReadableByteChannel channel;
124 tos.getChannel().transferFrom(channel=fileLock.getRaChannel(), 0,
127 if (!channel.isOpen())
128 throw new Error(tos.getChannel().getClass()+".transferFrom closes source channel!");
129 if (!lockFile(extantLock))
130 throw new Error("Lost lock for "+sessionFile.getName()+" after backup.");
133 } catch (FileNotFoundException e1) {
134 log.warn("Can't create temp file for "+sessionFile.getName(),e1);
136 } catch (IOException e1) {
137 log.warn("Error when copying content to temp file for "+sessionFile.getName(),e1);
144 * Replaces data in sessionFile with data from file handled by another sessionFile
145 * passes up any exceptions.
146 * @param newData source for new data
148 protected void updateFrom(Lock extantLock, SessionFile newData) throws IOException {
149 log.debug("Updating "+sessionFile.getAbsolutePath()+" from "+newData.sessionFile.getAbsolutePath());
151 throw new IOException("Null newData object.");
152 if (newData.sessionFile==null)
153 throw new IOException("Null SessionFile in newData.");
155 if (!lockFile(extantLock))
156 throw new IOException("Failed to get write lock for "+sessionFile);
157 if (!newData.lockFile())
158 throw new IOException("Failed to get lock for updateFrom "+newData.sessionFile);
159 RandomAccessFile nrafile = newData.fileLock.getRaFile();
161 RandomAccessFile trafile = fileLock.getRaFile();
163 while (trafile==null && --tries>0) {
164 log.debug("Lost lock on "+sessionFile+"! Re-trying for a transfer.");
166 trafile = fileLock.getRaFile();
169 trafile.getChannel().transferFrom(nrafile.getChannel(), 0,
173 * remove all trace of the sessionFile file
176 protected void eraseExistence() {
178 if (sessionFile!=null) {
179 sessionFile.delete();
184 * @see uk.ac.vamsas.client.simpleclient.Lock#getBufferedInputStream(boolean)
186 public BufferedInputStream getBufferedInputStream(boolean atStart) throws IOException {
188 return fileLock.getBufferedInputStream(atStart);
192 * @see uk.ac.vamsas.client.simpleclient.Lock#getBufferedOutputStream(boolean)
194 public BufferedOutputStream getBufferedOutputStream(boolean clear) throws IOException {
196 return fileLock.getBufferedOutputStream(clear);
200 * @see uk.ac.vamsas.client.simpleclient.Lock#getFileInputStream(boolean)
202 public FileInputStream getFileInputStream(boolean atStart) throws IOException {
204 return fileLock.getFileInputStream(atStart);
208 * @see uk.ac.vamsas.client.simpleclient.Lock#getFileOutputStream(boolean)
210 public FileOutputStream getFileOutputStream(boolean clear) throws IOException {
212 return fileLock.getFileOutputStream(clear);