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.VamsasDocument;
32 public class SimpleClient implements IClient {
34 private static Log log = LogFactory.getLog(SimpleClient.class);
36 protected UserHandle user = null;
38 protected SessionUrn session = null;
39 protected VamsasSession _session;
40 protected ClientHandle client = null;
41 protected EventGeneratorThread evgen = null;
42 IdFactory vorba = null;
43 private void makeVorbaIdFactory() {
45 vorba = new IdFactory(getSessionHandle(), client, user);
47 log.debug("Vorba Id factory exists already.");
51 * construct SimpleClient for user, client and VamsasSession directory
52 * use the SimpleClientFactory rather than this constructor directly.
57 protected SimpleClient(UserHandle user, ClientHandle client, VamsasSession sess) {
58 // TODO: validate user/client/session
62 session = new SessionUrn(_session);
65 * construct new session by importing objects from an existing vamsas document
69 * @param importingArchive
70 * @throws Exception IOExceptions for Session IO problems, and general Exception if importing document is invalid.
72 protected SimpleClient(UserHandle user, ClientHandle client, VamsasSession sess, File importingArchive) throws Exception {
73 this(user, client, sess);
75 VamsasArchive sessdoc = _session.getVamsasDocument();
77 VamsasArchiveReader odoc = new VamsasArchiveReader(importingArchive);
78 SimpleDocument sdoc = new SimpleDocument(vorba);
79 VamsasDocument doc = sdoc.getVamsasDocument(odoc);
80 sessdoc.putVamsasDocument(doc, vorba);
81 sessdoc.closeArchive();
82 } catch (Exception e) {
83 sessdoc.cancelArchive();
84 // write a dummy archive
85 throw new Exception("Failed to import data from "+importingArchive, e);
90 * TODO: LATER: check that build substitution variables are correct
91 * @see org.vamsas.client.IClient#getAbout()
93 public String getAbout() {
94 return new String("VORBA SimpleClient version $version$ build $build$");
100 * @see org.vamsas.client.IClient#getSessionUrn()
102 public String getSessionUrn() {
103 return session.getSessionUrn();
109 * @see org.vamsas.client.IClient#getSessionHandle()
111 public SessionHandle getSessionHandle() {
112 // TODO: eliminate SessionHandle ? need to refactor interfaces.
113 SessionHandle sh = new SessionHandle(session.getSessionUrn());
120 * @see org.vamsas.client.IClient#getClientHandle()
122 public ClientHandle getClientHandle() {
129 * @see org.vamsas.client.IClient#getUserHandle()
131 public UserHandle getUserHandle() {
135 private Hashtable handlers = initHandlers();
137 private Vector listeners = new Vector();
140 * make all the PropertyChangeSupport objects for the
141 * events described in org.vamsas.client.Event
144 private static Hashtable initHandlers() {
145 Hashtable events = new Hashtable();
146 java.util.Iterator evt = Events.EventList.iterator();
147 while (evt.hasNext()) {
148 Object ths = evt.next();
149 events.put(ths, (Object) new PropertyChangeSupport(ths));
157 * @see org.vamsas.client.IClient#addDocumentUpdateHandler(java.util.EventListener)
159 public void addDocumentUpdateHandler(PropertyChangeListener evt) {
160 if (handlers.containsKey(Events.DOCUMENT_UPDATE)) {
162 ((PropertyChangeSupport) (handler = handlers.get(Events.DOCUMENT_UPDATE)))
163 .addPropertyChangeListener(evt);
164 listeners.add(handler);
165 listeners.add((Object) evt);
168 boolean finalized=false;
172 * @see org.vamsas.client.IClient#finalizeClient()
174 public void finalizeClient() {
175 // TODO: determine if this is last client in session
176 // TODO: raise events like : ((lst_client && document.request.to.close), (client_finalization), (
178 // if (handlers.containsKey(Events.))
179 // if (handlers.containsKey(Events.CLIENT_FINALIZATION))
180 // deregister listeners.
181 // mark this instance as finalized
185 * extract data appropriate for client, session and user
186 * from vamsas document.
187 * @return application's byte array
189 private byte[] getApplicationData() {
190 // TODO: extract correct byte object from Jar and return it to application.
197 * @see org.vamsas.client.IClient#getClientDocument()
199 public IClientDocument getClientDocument() throws IOException {
201 makeVorbaIdFactory();
202 VamsasArchive va = _session.getVamsasDocument();
203 // TODO: reduce size of vorba ids generated from these parameters to IdFactory (mainly sessionHandle rationalization ?)
205 va.getOriginalVamsasDocument(va, vorba);
206 // if session currently holds data - read it in.
208 catch (Exception e) {
211 //ClientDocument cdoc = new ClientDocument();
213 Object[] vdoc;// TODO: = getVamsasDocument(new Reader());
214 // ClientDocument cdoc = new ClientDocument(getApplicationData(),
215 // ((VamsasDocument) vdoc[0]).getVAMSAS(), (Hashtable) vdoc[1], this);
223 * @see org.vamsas.client.IClient#updateDocument(org.vamsas.client.IClientDocument)
225 public void updateDocument(IClientDocument newdoc) {
226 if (!(newdoc instanceof ClientDocument)) {
227 throw new Error("Invalid IClientDocument instance for SimpleClient.");
230 // try to update the sessionFile
232 // write the appHandle to the lastupdate file.
239 * @see org.vamsas.client.IClient#storeDocument(java.io.File)
241 public void storeDocument(File location) {
243 // write storeDocument file to inform other clients that they should raise
244 Lock vamlock = evgen.want_to_store();
245 // Events.DOCUMENT_FINALIZEAPPDATA
247 _session.writeVamsasDocument(location, vamlock);
248 } catch (Exception e) {
249 log.warn("Exception whilst trying to store document in "+location,e);
257 * @see org.vamsas.client.IClient#addVorbaEventHandler(java.lang.String,
258 * java.beans.PropertyChangeListener)
260 public void addVorbaEventHandler(String EventChain, PropertyChangeListener evt) {
261 if (handlers.containsKey(EventChain)) {
263 ((PropertyChangeSupport) (handler = handlers.get(EventChain)))
264 .addPropertyChangeListener(evt);
265 listeners.add(handler);
266 listeners.add((Object) evt);
271 * @see org.vamsas.client.IClient#pollUpdate()
273 public void pollUpdate() {
276 log.warn("pollUpdate called on incomplete SimpleClient object.");
279 //TODO ensure event generator robustly handles these interrupts.
280 log.debug("interrrupting event generator.");
282 log.debug("interrrupted event generator.");
286 * @see org.vamsas.client.IClient#joinSession()
288 public void joinSession() throws Exception {
289 // start the EventGenerator thread.
291 log.warn("joinSession called on incomplete SimpleClient object.");
295 throw new Error("Join session called twice for the same SimpleClient (IClient instance).");
298 log.debug("Started EventGenerator thread.");
300 log.warn("Failed to start EventGenerator thread.");
301 throw new Exception("Failed to start event generator thread - client cannot be instantiated.");
309 * @see org.vamsas.client.IClient#importDocument(java.io.File)
311 public void importDocument(File location) {
312 // TODO LATER: implement SimpleClient.importDocument()
313 log.error("importDocument is not implemented for a SimpleClient Session.");