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()) {
52 if (!sessionFile.createNewFile()) {
53 log.error("Failed to create file prior to locking: "+sessionFile);
56 } catch (IOException e) {
57 log.error("Exception when trying to create file "+sessionFile, e);
61 // TODO: see if we need to loop-wait for locks or they just block until
66 fileLock = new Lock(sessionFile); // TODO: wait around if we can't get the lock.
67 } while (!fileLock.isLocked());
68 // fileLock = new Lock(sessionFile);
69 return fileLock.isLocked();
71 log.error("lockFile called for non-initialised SessionFile!");
78 * Explicitly release the SessionFile's lock.
80 * @return true if lock was released.
82 protected void unlockFile() {
83 if (fileLock != null) {
90 * Makes a backup of the sessionFile.
91 * @return Backed up SessionFile or null if failed to make backup.
93 protected File backupSessionFile() {
94 return backupSessionFile(null, sessionFile.getName(),".old", sessionFile.getParentFile());
97 protected File backupSessionFile(Lock extantLock, String backupPrefix, String backupSuffix, File backupDir) {
99 if (lockFile(extantLock)) {
101 tempfile = File.createTempFile(backupPrefix, backupSuffix, backupDir);
102 FileOutputStream tos = new FileOutputStream(tempfile);
103 tos.getChannel().transferFrom(fileLock.rafile.getChannel(), 0,
104 fileLock.rafile.length());
106 } catch (FileNotFoundException e1) {
107 log.warn("Can't create temp file for "+sessionFile.getName(),e1);
109 } catch (IOException e1) {
110 log.warn("Error when copying content to temp file for "+sessionFile.getName(),e1);
117 * Replaces data in sessionFile with data from file handled by another sessionFile
118 * passes up any exceptions.
119 * @param newData source for new data
121 protected void updateFrom(Lock extantLock, SessionFile newData) throws IOException {
122 log.debug("Updating "+sessionFile.getAbsolutePath()+" from "+newData.sessionFile.getAbsolutePath());
124 throw new IOException("Null newData object.");
125 if (newData.sessionFile==null)
126 throw new IOException("Null SessionFile in newData.");
128 lockFile(extantLock);
130 fileLock.rafile.getChannel().transferFrom(newData.fileLock.rafile.getChannel(), 0,
131 newData.fileLock.rafile.length());
134 * remove all trace of the sessionFile file
137 protected void eraseExistence() {
139 if (sessionFile!=null) {
140 sessionFile.delete();