b43b1a99d2842ea480713559a16902f33e657f52
[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 SessionFile(File file) {
20     super();
21     sessionFile = file;
22   }
23
24   protected boolean lockFile(Lock extantlock) {
25     if (fileLock!=null && !fileLock.isLocked()) {
26       fileLock.release();// tidy up invalid lock
27     }
28     fileLock=extantlock;
29     return lockFile();
30   }
31
32   /**
33    * Get a lock for the SessionFile
34    * 
35    * @return true if lock was made
36    */
37   protected boolean lockFile() {
38     if (fileLock != null)
39       if (fileLock.isLocked())
40         return true;
41       else 
42         // lock failed for some reason.
43         fileLock.release();
44     fileLock = null;
45     if (sessionFile != null) {
46       if (sessionFile.exists()) {
47         // TODO: see if we need to loop-wait for locks or they just block until
48         // lock is made...
49         do {
50           if (fileLock!=null)
51             fileLock.release();
52           fileLock = new Lock(sessionFile); // TODO: wait around if we can't get the lock.
53         } while (!fileLock.isLocked());
54         // fileLock = new Lock(sessionFile);
55         return fileLock.isLocked();
56       }
57     } else
58       throw new Error(
59           "org.vamsas.client.simpleclient.SessionFile.lockFile called for non-initialised SessionFile!");
60   
61     // no lock possible
62     return false;
63   }
64
65   /**
66    * Explicitly release the SessionFile's lock.
67    * 
68    * @return true if lock was released.
69    */
70   protected void unlockFile() {
71     if (fileLock != null) {
72       fileLock.release();    
73       fileLock = null;
74     }
75   }
76
77   /**
78    * Makes a backup of the sessionFile.
79    * @return Backed up SessionFile or null if failed to make backup.
80    */
81   protected File backupSessionFile() {
82     return backupSessionFile(null, sessionFile.getName(),".old", sessionFile.getParentFile());
83   }
84
85   protected File backupSessionFile(Lock extantLock, String backupPrefix, String backupSuffix, File backupDir) {
86     File tempfile=null;
87     if (lockFile(extantLock)) {
88       try {
89         tempfile = File.createTempFile(backupPrefix, backupSuffix, backupDir);
90         FileOutputStream tos = new FileOutputStream(tempfile);
91         tos.getChannel().transferFrom(fileLock.rafile.getChannel(), 0,
92             fileLock.rafile.length());
93         tos.close();
94       } catch (FileNotFoundException e1) {
95         System.err.println("Can't create temp file for "+sessionFile.getName());
96         e1.printStackTrace(System.err);
97         tempfile=null;
98       } catch (IOException e1) {
99         System.err
100             .println("Error when copying content to temp file for "+sessionFile.getName());
101         e1.printStackTrace(System.err);
102         tempfile=null;
103       }
104     }
105     return tempfile;
106   }
107
108 }