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.EventHandler;
10 import java.beans.PropertyChangeEvent;
11 import java.beans.PropertyChangeListener;
12 import java.beans.PropertyChangeSupport;
13 import java.io.BufferedReader;
15 import java.io.IOException;
16 import java.net.MalformedURLException;
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.InvalidSessionUrnException;
29 import uk.ac.vamsas.client.SessionHandle;
30 import uk.ac.vamsas.client.UserHandle;
31 import uk.ac.vamsas.client.picking.IPickManager;
32 import uk.ac.vamsas.objects.core.ApplicationData;
33 import uk.ac.vamsas.objects.core.Entry;
34 import uk.ac.vamsas.objects.core.LockFile;
35 import uk.ac.vamsas.objects.core.VamsasDocument;
36 import uk.ac.vamsas.objects.utils.AppDataReference;
37 import uk.ac.vamsas.objects.utils.ProvenanceStuff;
38 import uk.ac.vamsas.objects.utils.document.VersionEntries;
43 public class SimpleClient implements IClient {
45 private static Log log = LogFactory.getLog(SimpleClient.class);
47 protected UserHandle user = null;
49 protected SessionUrn session = null;
50 protected VamsasSession _session;
51 protected ClientHandle client = null;
52 protected EventGeneratorThread evgen = null;
53 protected ClientDocument cdocument = null;
55 * object hash table that persists in each client holding vorbaIds and hash values after a document write
57 protected Hashtable extantobjects=null;
59 * construct a transient IdFactory instance - this should last only as long as the
60 * SimpleClient object holds the lock on the vamsas document being created/manipulated.
63 private IdFactory makeVorbaIdFactory() {
64 return new IdFactory(getSessionHandle(), client, user);
68 * construct SimpleClient for user, client and VamsasSession directory
69 * use the SimpleClientFactory rather than this constructor directly.
74 protected SimpleClient(UserHandle user, ClientHandle client, VamsasSession sess) throws InvalidSessionUrnException {
75 // TODO: validate user/client/session
80 session = new SessionUrn(_session);
81 } catch (MalformedURLException e) {
82 log.error("Couldn't form a valid SessionUrn object!",e);
83 throw new InvalidSessionUrnException(_session.toString());
87 * construct new session by importing objects from an existing vamsas document
91 * @param importingArchive
92 * @throws Exception IOExceptions for Session IO problems, and general Exception if importing document is invalid.
94 protected SimpleClient(UserHandle user, ClientHandle client, VamsasSession sess, File importingArchive) throws Exception {
95 this(user, client, sess);
96 VamsasArchive sessdoc = _session.getVamsasDocument();
98 VamsasArchiveReader odoc = new VamsasArchiveReader(importingArchive);
99 SimpleDocument sdoc = new SimpleDocument(makeVorbaIdFactory());
100 VamsasDocument doc = sdoc.getVamsasDocument(odoc);
101 sessdoc.putVamsasDocument(doc, sdoc.vorba);
102 sessdoc.closeArchive();
103 } catch (Exception e) {
104 sessdoc.cancelArchive();
105 // write a dummy archive
106 _session.slog.info("Exception when importing document data from "+importingArchive);
107 throw new Exception("Failed to import data from "+importingArchive, e);
113 * LATER: check that build substitution variables are correct
114 * @see uk.ac.vamsas.client.IClient#getAbout()
116 public String getAbout() {
117 return new String("VORBA SimpleClient version $version$ build $build$");
123 * @see uk.ac.vamsas.client.IClient#getSessionUrn()
125 public String getSessionUrn() {
126 return session.getSessionUrn();
132 * @see uk.ac.vamsas.client.IClient#getSessionHandle()
134 public SessionHandle getSessionHandle() {
135 // TODO: eliminate SessionHandle ? need to refactor interfaces.
136 SessionHandle sh = new SessionHandle(session.getSessionUrn());
143 * @see uk.ac.vamsas.client.IClient#getClientHandle()
145 public ClientHandle getClientHandle() {
152 * @see uk.ac.vamsas.client.IClient#getUserHandle()
154 public UserHandle getUserHandle() {
159 * @return user field for a provenance entry
161 protected String getProvenanceUser() {
162 return new String(user.getFullName());
165 * construct a provenance entry for this client with the specified action string.
167 * @return properly completed provenance entry
169 protected Entry getProvenanceEntry(String action) {
170 Entry prov = ProvenanceStuff.newProvenanceEntry(client.getClientUrn(), getProvenanceUser(), action);
173 private Hashtable handlers = initHandlers();
175 private Vector listeners = new Vector();
178 * make all the PropertyChangeSupport objects for the
179 * events described in uk.ac.vamsas.client.Event
182 private static Hashtable initHandlers() {
183 Hashtable events = new Hashtable();
184 java.util.Iterator evt = Events.EventList.iterator();
185 while (evt.hasNext()) {
186 Object ths = evt.next();
187 events.put(ths, (Object) new PropertyChangeSupport(ths));
195 * @see uk.ac.vamsas.client.IClient#addDocumentUpdateHandler(java.util.EventListener)
197 public void addDocumentUpdateHandler(PropertyChangeListener evt) {
198 if (handlers.containsKey(Events.DOCUMENT_UPDATE)) {
200 ((PropertyChangeSupport) (handler = handlers.get(Events.DOCUMENT_UPDATE)))
201 .addPropertyChangeListener(evt);
202 listeners.add(handler);
203 listeners.add((Object) evt);
206 boolean finalized=false;
210 * @see uk.ac.vamsas.client.IClient#finalizeClient()
212 public void finalizeClient() {
213 // TODO: determine if this is last client in session
214 // TODO: raise events like : ((lst_client && document.request.to.close), (client_finalization), (
216 // if (handlers.containsKey(Events.))
217 // if (handlers.containsKey(Events.CLIENT_FINALIZATION))
218 // deregister listeners.
219 // mark this instance as finalized
220 this._session.removeClient(this);
226 * @see uk.ac.vamsas.client.IClient#getClientDocument()
228 public IClientDocument getClientDocument() throws IOException {
229 if (cdocument!=null) {
230 // cdocument is non-nill if the ClientDocument.finalise() method hasn't been called.
233 VamsasArchive va = null;
235 // LATER: bail out if it takes too long to get the lock ?
236 va = _session.getVamsasDocument();
238 catch (IOException e) {
239 throw new IOException("Failed to get lock on session document");
241 VamsasDocument doc=null;
242 IdFactory vorba = null;
243 // TODO: reduce size of vorba ids generated from these parameters to IdFactory (mainly sessionHandle rationalization ?)
245 va.setVorba(vorba=makeVorbaIdFactory());
246 // if session currently holds data - read it in - or get a dummy
247 _session.slog.debug("Accessing document");
249 va.getVamsasDocument(getProvenanceUser(),
250 "created new session document.", null);
252 _session.slog.debug("Successfully retrieved document.");
254 log.error("Unexpectedly retrieved null document!");
256 catch (Exception e) {
257 log.error("Failed to get session document for session directory '"+_session.sessionDir+"'", e);
258 throw new IOException("Failed to get session document for session directory '"+_session.sessionDir+"'");
260 // Construct the IClientDocument instance
262 ClientDocument cdoc = new ClientDocument(doc, va, vorba, this);
268 * @throws Errors for invalid newdoc parameter
269 * @see uk.ac.vamsas.client.IClient#updateDocument(uk.ac.vamsas.client.IClientDocument)
271 public void updateDocument(IClientDocument newdoc) {
272 if (!(newdoc instanceof ClientDocument)) {
273 throw new Error("Invalid IClientDocument instance for SimpleClient.");
276 throw new Error("Client Error - updateDocument() called before getClientDocument().");
277 if (newdoc!=cdocument)
278 throw new Error("Client Error - SimpleClient.updateDocument() can only take the IClientDocument instance returned from SimpleClient.getClientDocument()");
279 if (!cdocument.isModified()) {
280 if (log.isDebugEnabled())
281 log.debug("updateDocument for "+session.getSessionUrn()+" with unmodified IClientDocument.");
284 if (!cdocument.updateSessionDocument()) {
285 log.warn("Session document did not update properly for session directory "+_session.sessionDir);
286 // cdocument.archive.cancelArchive(); // LATER: could be done - would need to prevent updateSessionDocument closing the archive.
287 _session.slog.warn("Session Document updated but may not be valid (false return from org.vamsas.simpleclient.ClientDocument.updateSessionDocument()");
290 catch (IOException e) {
291 log.warn("IO Problems when updating document!",e);
292 _session.slog.error("IO problems when attempting to update document.");
295 // garbage collect the ClientDocument instance.
297 cdocument.finalize();
299 } catch (Throwable e) {
300 log.error("Exception when trying to garbage collect ClientDocument for "+session.getSessionUrn(), e);
302 cdocument = null; // this is probably done by finalize
308 * @see uk.ac.vamsas.client.IClient#storeDocument(java.io.File)
310 public void storeDocument(File location) {
312 // write storeDocument file to inform other clients that they should raise
313 Lock vamlock = evgen.want_to_store();
314 // Events.DOCUMENT_FINALIZEAPPDATA
316 _session.writeVamsasDocument(location, vamlock);
317 _session.clearUnsavedFlag();
318 } catch (Exception e) {
319 log.warn("Exception whilst trying to store document in "+location,e);
328 * @see uk.ac.vamsas.client.IClient#addVorbaEventHandler(java.lang.String,
329 * java.beans.PropertyChangeListener)
331 public void addVorbaEventHandler(String EventChain, PropertyChangeListener evt) {
332 if (handlers.containsKey(EventChain)) {
334 ((PropertyChangeSupport) (handler = handlers.get(EventChain)))
335 .addPropertyChangeListener(evt);
336 listeners.add(handler);
337 listeners.add((Object) evt);
342 * @see uk.ac.vamsas.client.IClient#pollUpdate()
344 public void pollUpdate() {
347 log.warn("pollUpdate called on incomplete SimpleClient object.");
351 if (!evgen.isAlive()) {
352 log.warn("pollUpdate called before joinSession() - trying to do this.");
355 } catch (Exception e) {
356 log.error("Unexpected exception on default call to joinSession",e);
360 //TODO ensure event generator robustly handles these interrupts.
361 log.debug("interrrupting event generator.");
363 log.debug("interrrupted event generator.");
367 * @see uk.ac.vamsas.client.IClient#joinSession()
369 public void joinSession() throws Exception {
370 // start the EventGenerator thread.
372 log.warn("joinSession called on incomplete SimpleClient object.");
376 throw new Error("Join session called twice for the same SimpleClient (IClient instance).");
379 log.debug("Started EventGenerator thread.");
381 log.warn("Failed to start EventGenerator thread.");
382 throw new Exception("Failed to start event generator thread - client cannot be instantiated.");
384 if (evgen.countHandlersFor(Events.DOCUMENT_CREATE)>0) {
385 //TODO: is this application connecting to a newly created session document ?
386 //evgen.raise(Events.DOCUMENT_CREATE);
393 * @see uk.ac.vamsas.client.IClient#importDocument(java.io.File)
395 public void importDocument(File location) {
396 // TODO LATER: implement SimpleClient.importDocument()
397 log.error("importDocument is not yet implemented for a SimpleClient Session.");
400 public IObjectUpdate getUpdateHandler(Class rootObject) {
401 // TODO Auto-generated method stub
405 public IObjectUpdate[] getUpdateHandlers() {
406 // TODO Auto-generated method stub
410 public void removeUpdateHandler(Class rootObject) {
411 // TODO Auto-generated method stub
415 public void setUpdateHandler(IObjectUpdate handler) {
416 // TODO Auto-generated method stub
421 * retrieves the current VamsasSession to which belong the client
422 * @return the _session
424 protected VamsasSession get_session() {
425 return this._session;
429 * @see uk.ac.vamsas.client.IClient#getPickManager()
431 public IPickManager getPickManager() {
432 // TODO Auto-generated method stub