1 package org.vamsas.client.simpleclient;
3 import org.vamsas.client.*;
5 import java.io.BufferedInputStream;
6 import java.io.BufferedOutputStream;
8 import java.io.FileInputStream;
9 import java.io.FileNotFoundException;
10 import java.io.FileOutputStream;
11 import java.io.IOException;
12 import java.io.InputStreamReader;
13 import java.io.ObjectInputStream;
14 import java.io.ObjectOutput;
15 import java.io.ObjectOutputStream;
16 import java.io.OutputStream;
17 import java.util.Vector;
20 * @author jim Handler for the clientsFile within a vamsas session thread.
22 public class ClientsFile {
23 private File filelist;
26 * number of my client in list (not known at start but used when known to make
29 private int syncnum = 1;
31 public ClientsFile(File filelist) throws IOException {
32 this.filelist = filelist;
33 if (!this.filelist.exists())
34 this.filelist.createNewFile();
37 private Lock listlock = null;
39 protected boolean lockList(Lock extantlock) {
40 if (listlock!=null && !listlock.isLocked()) {
41 listlock.release();// tidy up invalid lock
47 * Get a lock for the ClientsFile
49 * @return true if lock was made
51 protected boolean lockList() {
53 if (listlock.isLocked())
56 // lock failed for some reason.
59 if (filelist != null) {
60 if (filelist.exists()) {
61 // TODO: see if we need to loop-wait for locks or they just block until
66 listlock = new Lock(filelist); // TODO: wait around if we can't get the lock.
67 } while (!listlock.isLocked());
68 // listlock = new Lock(filelist);
69 return listlock.isLocked();
73 "org.vamsas.client.simpleclient.ClientsFile.lockList called for non-initialised ClientsFile!");
80 * Explicitly release the ClientsFile lock.
82 * @return true if lock was released.
84 protected void unlockList() {
85 if (listlock != null) {
92 * internal method for getting clientList - ensures a lock has been made but
93 * does not release it.
95 * @return list of clients
97 private ClientHandle[] retrieveClientHandles() {
100 ClientHandle[] clients=null;
101 if (this.listlock.rafile.length()>0) {
102 ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(
103 new java.io.FileInputStream(filelist)));
108 clients = (ClientHandle[]) o;
110 catch (Exception e) {
111 System.err.println("Garbage in the clientHandle list "+this.filelist);
116 } catch (FileNotFoundException e) {
117 // TODO Auto-generated catch block
118 e.printStackTrace(System.err);
119 } catch (Exception e) {
120 e.printStackTrace(System.err);
126 * get the clientList from the file. May return null if lock failed!
129 public ClientHandle[] retrieveClientList() {
131 ClientHandle[] clients = retrieveClientHandles();
138 * get list from the locked ClientList.
140 * @return clientList or null if lock failed (or file was empty)
142 public ClientHandle[] retrieveClientList(Lock extantlock) {
143 if (lockList(extantlock)) {
144 ClientHandle[] clients = retrieveClientHandles();
151 * adds clientHandle me to the clientList under an existing lock extantLock.
154 * @return client index in list or 0 if lock was invalid or addClient operation failed.
156 public int addClient(ClientHandle me, Lock extantLock) {
157 return addClient(me, true, extantLock);
161 * adds clientHandle me to the clientList under an existing lock.
162 * @param me - clientHandle
163 * @param disambig - if true then add will fail if an identical clientHandle already exists
164 * @param extantLock - existing lock
165 * @return client index in list or 0 if addClient (or the lock) failed.
168 public int addClient(ClientHandle me, boolean disambig, Lock extantLock) {
169 if (lockList(extantLock)) {
170 syncnum = addClient(me, disambig);
178 * adds the ClientHandle to the list - if it is not unique, then the
179 * ClientHandle object is modified to make it unique in the list. returns the
180 * clientNumber for the client in the session.
186 public int addClient(ClientHandle me) {
187 syncnum = addClient(me, true);
193 * removes 'me' from the session ClientList without complaint if 'me' isn't in the clientList already.
194 * @param me client handle to be removed
195 * @param clientlock existing lock passed from watcher.
197 public void removeClient(ClientHandle me, Lock clientlock) {
199 if (lockList(clientlock)) {
200 ClientHandle[] clients = retrieveClientHandles();
201 if (clients != null) {
202 if ((syncnum<=0 || syncnum>clients.length) || clients[syncnum-1]!=me) {
203 for (int i = 0, j = clients.length; i < j; i++)
204 if (clients[i].equals(me)) {
212 ClientHandle[] newlist = new ClientHandle[clients.length - 1];
213 for (int k=0,i = 0, j = clients.length; i < j; i++)
215 newlist[k++] = clients[i];
216 if (!putClientList(newlist))
217 throw new Error("Failed to write new clientList!"); // failed to put the clientList to disk.
222 throw new Error("Couldn't get lock for "+((filelist==null) ? "Unitialised filelist in ClientsFile" : filelist.getAbsolutePath()));
226 * Adds a ClientHandle to the ClientList file - optionally disambiguating
227 * the ClientHandle (modifes the URN).
228 * Note: Caller is left to release the lock on the ClientList.
230 * @param disambiguate -
231 * flag indicating if the URN for me should be disambiguated to
232 * differentiate between sessions.
233 * @return index of clientHandle in new list, or -1-position of existing
234 * clientHandle (if disambiguate is true)
236 protected int addClient(ClientHandle me, boolean disambiguate) {
239 ClientHandle[] clients = retrieveClientHandles();
240 if (me.getClientUrn()==null) {
241 // TODO: move this into ClientUrn as a standard form method.
242 me.setClientUrn("vamsas://"+me.getClientName()+":"+me.getVersion()+"/");
244 if (clients == null) {
245 clients = new ClientHandle[1];
250 for (int i = 0, j = clients.length; i < j; i++) {
251 if (clients[i].equals(me)) {
253 while (clients[i].equals(me)) {
254 me.setClientUrn(me.getClientUrn() + k++); // TODO: make a better
259 // will not write the ambiguous clientHandle to disk, just return
266 ClientHandle[] newlist = new ClientHandle[clients.length + 1];
267 for (i = 0, j = clients.length; i < j; i++)
268 newlist[i] = clients[i];
273 if (!putClientList(clients))
274 return 0; // failed to put the clientList to disk.
280 * safely writes clients array to the file referred to by filelist.
283 * @return true if successful write. Throws Errors otherwise.
285 protected boolean putClientList(ClientHandle[] clients) {
287 File templist = null;
289 templist = File.createTempFile(filelist.getName(),".old", filelist.getParentFile());
290 FileOutputStream tos = new FileOutputStream(templist);
291 tos.getChannel().transferFrom(listlock.rafile.getChannel(), 0,
292 listlock.rafile.length());
294 } catch (FileNotFoundException e1) {
295 System.err.println("Can't create temp file for clientlist");
296 e1.printStackTrace(System.err);
297 } catch (IOException e1) {
299 .println("Error when copying content to temp file for clientlist");
300 e1.printStackTrace(System.err);
302 if (templist != null) {
304 listlock.rafile.setLength(0);
305 ObjectOutputStream os = new ObjectOutputStream(
306 new BufferedOutputStream(new FileOutputStream(this.filelist)));
307 os.writeObject(clients);
309 // All done - remove the backup.
312 } catch (Exception e) {
313 if (templist != null) {
315 .println("Serious - problems writing to filelist. Backup in "
316 + templist.getAbsolutePath());
317 e.printStackTrace(System.err);
322 "Couldn't create backup of the clientList before writing to it!");
325 throw new Error("Could not lock the clientList: "
326 + ((filelist == null) ? "Unitialized ClientsFile"
327 : " failed to get lock on " + filelist.getAbsolutePath()));