* @author jim
*/
public class ClientsFile extends ListFile {
+ private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(ClientsFile.class);
/**
* number of my client in list - passed back when a client
* is added to list, and used (if valid) for quickly
if (lockFile()) {
try {
ClientHandle[] clients=null;
- if (this.fileLock.rafile.length()>0) {
- ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(
- new java.io.FileInputStream(sessionFile)));
+ if (fileLock.length()>0) {
+
+ ObjectInputStream is = new ObjectInputStream(fileLock.getBufferedInputStream(true));
Object o;
o=is.readObject();
if (o!=null) {
*/
protected int addClient(ClientHandle me, boolean disambiguate) {
int newclient = 0;
+ int tries=5;
+ while (tries-->0 && !lockFile())
+ try { Thread.sleep(1); } catch (Exception e){};
if (lockFile()) {
ClientHandle[] clients = retrieveClientHandles();
if (me.getClientUrn()==null) {
}
return newclient;
}
-
+ /**
+ * when set true - get FileNotFoundExceptions on WinXP when writing to locked stream after the backup has been made (via the backupFile method)
+ */
+ boolean backup=false;
/**
* safely writes clients array to the file referred to by sessionFile.
*
*/
protected boolean putClientList(ClientHandle[] clients) {
if (lockFile()) {
- File templist = backupSessionFile();
- if (templist != null) {
- try {
- fileLock.rafile.setLength(0);
- ObjectOutputStream os = new ObjectOutputStream(
- new BufferedOutputStream(new FileOutputStream(this.sessionFile)));
- os.writeObject(clients);
- os.close();
+ File templist=null;
+ if (!backup || (templist = backupSessionFile()) != null) {
+ int retries=3;
+ while (retries-->0) {
+ try {
+ ObjectOutputStream os =
+ new ObjectOutputStream(fileLock.getBufferedOutputStream(true));
+ log.debug("About to write "+clients.length+" clientHandles to output stream.");
+ os.writeObject(clients);
+ os.close();
// All done - remove the backup.
- templist.delete();
+ if (backup)
+ templist.delete();
templist = null;
- } catch (Exception e) {
- if (templist != null) {
+ retries=-1;
+ } catch (Exception e) {
System.err
- .println("Serious - problems writing to sessionFile. Backup in "
- + templist.getAbsolutePath());
+ .println("Serious - problems writing to sessionFile.");
+ if (retries>0 && templist != null) {
+ System.err.println("Recovering from Backup in "
+ + templist.getAbsolutePath());
+ templist.renameTo(fileLock.target);
+ }
e.printStackTrace(System.err);
}
}
+ if (retries>-2) {
+ System.err
+ .println("Serious - problems writing to sessionFile. Giving Up.");
+ return false;
+ }
} else {
throw new Error(
"Couldn't create backup of the clientList before writing to it!");
// successful!
return true;
}
+
+ public void clearList() {
+ if (lockFile()) {
+ try {
+ FileOutputStream fout = fileLock.getFileOutputStream(true);
+ fout.flush();
+ fout.close();
+ } catch (Exception e) {
+ throw new Error("Problems trying to clear clientlist!",e);
+
+ }
+ }
+
+ }
}