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
36 fileLock=extantlock; // THIS IS BROKEN - lockFile then nulls the 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.
52 if (sessionFile != null) {
53 if (!sessionFile.exists()) {
56 if (!sessionFile.createNewFile()) {
57 log.error("Failed to create file prior to locking: "+sessionFile);
60 } catch (IOException e) {
61 log.error("Exception when trying to create file "+sessionFile, e);
65 // TODO: see if we need to loop-wait for locks or they just block until
70 if (fileLock!=null && !fileLock.isLocked()) {
72 try { Thread.sleep(5); } catch (Exception e) {};
74 fileLock = LockFactory.getLock(sessionFile); // TODO: wait around if we can't get the lock.
75 } while (tries>0 && !fileLock.isLocked());
76 if (!fileLock.isLocked())
77 log.error("Failed to get lock for "+sessionFile);
78 // fileLock = new Lock(sessionFile);
79 return fileLock.isLocked();
81 log.error("lockFile called for non-initialised SessionFile!");
88 * Explicitly release the SessionFile's lock.
90 * @return true if lock was released.
92 protected void unlockFile() {
93 if (fileLock != null) {
100 * Makes a backup of the sessionFile.
101 * @return Backed up SessionFile or null if failed to make backup.
103 protected File backupSessionFile() {
104 return backupSessionFile(null, sessionFile.getName(),".old", sessionFile.getParentFile());
107 protected File backupSessionFile(Lock extantLock, String backupPrefix, String backupSuffix, File backupDir) {
109 if (lockFile(extantLock)) {
111 tempfile = File.createTempFile(backupPrefix, backupSuffix, backupDir);
112 FileOutputStream tos = new FileOutputStream(tempfile);
113 tos.getChannel().transferFrom(fileLock.rafile.getChannel(), 0,
114 fileLock.rafile.length());
116 } catch (FileNotFoundException e1) {
117 log.warn("Can't create temp file for "+sessionFile.getName(),e1);
119 } catch (IOException e1) {
120 log.warn("Error when copying content to temp file for "+sessionFile.getName(),e1);
127 * Replaces data in sessionFile with data from file handled by another sessionFile
128 * passes up any exceptions.
129 * @param newData source for new data
131 protected void updateFrom(Lock extantLock, SessionFile newData) throws IOException {
132 log.debug("Updating "+sessionFile.getAbsolutePath()+" from "+newData.sessionFile.getAbsolutePath());
134 throw new IOException("Null newData object.");
135 if (newData.sessionFile==null)
136 throw new IOException("Null SessionFile in newData.");
138 if (!lockFile(extantLock))
139 log.error("Failed to get write lock for "+sessionFile);
140 if (!newData.lockFile())
141 log.warn("Failed to get lock for updateFrom "+newData.sessionFile);
142 fileLock.rafile.getChannel().transferFrom(newData.fileLock.rafile.getChannel(), 0,
143 newData.fileLock.rafile.length());
146 * remove all trace of the sessionFile file
149 protected void eraseExistence() {
151 if (sessionFile!=null) {
152 sessionFile.delete();
157 * @see org.vamsas.client.simpleclient.Lock#getBufferedInputStream(boolean)
159 public BufferedInputStream getBufferedInputStream(boolean atStart) throws IOException {
161 return fileLock.getBufferedInputStream(atStart);
165 * @see org.vamsas.client.simpleclient.Lock#getBufferedOutputStream(boolean)
167 public BufferedOutputStream getBufferedOutputStream(boolean clear) throws IOException {
169 return fileLock.getBufferedOutputStream(clear);
173 * @see org.vamsas.client.simpleclient.Lock#getFileInputStream(boolean)
175 public FileInputStream getFileInputStream(boolean atStart) throws IOException {
177 return fileLock.getFileInputStream(atStart);
181 * @see org.vamsas.client.simpleclient.Lock#getFileOutputStream(boolean)
183 public FileOutputStream getFileOutputStream(boolean clear) throws IOException {
185 return fileLock.getFileOutputStream(clear);