2 * Created on 15-Sep-2005
4 * TODO To change the template for this generated file go to
5 * Window - Preferences - Java - Code Style - Code Templates
7 package uk.ac.vamsas.client.simpleclient;
9 import java.beans.PropertyChangeListener;
10 import java.beans.PropertyChangeSupport;
12 import java.io.IOException;
13 import java.io.RandomAccessFile;
14 import java.nio.channels.FileChannel;
16 import java.nio.channels.OverlappingFileLockException;
17 import java.util.Hashtable;
18 import java.util.Vector;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
23 import uk.ac.vamsas.client.ClientHandle;
24 import uk.ac.vamsas.client.Events;
25 import uk.ac.vamsas.client.IClient;
26 import uk.ac.vamsas.client.IClientDocument;
27 import uk.ac.vamsas.client.IObjectUpdate;
28 import uk.ac.vamsas.client.InvalidSessionDocumentException;
29 import uk.ac.vamsas.client.InvalidSessionUrnException;
30 import uk.ac.vamsas.client.SessionHandle;
31 import uk.ac.vamsas.client.UserHandle;
32 import uk.ac.vamsas.client.picking.IPickManager;
33 import uk.ac.vamsas.objects.core.Entry;
34 import uk.ac.vamsas.objects.core.VamsasDocument;
35 import uk.ac.vamsas.objects.utils.ProvenanceStuff;
40 public class SimpleClient implements IClient {
42 private static Log log = LogFactory.getLog(SimpleClient.class);
44 protected UserHandle user = null;
46 protected SessionUrn session = null;
47 protected VamsasSession _session;
48 protected ClientHandle client = null;
49 protected EventGeneratorThread evgen = null;
50 protected ClientDocument cdocument = null;
55 private Lock activeClientFilelock = null;
56 private File clientlockFile = null;
59 * object hash table that persists in each client holding vorbaIds and hash values after a document write
61 protected Hashtable extantobjects=null;
63 * construct a transient IdFactory instance - this should last only as long as the
64 * SimpleClient object holds the lock on the vamsas document being created/manipulated.
67 private IdFactory makeVorbaIdFactory() {
68 if (extantobjects==null)
69 extantobjects=new Hashtable();
70 return new IdFactory(getSessionHandle(), client, user, extantobjects);
74 * construct SimpleClient for user, client and VamsasSession directory
75 * use the SimpleClientFactory rather than this constructor directly.
80 protected SimpleClient(UserHandle user, ClientHandle client, VamsasSession sess) throws InvalidSessionUrnException {
81 // TODO: validate user/client/session
86 log.debug("Creating new session for "+_session);
87 session = new SessionUrn(_session);
88 log.debug("Creating new Event Generator");
89 evgen = new EventGeneratorThread(_session, this, handlers);
90 /*} catch (MalformedURLException e) {
91 log.error("Couldn't form a valid SessionUrn object!",e);
92 throw new InvalidSessionUrnException(_session.toString());
94 log.debug("SimpleClient constructed for session "+session.getSessionUrn());
98 * construct new SimpleClientsession by importing objects from an existing vamsas document
102 * @param importingArchive
103 * @throws Exception IOExceptions for Session IO problems, and general Exception if importing document is invalid.
104 protected SimpleClient(UserHandle user, ClientHandle client, VamsasSession sess, File importingArchive) throws Exception {
105 this(user, client, sess);
106 if (log.isDebugEnabled())
108 log.debug("Attempting to overwrite session document with file: "+importingArchive);
110 // TODO: write provenance entry for new session indicating the import.
117 * LATER: check that build substitution variables are correct
118 * @see uk.ac.vamsas.client.IClient#getAbout()
120 public String getAbout() {
121 return new String("VORBA SimpleClient version $version$ build $build$");
127 * @see uk.ac.vamsas.client.IClient#getSessionUrn()
129 public String getSessionUrn() {
130 return session.getSessionUrn();
136 * @see uk.ac.vamsas.client.IClient#getSessionHandle()
138 public SessionHandle getSessionHandle() {
139 // TODO: eliminate SessionHandle ? need to refactor interfaces.
140 SessionHandle sh = new SessionHandle(session.getSessionUrn());
147 * @see uk.ac.vamsas.client.IClient#getClientHandle()
149 public ClientHandle getClientHandle() {
156 * @see uk.ac.vamsas.client.IClient#getUserHandle()
158 public UserHandle getUserHandle() {
163 * @return user field for a provenance entry
165 protected String getProvenanceUser() {
166 return new String(user.getFullName());
169 * construct a provenance entry for this client with the specified action string.
171 * @return properly completed provenance entry
173 protected Entry getProvenanceEntry(String action) {
174 Entry prov = ProvenanceStuff.newProvenanceEntry(client.getClientUrn(), getProvenanceUser(), action);
177 private Hashtable handlers = initHandlers();
179 private Vector listeners = new Vector();
182 * make all the PropertyChangeSupport objects for the
183 * events described in uk.ac.vamsas.client.Event
186 private static Hashtable initHandlers() {
187 Hashtable events = new Hashtable();
188 java.util.Iterator evt = Events.EventList.iterator();
189 while (evt.hasNext()) {
190 Object ths = evt.next();
191 events.put(ths, (Object) new PropertyChangeSupport(ths));
199 * @see uk.ac.vamsas.client.IClient#addDocumentUpdateHandler(java.util.EventListener)
201 public void addDocumentUpdateHandler(PropertyChangeListener evt) {
202 this.addVorbaEventHandler(Events.DOCUMENT_UPDATE, evt);
204 boolean finalized=false;
205 private void haltPickmanager() {
206 if (pickmanager!=null) {
207 final SimpleClient dying=this;
210 SimpleClient.log.debug("Stopping pickManager..");
211 dying.pickmanager.shutdown();
212 SimpleClient.log.debug("pickManager halted.");
220 * @see uk.ac.vamsas.client.IClient#finalizeClient()
222 public void finalizeClient() {
224 throw new Error("VAMSAS Client Implementation Error: Finalized called twice for same client instance.");
226 // mark this instance as finalized
229 // TODO: determine if this is last client in session
231 // TODO: raise events like : ((lst_client && document.request.to.close), (client_finalization), (
232 evgen._raise(Events.CLIENT_FINALIZATION, null, this,null);
233 // if (handlers.containsKey(Events.))
234 // if (handlers.containsKey(Events.CLIENT_FINALIZATION))
235 // deregister listeners.
236 log.debug("Stopping pickManager");
239 log.debug("Deregistering Client");
240 _session.removeClient(this);
241 //log.debug("Stopping EventGenerator..");
242 //evgen.stopWatching();
243 this.cdocument = null;
244 SimpleClient.log.debug("EventGenerator halted.");
245 log.debug("finalization Complete.");
251 * @see uk.ac.vamsas.client.IClient#getClientDocument()
253 public IClientDocument getClientDocument() throws IOException {
254 log.debug("getClientDocument");
255 if (cdocument!=null) {
256 // cdocument is non-nill if the ClientDocument.finalise() method hasn't been called.
259 evgen.disableDocumentWatch();
260 VamsasArchive va = null;
262 // LATER: bail out if it takes too long to get the lock ?
263 va = _session.getVamsasDocument();
265 catch (IOException e) {
266 throw new IOException("Failed to get lock on session document");
268 VamsasDocument doc=null;
269 IdFactory vorba = null;
270 // TODO: LATER: reduce size of vorba ids generated from these parameters to IdFactory (mainly sessionHandle rationalization ?)
272 va.setVorba(vorba=makeVorbaIdFactory());
273 // if session currently holds data - read it in - or get a dummy
274 log.debug("Accessing document");
276 va.getVamsasDocument(getProvenanceUser(),
277 "created new session document.", null);
279 log.debug("Successfully retrieved document.");
281 log.error("Unexpectedly retrieved null document!");
283 catch (Exception e) {
284 log.error("Failed to get session document for session directory '"+_session.sessionDir+"'", e);
285 throw new IOException("Failed to get session document for session directory '"+_session.sessionDir+"'");
287 // Construct the IClientDocument instance
289 cdocument = new ClientDocument(doc, va, vorba, this);
295 * @throws Errors for invalid newdoc parameter
296 * @see uk.ac.vamsas.client.IClient#updateDocument(uk.ac.vamsas.client.IClientDocument)
298 public void updateDocument(IClientDocument newdoc) {
299 log.debug("updateDocument:");
300 // Check validity of simpleclient instance and that it holds a lock on the session's document
301 if (!(newdoc instanceof ClientDocument)) {
302 throw new Error("Invalid IClientDocument passsed to SimpleClient.");
305 throw new Error("Client Error - updateDocument() called before getClientDocument() on this SimpleClient instance.");
306 if (newdoc!=cdocument)
307 throw new Error("Client Error - SimpleClient.updateDocument() can only take the IClientDocument instance returned from SimpleClient.getClientDocument()");
309 if (evgen.isDocumentWatchEnabled())
310 throw new Error("Probable Client Error (did you remember to call SimpleClient.updateDocument(clientdoc) at the end of the document update handler?) - or Library Bug : Document watcher still enabled whilst ClientDocument instance exists.");
312 if (!cdocument.isModified()) {
313 // client document is silently got rid of, with no session update events.
314 if (log.isDebugEnabled())
315 log.debug("updateDocument for "+session.getSessionUrn()+" with unmodified IClientDocument (skipping the write)");
317 writeSessionDocument();
319 tidyAwaySessionDocumentState();
322 * garbage collect the ClientDocument instance and re-enable watchers.
324 protected void tidyAwaySessionDocumentState() {
326 log.debug("Finalizing ClientDocument instance.");
327 cdocument.finalize();
328 } catch (Throwable e) {
329 log.error("Exception when trying to garbage collect ClientDocument for "+session.getSessionUrn(), e);
331 cdocument = null; // this is probably done by finalize
334 _session.unlockVamsasDocument();
335 evgen.enableDocumentWatch();
336 } catch (IOException e) {
337 log.warn("IO Problems when releasing lock on session document!",e);
338 _session.slog.error("IO problems when attempting to release lock on session document.");
343 * write the cdocument instance to the session for real.
345 private void writeSessionDocument() {
347 boolean updated=cdocument.updateSessionDocument();
349 log.warn("Session document did not update properly for session directory "+_session.sessionDir);
350 // cdocument.archive.cancelArchive(); // LATER: could be done - would need to prevent updateSessionDocument closing the iohandler.
351 _session.slog.warn("Session Document updated but may not be valid (false return from org.vamsas.simpleclient.ClientDocument.updateSessionDocument()");
353 log.debug("Document update successful.");
355 _session.setUnsavedFlag();
357 catch (IOException e) {
358 log.warn("IO Problems when updating document!",e);
359 _session.slog.error("IO problems when attempting to update document.");
366 * @see uk.ac.vamsas.client.IClient#storeDocument(java.io.File)
368 public void storeDocument(File location) {
370 throw new Error("Vamsas Client API Usage Error: storeDocument called with null location.");
371 log.debug("StoreDocument to "+location);
372 // write storeDocument file to inform other clients that they should raise
373 Lock vamlock = evgen.want_to_store();
374 // Events.DOCUMENT_FINALIZEAPPDATA
376 _session.writeVamsasDocument(location, vamlock);
377 _session.clearUnsavedFlag();
378 } catch (Exception e) {
379 log.warn("Exception whilst trying to store document in "+location,e);
387 * @see uk.ac.vamsas.client.IClient#addVorbaEventHandler(java.lang.String,
388 * java.beans.PropertyChangeListener)
390 public void addVorbaEventHandler(String EventChain, PropertyChangeListener evt) {
391 if (handlers.containsKey(EventChain)) {
392 log.debug("Adding new handler for "+EventChain);
394 ((PropertyChangeSupport) (handler = handlers.get(EventChain)))
395 .addPropertyChangeListener(evt);
396 listeners.add(handler);
397 listeners.add((Object) evt);
402 * @see uk.ac.vamsas.client.IClient#pollUpdate()
404 public void pollUpdate() {
405 log.debug("pollUpdate");
407 log.warn("pollUpdate called on incomplete SimpleClient object.");
411 if (!evgen.isWatcherAlive()) {
412 log.warn("pollUpdate called before joinSession() - trying to do this.");
415 } catch (Exception e) {
416 log.error("Unexpected exception on default call to joinSession",e);
420 //TODO ensure event generator robustly handles these interrupts.
421 log.debug("interrrupting event generator.");
422 evgen.interruptWatching();
423 log.debug("interrrupted event generator.");
427 * @see uk.ac.vamsas.client.IClient#joinSession()
429 public void joinSession() throws Exception {
430 log.debug("Joining Session.");
431 // start the EventGenerator thread.
433 log.warn("joinSession called on incomplete SimpleClient object.");
436 if (evgen.isWatcherAlive())
437 throw new Error("Join session called twice for the same SimpleClient (IClient instance).");
438 evgen.startWatching();
439 if (evgen.isWatcherAlive())
440 log.debug("Started EventGenerator thread.");
442 log.warn("Failed to start EventGenerator thread.");
443 throw new Exception("Failed to start event generator thread - client cannot be instantiated.");
445 if (evgen.countHandlersFor(Events.DOCUMENT_CREATE)>0) {
446 //TODO: LATER: is this application connecting to a newly created session document ?
447 //evgen.raise(Events.DOCUMENT_CREATE);
455 * @see uk.ac.vamsas.client.IClient#importDocument(java.io.File)
457 public void importDocument(File location) {
458 // TODO LATER: implement SimpleClient.importDocument()
459 // if (numberOfClients<1 or document file is empty/new, then can import directly.)
460 // more complex if data is already present in document. Could have a 'clearSession' method, too, or dump and overwrite instead.
461 log.error("importDocument is not yet implemented for a SimpleClient Session.");
464 this._session.setVamsasDocument(location);
465 } catch (IOException e) {
466 log.error("importDocument failed.");
470 public IObjectUpdate getUpdateHandler(Class rootObject) {
471 // TODO Auto-generated method stub
475 public IObjectUpdate[] getUpdateHandlers() {
476 // TODO Auto-generated method stub
480 public void removeUpdateHandler(Class rootObject) {
481 // TODO Auto-generated method stub
485 public void setUpdateHandler(IObjectUpdate handler) {
486 // TODO Auto-generated method stub
491 * retrieves the current VamsasSession to which belong the client
492 * @return the _session
494 protected VamsasSession get_session() {
495 return this._session;
497 SimplePickManager pickmanager=null;
499 * @see uk.ac.vamsas.client.IClient#getPickManager()
501 public IPickManager getPickManager() {
506 private void createPickManager() {
507 if (pickmanager==null){
508 // TODO: Construct PickManager for session using details from sessionURN!
509 log.debug("Creating PickManager (not from sessionURN yet)");
510 pickmanager = new SimplePickManager(new uk.ac.vamsas.client.picking.SocketManager());
515 protected void releaseActiveClientFile() throws IOException
518 log.debug("Releasing active client locks");
519 if( activeClientFilelock != null)
521 log.debug("Releasing lock on active client lock file");
522 activeClientFilelock.release();
523 log.debug("ReleaseActiveClientFile called when client has no lock on its clientLockFile");
524 activeClientFilelock = null;
526 log.debug("ReleaseActiveClientFile called when client has no lock on its clientLockFile");
528 if (this.clientlockFile != null)
530 log.debug("trying to delete active client lock file");
531 if (this.clientlockFile.exists())
533 this.clientlockFile.delete();
534 log.debug("deleted active client lock file");
537 log.debug("ReleaseActiveClientFile called when client has no clientLockFile");
541 protected void createActiveClientFile() throws IOException
543 if(this.clientlockFile != null )return;
544 log.debug("createActiveClientFile");
545 //create, if need, subdirectory to contain client files
546 File clientlockFileDir = new File ( this.get_session().sessionDir, this.get_session().clientFileDirectory);
547 if( !clientlockFileDir.exists())
548 {//the directory does not exist, create it
549 if (! clientlockFileDir.mkdirs())
551 throw new IOException("Failed to create sub directory to session directory for client lock files'"+clientlockFileDir.getAbsolutePath()+"'");
556 if (!(clientlockFileDir.isDirectory() && clientlockFileDir.canWrite()))
558 throw new IOException("Directory for client lock files is not a directory or is not accessibl: '"+clientlockFileDir.getAbsolutePath()+"'");
561 this.clientlockFile = new File (clientlockFileDir, this.getClientHandle().getClientUrn().replaceAll("[:;/\\\\]+",""));
563 log.debug("Creating active client lock file "+ this.clientlockFile.getAbsolutePath());
564 Lock clientLock = uk.ac.vamsas.client.simpleclient.LockFactory.getLock(clientlockFile, false);
565 if (clientLock==null || !clientLock.isLocked())
567 log.fatal("IMPLEMENTATION ERROR: Couldn't get a lock for the client lock file "+clientlockFile);
569 activeClientFilelock = clientLock;
572 * @return the clientlockFile
574 protected File getClientlockFile() {
575 return clientlockFile;
579 * @return the lock for the client in the session
581 protected Lock getClientLock() {
582 return activeClientFilelock;