refactored common locked IO methods from ClientsFile to SessionFile
[vamsas.git] / src / org / vamsas / client / simpleclient / SessionFile.java
1 package org.vamsas.client.simpleclient;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7
8 /**
9  * Basic methods for classes handling locked IO on files 
10  * monitored by all (simpleclient) clients in a vamsas session.
11  * @author jimp
12  *
13  */
14 public class SessionFile {
15
16   protected File sessionFile;
17   protected Lock fileLock = null;
18
19   protected boolean lockFile(Lock extantlock) {
20     if (fileLock!=null && !fileLock.isLocked()) {
21       fileLock.release();// tidy up invalid lock
22     }
23     fileLock=extantlock;
24     return lockFile();
25   }
26
27   /**
28    * Get a lock for the SessionFile
29    * 
30    * @return true if lock was made
31    */
32   protected boolean lockFile() {
33     if (fileLock != null)
34       if (fileLock.isLocked())
35         return true;
36       else 
37         // lock failed for some reason.
38         fileLock.release();
39     fileLock = null;
40     if (sessionFile != null) {
41       if (sessionFile.exists()) {
42         // TODO: see if we need to loop-wait for locks or they just block until
43         // lock is made...
44         do {
45           if (fileLock!=null)
46             fileLock.release();
47           fileLock = new Lock(sessionFile); // TODO: wait around if we can't get the lock.
48         } while (!fileLock.isLocked());
49         // fileLock = new Lock(sessionFile);
50         return fileLock.isLocked();
51       }
52     } else
53       throw new Error(
54           "org.vamsas.client.simpleclient.ClientsFile.lockList called for non-initialised ClientsFile!");
55   
56     // no lock possible
57     return false;
58   }
59
60   /**
61    * Explicitly release the SessionFile's lock.
62    * 
63    * @return true if lock was released.
64    */
65   protected void unlockFile() {
66     if (fileLock != null) {
67       fileLock.release();    
68       fileLock = null;
69     }
70   }
71
72   /**
73    * Makes a backup of the sessionFile.
74    * @return Backed up SessionFile or null if failed to make backup.
75    */
76   protected File backupSessionFile() {
77     return backupSessionFile(null, sessionFile.getName(),".old", sessionFile.getParentFile());
78   }
79
80   protected File backupSessionFile(Lock extantLock, String backupPrefix, String backupSuffix, File backupDir) {
81     File tempfile=null;
82     if (lockFile(extantLock)) {
83       try {
84         tempfile = File.createTempFile(backupPrefix, backupSuffix, backupDir);
85         FileOutputStream tos = new FileOutputStream(tempfile);
86         tos.getChannel().transferFrom(fileLock.rafile.getChannel(), 0,
87             fileLock.rafile.length());
88         tos.close();
89       } catch (FileNotFoundException e1) {
90         System.err.println("Can't create temp file for "+sessionFile.getName());
91         e1.printStackTrace(System.err);
92         tempfile=null;
93       } catch (IOException e1) {
94         System.err
95             .println("Error when copying content to temp file for "+sessionFile.getName());
96         e1.printStackTrace(System.err);
97         tempfile=null;
98       }
99     }
100     return tempfile;
101   }
102
103 }