1 package org.vamsas.client.simpleclient;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
12 * Basic methods for classes handling locked IO on files
13 * monitored by all (simpleclient) clients in a vamsas session.
15 *TODO: support non nio file locking capable systems
17 public class SessionFile {
18 private static Log log = LogFactory.getLog(SessionFile.class);
19 protected File sessionFile;
20 protected Lock fileLock = null;
22 protected SessionFile(File file) {
27 protected boolean lockFile(Lock extantlock) {
28 if (fileLock!=null && !fileLock.isLocked()) {
29 fileLock.release();// tidy up invalid lock
36 * Get a lock for the SessionFile
38 * @return true if lock was made
40 protected boolean lockFile() {
42 if (fileLock.isLocked())
45 // lock failed for some reason.
48 if (sessionFile != null) {
49 if (sessionFile.exists()) {
50 // TODO: see if we need to loop-wait for locks or they just block until
55 fileLock = new Lock(sessionFile); // TODO: wait around if we can't get the lock.
56 } while (!fileLock.isLocked());
57 // fileLock = new Lock(sessionFile);
58 return fileLock.isLocked();
61 log.error("lockFile called for non-initialised SessionFile!");
68 * Explicitly release the SessionFile's lock.
70 * @return true if lock was released.
72 protected void unlockFile() {
73 if (fileLock != null) {
80 * Makes a backup of the sessionFile.
81 * @return Backed up SessionFile or null if failed to make backup.
83 protected File backupSessionFile() {
84 return backupSessionFile(null, sessionFile.getName(),".old", sessionFile.getParentFile());
87 protected File backupSessionFile(Lock extantLock, String backupPrefix, String backupSuffix, File backupDir) {
89 if (lockFile(extantLock)) {
91 tempfile = File.createTempFile(backupPrefix, backupSuffix, backupDir);
92 FileOutputStream tos = new FileOutputStream(tempfile);
93 tos.getChannel().transferFrom(fileLock.rafile.getChannel(), 0,
94 fileLock.rafile.length());
96 } catch (FileNotFoundException e1) {
97 log.warn("Can't create temp file for "+sessionFile.getName(),e1);
99 } catch (IOException e1) {
100 log.warn("Error when copying content to temp file for "+sessionFile.getName(),e1);
107 * Replaces data in sessionFile with data from file handled by another sessionFile
108 * passes up any exceptions.
109 * @param newData source for new data
111 protected void updateFrom(Lock extantLock, SessionFile newData) throws IOException {
112 log.debug("Updating "+sessionFile.getAbsolutePath()+" from "+newData.sessionFile.getAbsolutePath());
114 throw new IOException("Null newData object.");
115 if (newData.sessionFile==null)
116 throw new IOException("Null SessionFile in newData.");
118 lockFile(extantLock);
120 fileLock.rafile.getChannel().transferFrom(newData.fileLock.rafile.getChannel(), 0,
121 newData.fileLock.rafile.length());
124 * remove all trace of the sessionFile file
127 protected void eraseExistence() {
129 if (sessionFile!=null) {
130 sessionFile.delete();