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;
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.io.Reader;
17 import java.io.Writer;
18 import java.lang.reflect.Field;
19 import java.util.Hashtable;
20 import java.util.Vector;
22 import org.exolab.castor.xml.IDResolver;
23 import org.exolab.castor.xml.MarshalException;
24 import org.exolab.castor.xml.Marshaller;
25 import org.exolab.castor.xml.UnmarshalListener;
26 import org.exolab.castor.xml.Unmarshaller;
27 import org.exolab.castor.xml.ValidationException;
28 import org.vamsas.objects.core.VamsasDocument;
33 public class SimpleClient implements IClient {
35 UserHandle user = null;
37 SessionHandle session = null;
39 ClientHandle client = null;
43 * @see org.vamsas.client.IClient#getAbout()
45 public String getAbout() {
46 return new String("VORBA SimpleClient version $version$ build $build$");
52 * @see org.vamsas.client.IClient#getSessionUrn()
54 public String getSessionUrn() {
55 return session.getSessionUrn();
61 * @see org.vamsas.client.IClient#getSessionHandle()
63 public SessionHandle getSessionHandle() {
70 * @see org.vamsas.client.IClient#getClientHandle()
72 public ClientHandle getClientHandle() {
79 * @see org.vamsas.client.IClient#getUserHandle()
81 public UserHandle getUserHandle() {
85 private Hashtable handlers = initHandlers();
86 private Vector listeners = new Vector();
88 * make all the PropertyChangeSupport objects for the
89 * events described in org.vamsas.client.Event
92 private static Hashtable initHandlers() {
93 Hashtable events = new Hashtable();
94 java.util.Iterator evt = Events.EventList.iterator();
95 while (evt.hasNext()) {
96 Object ths = evt.next();
97 events.put(ths, (Object) new PropertyChangeSupport(ths));
104 * @see org.vamsas.client.IClient#addDocumentUpdateHandler(java.util.EventListener)
106 public void addDocumentUpdateHandler(PropertyChangeListener evt) {
107 if (handlers.containsKey(Events.DOCUMENT_UPDATE)) {
109 ((PropertyChangeSupport) (handler
110 = handlers.get(Events.DOCUMENT_UPDATE))).addPropertyChangeListener(evt);
111 listeners.add(handler);
112 listeners.add((Object) evt);
119 * @see org.vamsas.client.IClient#finalizeClient()
121 public void finalizeClient() {
122 // TODO: raise events
123 // deregister listeners.
124 // mark this instance as finalized
128 * writes the VamsasDocument to the given stream.
131 * @throws IOException
132 * @throws MarshalException
133 * @throws ValidationException
135 private static void setVamsasDocument(Writer outstream, VamsasDocument doc) throws IOException, MarshalException, ValidationException {
136 Marshaller marshaller = new Marshaller(outstream);
137 marshaller.marshal(doc);
141 * Unmarshals a vamsasDocument object from a stream, registers
142 * any VorbaIds, and completes the org.vamsas.client.object housekeeping fields.
144 * @param instream - the XML input stream
145 * @param factory - the SimpleClient's properly configured VorbaId factory to make new references.
146 * @return null or {(Object) VamsasDocument object, (Object) Hashtable of object references)
148 private static Object[] getVamsasDocument(Reader instream,
149 IVorbaIdFactory factory) {
150 Unmarshaller unmarshaller = new Unmarshaller(instream);
151 unmarshaller.setIDResolver(new IDResolver() {
152 public Object resolve(String id) {
153 System.err.println("Warning - id " + id
154 + " is not found in the VamsasDocument!");
158 Hashtable refbase = new Hashtable();
159 final Hashtable objrefs = refbase;
160 final IVorbaIdFactory vorbafactory = factory;
161 unmarshaller.setUnmarshalListener(new UnmarshalListener() {
166 * @see org.exolab.castor.xml.UnmarshalListener#attributesProcessed(java.lang.Object)
168 public void attributesProcessed(Object object) {
174 * @see org.exolab.castor.xml.UnmarshalListener#fieldAdded(java.lang.String,
175 * java.lang.Object, java.lang.Object)
177 public void fieldAdded(String fieldName, Object parent, Object child) {
183 * @see org.exolab.castor.xml.UnmarshalListener#initialized(java.lang.Object)
185 public void initialized(Object object) {
189 * Check if the object has an 'id' field - if it does, copy the value into
190 * the VorbaId field of object, and add the object to the VorbaId hash.
192 * @see org.exolab.castor.xml.UnmarshalListener#unmarshalled(java.lang.Object)
194 public void unmarshalled(Object newobj) {
195 if (newobj instanceof object) {
196 object nobj = (object) newobj;
197 nobj.set__stored_in_document(true);
200 // look for the id field (should be an NCName string)
201 fd = nobj.getClass().getField("id");
203 if (fd.getType().getClass().equals("astring".getClass())) {
204 if (fd.get(nobj) != null) {
205 idstring = (String) fd.get(nobj);
206 if (idstring.length() > 0)
207 if (!objrefs.containsKey(idstring)) {
208 objrefs.put(idstring, nobj);
209 nobj.setVorbaId(VorbaId.newId(idstring));
210 nobj.__vorba = vorbafactory;
215 } catch (Exception e) {
223 // Call the unmarshaller.
225 while (instream.ready()) {
226 Object obj = unmarshaller.unmarshal(instream);
227 if (obj instanceof VamsasDocument) {
228 return new Object[] { obj, objrefs};
231 } catch (MarshalException e) {
232 // TODO Auto-generated catch block
234 } catch (ValidationException e) {
235 // TODO Auto-generated catch block
237 } catch (IOException e) {
238 // TODO Auto-generated catch block
244 * extract data appropriate for client, session and user
245 * from vamsas document.
246 * @return application's byte array
248 private byte[] getApplicationData() {
255 * @see org.vamsas.client.IClient#getClientDocument()
257 public IClientDocument getClientDocument() {
258 Object[] vdoc;// TODO: = getVamsasDocument(new Reader());
259 ClientDocument cdoc = new ClientDocument(getApplicationData(),
260 ((VamsasDocument) vdoc[0]).getVAMSAS(), (Hashtable) vdoc[1], this);
268 * @see org.vamsas.client.IClient#updateDocument(org.vamsas.client.IClientDocument)
270 public void updateDocument(IClientDocument newdoc) {
271 // TODO Auto-generated method stub
278 * @see org.vamsas.client.IClient#storeDocument(java.io.File)
280 public void storeDocument(File location) {
281 // TODO Auto-generated method stub
288 * @see org.vamsas.client.IClient#addVorbaEventHandler(java.lang.String,
289 * java.beans.PropertyChangeListener)
291 public void addVorbaEventHandler(String EventChain, PropertyChangeListener evt) {
292 if (handlers.containsKey(EventChain)) {
294 ((PropertyChangeSupport) (handler
295 = handlers.get(EventChain))).addPropertyChangeListener(evt);
296 listeners.add(handler);
297 listeners.add((Object) evt);
302 * @see org.vamsas.client.IClient#pollUpdate()
304 public void pollUpdate() {
305 // TODO wake up UpdateWatcher thread to check for updates.
309 public static void main(String[] args) {