implementing minimal set : SimpleClientFactory createSession method -> SimpleClient...
[vamsas.git] / src / org / vamsas / client / simpleclient / SimpleClient.java
1 /*
2  * Created on 15-Sep-2005
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */
7 package org.vamsas.client.simpleclient;
8
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;
14 import java.io.File;
15 import java.io.IOException;
16 import java.util.Hashtable;
17 import java.util.Vector;
18
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;
28
29 /**
30  * @author jimp
31  */
32 public class SimpleClient implements IClient {
33   
34   private static Log log = LogFactory.getLog(SimpleClient.class);
35   
36   protected UserHandle user = null;
37   
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() {
44     if (vorba==null) {
45       vorba = new IdFactory(getSessionHandle(), client, user);
46     } else
47       log.debug("Vorba Id factory exists already.");
48   }
49   
50   /**
51    * construct SimpleClient for user, client and VamsasSession directory
52    * use the SimpleClientFactory rather than this constructor directly. 
53    * @param user
54    * @param client
55    * @param sess
56    */
57   protected SimpleClient(UserHandle user, ClientHandle client, VamsasSession sess) {
58     // TODO: validate user/client/session
59     _session = sess;
60     this.user = user;
61     this.client = client;
62     session = new SessionUrn(_session);
63   }
64   /**
65    * construct new session by importing objects from an existing vamsas document
66    * @param user
67    * @param client
68    * @param sess
69    * @param importingArchive
70    * @throws Exception IOExceptions for Session IO problems, and general Exception if importing document is invalid.
71    */
72   protected SimpleClient(UserHandle user, ClientHandle client, VamsasSession sess, File importingArchive) throws Exception {
73     this(user, client, sess);
74     makeVorbaIdFactory();
75     VamsasArchive sessdoc = _session.getVamsasDocument();
76     try {
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);
86     }
87   }
88   /*
89    * (non-Javadoc)
90    * TODO: LATER: check that build substitution variables are correct
91    * @see org.vamsas.client.IClient#getAbout()
92    */
93   public String getAbout() {
94     return new String("VORBA SimpleClient version $version$ build $build$");
95   }
96   
97   /*
98    * (non-Javadoc)
99    * 
100    * @see org.vamsas.client.IClient#getSessionUrn()
101    */
102   public String getSessionUrn() {
103     return session.getSessionUrn();
104   }
105   
106   /*
107    * (non-Javadoc)
108    * 
109    * @see org.vamsas.client.IClient#getSessionHandle()
110    */
111   public SessionHandle getSessionHandle() {
112     // TODO: eliminate SessionHandle ? need to refactor interfaces.
113     SessionHandle sh = new SessionHandle(session.getSessionUrn());
114     return sh;
115   }
116   
117   /*
118    * (non-Javadoc)
119    * 
120    * @see org.vamsas.client.IClient#getClientHandle()
121    */
122   public ClientHandle getClientHandle() {
123     return client;
124   }
125   
126   /*
127    * (non-Javadoc)
128    * 
129    * @see org.vamsas.client.IClient#getUserHandle()
130    */
131   public UserHandle getUserHandle() {
132     return user;
133   }
134   
135   private Hashtable handlers = initHandlers();
136   
137   private Vector listeners = new Vector();
138   
139   /**
140    * make all the PropertyChangeSupport objects for the
141    * events described in org.vamsas.client.Event
142    * @return
143    */
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));
150     }
151     return events;
152   }
153   
154   /*
155    * (non-Javadoc)
156    * 
157    * @see org.vamsas.client.IClient#addDocumentUpdateHandler(java.util.EventListener)
158    */
159   public void addDocumentUpdateHandler(PropertyChangeListener evt) {
160     if (handlers.containsKey(Events.DOCUMENT_UPDATE)) {
161       Object handler;
162       ((PropertyChangeSupport) (handler = handlers.get(Events.DOCUMENT_UPDATE)))
163       .addPropertyChangeListener(evt);
164       listeners.add(handler);
165       listeners.add((Object) evt);
166     }
167   }
168   boolean finalized=false;
169   /*
170    * (non-Javadoc)
171    * 
172    * @see org.vamsas.client.IClient#finalizeClient()
173    */
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), (
177     
178     // if (handlers.containsKey(Events.))
179     // if (handlers.containsKey(Events.CLIENT_FINALIZATION))
180     // deregister listeners.
181     // mark this instance as finalized
182   }
183   
184   /**
185    * extract data appropriate for client, session and user
186    * from vamsas document.
187    * @return application's byte array
188    */
189   private byte[] getApplicationData() {
190     // TODO: extract correct byte object from Jar and return it to application.
191     return null;
192   }
193
194   /*
195    * (non-Javadoc)
196    * 
197    * @see org.vamsas.client.IClient#getClientDocument()
198    */
199   public IClientDocument getClientDocument() throws IOException {
200     
201     makeVorbaIdFactory();
202     VamsasArchive va = _session.getVamsasDocument();
203     // TODO: reduce size of vorba ids generated from these parameters to IdFactory (mainly sessionHandle rationalization ?)
204     try {
205       va.getOriginalVamsasDocument(va, vorba);
206       // if session currently holds data - read it in.
207     }
208     catch (Exception e) {
209       
210     }
211     //ClientDocument cdoc = new ClientDocument();
212     
213     Object[] vdoc;//  TODO: = getVamsasDocument(new Reader());
214     // ClientDocument cdoc = new ClientDocument(getApplicationData(),
215     // ((VamsasDocument) vdoc[0]).getVAMSAS(), (Hashtable) vdoc[1], this);
216     // 
217     return null;
218   }
219   
220   /*
221    * (non-Javadoc)
222    * 
223    * @see org.vamsas.client.IClient#updateDocument(org.vamsas.client.IClientDocument)
224    */
225   public void updateDocument(IClientDocument newdoc) {
226     if (!(newdoc instanceof ClientDocument)) {
227       throw new Error("Invalid IClientDocument instance for SimpleClient.");
228     }
229     
230     // try to update the sessionFile
231     
232     // write the appHandle to the lastupdate file.
233     
234   }
235   
236   /*
237    * (non-Javadoc)
238    * 
239    * @see org.vamsas.client.IClient#storeDocument(java.io.File)
240    */
241   public void storeDocument(File location) {
242
243     // write storeDocument file to inform other clients that they should raise
244     Lock vamlock = evgen.want_to_store();
245     // Events.DOCUMENT_FINALIZEAPPDATA
246     try {
247       _session.writeVamsasDocument(location, vamlock);
248     } catch (Exception e) {
249       log.warn("Exception whilst trying to store document in "+location,e);
250     }
251     vamlock.release();
252   }
253   
254   /*
255    * (non-Javadoc)
256    * 
257    * @see org.vamsas.client.IClient#addVorbaEventHandler(java.lang.String,
258    *      java.beans.PropertyChangeListener)
259    */
260   public void addVorbaEventHandler(String EventChain, PropertyChangeListener evt) {
261     if (handlers.containsKey(EventChain)) {
262       Object handler;
263       ((PropertyChangeSupport) (handler = handlers.get(EventChain)))
264       .addPropertyChangeListener(evt);
265       listeners.add(handler);
266       listeners.add((Object) evt);
267     }
268   }
269   
270   /* (non-Javadoc)
271    * @see org.vamsas.client.IClient#pollUpdate()
272    */
273   public void pollUpdate() {
274
275     if (evgen==null) {
276       log.warn("pollUpdate called on incomplete SimpleClient object.");
277       return;
278     }
279     //TODO ensure event generator robustly handles these interrupts.
280     log.debug("interrrupting event generator.");
281     evgen.interrupt();
282     log.debug("interrrupted event generator.");
283   }
284
285   /* (non-Javadoc)
286    * @see org.vamsas.client.IClient#joinSession()
287    */
288   public void joinSession() throws Exception {
289     // start the EventGenerator thread.
290     if (evgen==null) {
291       log.warn("joinSession called on incomplete SimpleClient object.");
292       return;
293     }
294     if (evgen.isAlive())
295       throw new Error("Join session called twice for the same SimpleClient (IClient instance).");
296     evgen.start();
297     if (evgen.isAlive())
298       log.debug("Started EventGenerator thread.");
299     else {
300       log.warn("Failed to start EventGenerator thread.");
301       throw new Exception("Failed to start event generator thread - client cannot be instantiated.");
302     }
303     
304   }
305   
306   
307   
308   /* (non-Javadoc)
309    * @see org.vamsas.client.IClient#importDocument(java.io.File)
310    */
311   public void importDocument(File location) {
312     // TODO LATER: implement SimpleClient.importDocument()
313     log.error("importDocument is not implemented for a SimpleClient Session.");
314   }
315 }