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 org.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.util.Hashtable;
17 import java.util.Vector;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.vamsas.client.ClientHandle;
22 import org.vamsas.client.Events;
23 import org.vamsas.client.IClient;
24 import org.vamsas.client.IClientDocument;
25 import org.vamsas.client.SessionHandle;
26 import org.vamsas.client.UserHandle;
27 import org.vamsas.objects.core.LockFile;
28 import org.vamsas.objects.core.VamsasDocument;
29 import org.vamsas.objects.utils.ProvenanceStuff;
30 import org.vamsas.objects.utils.document.VersionEntries;
35 public class SimpleClient implements IClient {
37 private static Log log = LogFactory.getLog(SimpleClient.class);
39 protected UserHandle user = null;
41 protected SessionUrn session = null;
42 protected VamsasSession _session;
43 protected ClientHandle client = null;
44 protected EventGeneratorThread evgen = null;
46 * construct a transient IdFactory instance - this should last only as long as the
47 * SimpleClient object holds the lock on the vamsas document being created/manipulated.
50 private IdFactory makeVorbaIdFactory() {
51 return new IdFactory(getSessionHandle(), client, user);
55 * construct SimpleClient for user, client and VamsasSession directory
56 * use the SimpleClientFactory rather than this constructor directly.
61 protected SimpleClient(UserHandle user, ClientHandle client, VamsasSession sess) {
62 // TODO: validate user/client/session
66 session = new SessionUrn(_session);
69 * construct new session by importing objects from an existing vamsas document
73 * @param importingArchive
74 * @throws Exception IOExceptions for Session IO problems, and general Exception if importing document is invalid.
76 protected SimpleClient(UserHandle user, ClientHandle client, VamsasSession sess, File importingArchive) throws Exception {
77 this(user, client, sess);
78 VamsasArchive sessdoc = _session.getVamsasDocument();
80 VamsasArchiveReader odoc = new VamsasArchiveReader(importingArchive);
81 SimpleDocument sdoc = new SimpleDocument(makeVorbaIdFactory());
82 VamsasDocument doc = sdoc.getVamsasDocument(odoc);
83 sessdoc.putVamsasDocument(doc, sdoc.vorba);
84 sessdoc.closeArchive();
85 } catch (Exception e) {
86 sessdoc.cancelArchive();
87 // write a dummy archive
88 _session.slog.info("Exception when importing document data from "+importingArchive);
89 throw new Exception("Failed to import data from "+importingArchive, e);
95 * LATER: check that build substitution variables are correct
96 * @see org.vamsas.client.IClient#getAbout()
98 public String getAbout() {
99 return new String("VORBA SimpleClient version $version$ build $build$");
105 * @see org.vamsas.client.IClient#getSessionUrn()
107 public String getSessionUrn() {
108 return session.getSessionUrn();
114 * @see org.vamsas.client.IClient#getSessionHandle()
116 public SessionHandle getSessionHandle() {
117 // TODO: eliminate SessionHandle ? need to refactor interfaces.
118 SessionHandle sh = new SessionHandle(session.getSessionUrn());
125 * @see org.vamsas.client.IClient#getClientHandle()
127 public ClientHandle getClientHandle() {
134 * @see org.vamsas.client.IClient#getUserHandle()
136 public UserHandle getUserHandle() {
140 private Hashtable handlers = initHandlers();
142 private Vector listeners = new Vector();
145 * make all the PropertyChangeSupport objects for the
146 * events described in org.vamsas.client.Event
149 private static Hashtable initHandlers() {
150 Hashtable events = new Hashtable();
151 java.util.Iterator evt = Events.EventList.iterator();
152 while (evt.hasNext()) {
153 Object ths = evt.next();
154 events.put(ths, (Object) new PropertyChangeSupport(ths));
162 * @see org.vamsas.client.IClient#addDocumentUpdateHandler(java.util.EventListener)
164 public void addDocumentUpdateHandler(PropertyChangeListener evt) {
165 if (handlers.containsKey(Events.DOCUMENT_UPDATE)) {
167 ((PropertyChangeSupport) (handler = handlers.get(Events.DOCUMENT_UPDATE)))
168 .addPropertyChangeListener(evt);
169 listeners.add(handler);
170 listeners.add((Object) evt);
173 boolean finalized=false;
177 * @see org.vamsas.client.IClient#finalizeClient()
179 public void finalizeClient() {
180 // TODO: determine if this is last client in session
181 // TODO: raise events like : ((lst_client && document.request.to.close), (client_finalization), (
183 // if (handlers.containsKey(Events.))
184 // if (handlers.containsKey(Events.CLIENT_FINALIZATION))
185 // deregister listeners.
186 // mark this instance as finalized
192 * @see org.vamsas.client.IClient#getClientDocument()
194 public IClientDocument getClientDocument() throws IOException {
196 VamsasArchive va = null;
198 // TODO: bail out if it takes too long to get the lock ?
199 va = _session.getVamsasDocument();
201 catch (IOException e) {
202 throw new IOException("Failed to get lock on session document");
204 VamsasDocument doc=null;
205 IdFactory vorba = null;
206 // TODO: reduce size of vorba ids generated from these parameters to IdFactory (mainly sessionHandle rationalization ?)
208 va.setVorba(vorba=makeVorbaIdFactory());
209 // if session currently holds data - read it in - or get a dummy
210 _session.slog.debug("Accessing document");
212 va.getVamsasDocument(user.getFullName()+" using "+client.getClientName(),
213 "created new session document.", null);
215 _session.slog.debug("Successfully retrieved document.");
217 log.error("Unexpectedly retrieved null document!");
219 catch (Exception e) {
220 log.error("Failed to get session document for session directory '"+_session.sessionDir+"'", e);
221 throw new IOException("Failed to get session document for session directory '"+_session.sessionDir+"'");
223 // Construct the IClientDocument instance
225 ClientDocument cdoc = new ClientDocument(doc, va, vorba, this);
232 * @see org.vamsas.client.IClient#updateDocument(org.vamsas.client.IClientDocument)
234 public void updateDocument(IClientDocument newdoc) {
235 if (!(newdoc instanceof ClientDocument)) {
236 throw new Error("Invalid IClientDocument instance for SimpleClient.");
239 // try to update the sessionFile
241 // write the appHandle to the lastupdate file.
248 * @see org.vamsas.client.IClient#storeDocument(java.io.File)
250 public void storeDocument(File location) {
252 // write storeDocument file to inform other clients that they should raise
253 Lock vamlock = evgen.want_to_store();
254 // Events.DOCUMENT_FINALIZEAPPDATA
256 _session.writeVamsasDocument(location, vamlock);
257 } catch (Exception e) {
258 log.warn("Exception whilst trying to store document in "+location,e);
266 * @see org.vamsas.client.IClient#addVorbaEventHandler(java.lang.String,
267 * java.beans.PropertyChangeListener)
269 public void addVorbaEventHandler(String EventChain, PropertyChangeListener evt) {
270 if (handlers.containsKey(EventChain)) {
272 ((PropertyChangeSupport) (handler = handlers.get(EventChain)))
273 .addPropertyChangeListener(evt);
274 listeners.add(handler);
275 listeners.add((Object) evt);
280 * @see org.vamsas.client.IClient#pollUpdate()
282 public void pollUpdate() {
285 log.warn("pollUpdate called on incomplete SimpleClient object.");
289 if (!evgen.isAlive()) {
290 log.warn("pollUpdate called before joinSession()");
293 } catch (Exception e) {
294 log.error("Unexpected exception on default call to joinSession",e);
298 //TODO ensure event generator robustly handles these interrupts.
299 log.debug("interrrupting event generator.");
301 log.debug("interrrupted event generator.");
305 * @see org.vamsas.client.IClient#joinSession()
307 public void joinSession() throws Exception {
308 // start the EventGenerator thread.
310 log.warn("joinSession called on incomplete SimpleClient object.");
314 throw new Error("Join session called twice for the same SimpleClient (IClient instance).");
317 log.debug("Started EventGenerator thread.");
319 log.warn("Failed to start EventGenerator thread.");
320 throw new Exception("Failed to start event generator thread - client cannot be instantiated.");
328 * @see org.vamsas.client.IClient#importDocument(java.io.File)
330 public void importDocument(File location) {
331 // TODO LATER: implement SimpleClient.importDocument()
332 log.error("importDocument is not implemented for a SimpleClient Session.");