1 package org.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;
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
15 * Basic methods for classes handling locked IO on files
16 * monitored by all (simpleclient) clients in a vamsas session.
18 *TODO: support non nio file locking capable systems
20 public class SessionFile {
21 private static Log log = LogFactory.getLog(SessionFile.class);
22 protected File sessionFile;
23 protected Lock fileLock = null;
25 protected SessionFile(File file) {
30 protected boolean lockFile(Lock extantlock) {
31 if (fileLock!=null && !fileLock.isLocked()) {
32 fileLock.release();// tidy up invalid lock
41 * Get a lock for the SessionFile
43 * @return true if lock was made
45 protected boolean lockFile() {
47 if (fileLock.isLocked())
50 // lock failed for some reason.
53 if (sessionFile != null) {
54 if (!sessionFile.exists()) {
57 if (!sessionFile.createNewFile()) {
58 log.error("Failed to create file prior to locking: "+sessionFile);
61 } catch (IOException e) {
62 log.error("Exception when trying to create file "+sessionFile, e);
66 // TODO: see if we need to loop-wait for locks or they just block until
71 if (fileLock!=null && !fileLock.isLocked())
73 fileLock = new Lock(sessionFile); // TODO: wait around if we can't get the lock.
74 } while (tries>0 && !fileLock.isLocked());
75 if (!fileLock.isLocked())
76 log.error("Failed to get lock for "+sessionFile);
77 // fileLock = new Lock(sessionFile);
78 return fileLock.isLocked();
80 log.error("lockFile called for non-initialised SessionFile!");
87 * Explicitly release the SessionFile's lock.
89 * @return true if lock was released.
91 protected void unlockFile() {
92 if (fileLock != null) {
99 * Makes a backup of the sessionFile.
100 * @return Backed up SessionFile or null if failed to make backup.
102 protected File backupSessionFile() {
103 return backupSessionFile(null, sessionFile.getName(),".old", sessionFile.getParentFile());
106 protected File backupSessionFile(Lock extantLock, String backupPrefix, String backupSuffix, File backupDir) {
108 if (lockFile(extantLock)) {
110 tempfile = File.createTempFile(backupPrefix, backupSuffix, backupDir);
111 FileOutputStream tos = new FileOutputStream(tempfile);
112 tos.getChannel().transferFrom(fileLock.rafile.getChannel(), 0,
113 fileLock.rafile.length());
115 } catch (FileNotFoundException e1) {
116 log.warn("Can't create temp file for "+sessionFile.getName(),e1);
118 } catch (IOException e1) {
119 log.warn("Error when copying content to temp file for "+sessionFile.getName(),e1);
126 * Replaces data in sessionFile with data from file handled by another sessionFile
127 * passes up any exceptions.
128 * @param newData source for new data
130 protected void updateFrom(Lock extantLock, SessionFile newData) throws IOException {
131 log.debug("Updating "+sessionFile.getAbsolutePath()+" from "+newData.sessionFile.getAbsolutePath());
133 throw new IOException("Null newData object.");
134 if (newData.sessionFile==null)
135 throw new IOException("Null SessionFile in newData.");
137 lockFile(extantLock);
139 fileLock.rafile.getChannel().transferFrom(newData.fileLock.rafile.getChannel(), 0,
140 newData.fileLock.rafile.length());
143 * remove all trace of the sessionFile file
146 protected void eraseExistence() {
148 if (sessionFile!=null) {
149 sessionFile.delete();
154 * @see org.vamsas.client.simpleclient.Lock#getBufferedInputStream(boolean)
156 public BufferedInputStream getBufferedInputStream(boolean atStart) throws IOException {
158 return fileLock.getBufferedInputStream(atStart);
162 * @see org.vamsas.client.simpleclient.Lock#getBufferedOutputStream(boolean)
164 public BufferedOutputStream getBufferedOutputStream(boolean clear) throws IOException {
166 return fileLock.getBufferedOutputStream(clear);
170 * @see org.vamsas.client.simpleclient.Lock#getFileInputStream(boolean)
172 public FileInputStream getFileInputStream(boolean atStart) throws IOException {
174 return fileLock.getFileInputStream(atStart);
178 * @see org.vamsas.client.simpleclient.Lock#getFileOutputStream(boolean)
180 public FileOutputStream getFileOutputStream(boolean clear) throws IOException {
182 return fileLock.getFileOutputStream(clear);