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
38 * Get a lock for the SessionFile
40 * @return true if lock was made
42 protected boolean lockFile() {
44 if (fileLock.isLocked())
47 // lock failed for some reason.
50 if (sessionFile != null) {
51 if (!sessionFile.exists()) {
54 if (!sessionFile.createNewFile()) {
55 log.error("Failed to create file prior to locking: "+sessionFile);
58 } catch (IOException e) {
59 log.error("Exception when trying to create file "+sessionFile, e);
63 // TODO: see if we need to loop-wait for locks or they just block until
68 if (fileLock!=null && !fileLock.isLocked())
70 fileLock = new Lock(sessionFile); // TODO: wait around if we can't get the lock.
71 } while (tries>0 && !fileLock.isLocked());
72 if (!fileLock.isLocked())
73 log.error("Failed to get lock for "+sessionFile);
74 // fileLock = new Lock(sessionFile);
75 return fileLock.isLocked();
77 log.error("lockFile called for non-initialised SessionFile!");
84 * Explicitly release the SessionFile's lock.
86 * @return true if lock was released.
88 protected void unlockFile() {
89 if (fileLock != null) {
96 * Makes a backup of the sessionFile.
97 * @return Backed up SessionFile or null if failed to make backup.
99 protected File backupSessionFile() {
100 return backupSessionFile(null, sessionFile.getName(),".old", sessionFile.getParentFile());
103 protected File backupSessionFile(Lock extantLock, String backupPrefix, String backupSuffix, File backupDir) {
105 if (lockFile(extantLock)) {
107 tempfile = File.createTempFile(backupPrefix, backupSuffix, backupDir);
108 FileOutputStream tos = new FileOutputStream(tempfile);
109 tos.getChannel().transferFrom(fileLock.rafile.getChannel(), 0,
110 fileLock.rafile.length());
112 } catch (FileNotFoundException e1) {
113 log.warn("Can't create temp file for "+sessionFile.getName(),e1);
115 } catch (IOException e1) {
116 log.warn("Error when copying content to temp file for "+sessionFile.getName(),e1);
123 * Replaces data in sessionFile with data from file handled by another sessionFile
124 * passes up any exceptions.
125 * @param newData source for new data
127 protected void updateFrom(Lock extantLock, SessionFile newData) throws IOException {
128 log.debug("Updating "+sessionFile.getAbsolutePath()+" from "+newData.sessionFile.getAbsolutePath());
130 throw new IOException("Null newData object.");
131 if (newData.sessionFile==null)
132 throw new IOException("Null SessionFile in newData.");
134 lockFile(extantLock);
136 fileLock.rafile.getChannel().transferFrom(newData.fileLock.rafile.getChannel(), 0,
137 newData.fileLock.rafile.length());
140 * remove all trace of the sessionFile file
143 protected void eraseExistence() {
145 if (sessionFile!=null) {
146 sessionFile.delete();