2 * Created on 14-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.test;
9 import uk.ac.vamsas.client.*;
10 import uk.ac.vamsas.client.picking.IMessageHandler;
11 import uk.ac.vamsas.client.picking.IPickManager;
12 import uk.ac.vamsas.client.picking.Message;
13 import uk.ac.vamsas.objects.core.VAMSAS;
14 import uk.ac.vamsas.test.objects.Core;
16 import java.awt.Event;
17 import java.beans.PropertyChangeEvent;
18 import java.beans.PropertyChangeListener;
19 import java.io.IOException;
20 import java.util.Vector;
23 * Toy vamsas command line client application demonstrating the API. TODO: test
24 * appData get/set methods TODO: verify and test pickManager and interaction
25 * between it and other session events TODO: add more session interaction events
26 * (currently: modifies document on start up, then modifies every 2 updates
27 * before finalizing after 5 updates.
32 public class ExampleApplication
34 private static ClientHandle app;
36 private static UserHandle user; // TODO: make this something defined by the
39 private static IClientFactory clientfactory;
41 private static IClient vorbaclient;
43 private static byte[] mydata;
45 private static Vector vamsasObjects;
47 private static boolean isUpdated = false;
49 private static boolean isShuttingdown = false;
51 private static boolean isFinalizing = false;
53 private static void processVamsasDocument(IClientDocument doc)
55 doc.addVamsasRoot(Core.getDemoVamsas());
56 vorbaclient.updateDocument(doc);
57 // merge vamsasObjects with vamsas objects in document
58 // get this apps 'mydata' if it hasn't got it already.
59 // .. access this application's 'public' mydata' if there is any.
62 private static void addHandlers(IClient avorbaclient)
64 // make a non-volatile reference to the client instance.
65 final IClient vorbaclient = avorbaclient;
66 // register update handler
67 vorbaclient.addDocumentUpdateHandler(new PropertyChangeListener() {
68 public void propertyChange(PropertyChangeEvent evt)
70 System.out.println("Vamsas document update for "
71 + evt.getPropertyName() + ": " + evt.getOldValue()
72 + " to " + evt.getNewValue());
73 // merge new data into ours.
74 // example - output doc
77 IClientDocument cdoc = vorbaclient.getClientDocument();
78 uk.ac.vamsas.test.simpleclient.ArchiveReports.rootReport(
79 cdoc.getVamsasRoots(), true, System.out);
80 vorbaclient.updateDocument(cdoc);
85 .println("Exception whilst dumping document tree after an update.");
86 e.printStackTrace(System.err);
88 isUpdated = true; // tell main thread to reflect change...
91 // register close handler
92 vorbaclient.addVorbaEventHandler(Events.DOCUMENT_REQUESTTOCLOSE,
93 new PropertyChangeListener() {
94 public void propertyChange(PropertyChangeEvent evt)
97 .println("Received request to close vamsas document.");
98 // ask user for a filename to save it to.
99 // Then pass it to the vorba object...
100 vorbaclient.storeDocument(new java.io.File(
105 // register some more handlers to monitor the session :
107 vorbaclient.addVorbaEventHandler(Events.CLIENT_CREATION,
108 new PropertyChangeListener() {
109 public void propertyChange(PropertyChangeEvent evt)
111 System.out.println("New Vamsas client for "
112 + evt.getPropertyName() + ": "
113 + evt.getOldValue() + " to "
114 + evt.getNewValue());
115 // tell app add new client to its list of clients.
118 vorbaclient.addVorbaEventHandler(Events.CLIENT_FINALIZATION,
119 new PropertyChangeListener() {
120 public void propertyChange(PropertyChangeEvent evt)
122 System.out.println("Vamsas client finalizing for "
123 + evt.getPropertyName() + ": "
124 + evt.getOldValue() + " to "
125 + evt.getNewValue());
126 // tell app to update its list of clients to communicate
130 vorbaclient.addVorbaEventHandler(Events.SESSION_SHUTDOWN,
131 new PropertyChangeListener() {
132 public void propertyChange(PropertyChangeEvent evt)
134 System.out.println("Session " + evt.getPropertyName()
135 + " is shutting down.");
136 // tell app to finalize its session data before
140 vorbaclient.addVorbaEventHandler(Events.DOCUMENT_FINALIZEAPPDATA,
141 new PropertyChangeListener() {
142 public void propertyChange(PropertyChangeEvent evt)
145 .println("Application received a DOCUMENT_FINALIZEAPPDATA event.");
146 // tell app to finalize its session data prior to the
147 // storage of the current session as an archive.
153 public static String Usage = "ExampleApplication [session urn] watch/n( future usage is :/n <vamsasFileDirectory> <vamsasSessionURN> <action> [+<arguments>]\n"
154 + "<action> is one of :\n\tsave,update,close,watch";
156 static String sess = null;
158 private static boolean parseArgs(String args[])
160 if (args.length == 0)
164 if (!args[0].toLowerCase().equals("watch"))
171 public static void main(String[] args)
173 class ExamplePicker extends Thread
177 public IPickManager pm = null;
179 ExamplePicker(String me, IPickManager pm)
190 try { Thread.sleep(1000 + (long) Math.random() * 10000); }
191 catch (Exception e) {}
195 pm.sendMessage(new uk.ac.vamsas.client.picking.CustomMessage("Message " + mcount++ + " from " + me));
202 if (!parseArgs(args))
204 System.err.print(Usage);
206 // get IClientFactory
209 clientfactory = new uk.ac.vamsas.client.simpleclient.SimpleClientFactory();
211 catch (IOException e)
213 System.err.println(e + "\n" + Usage);
217 // get an Iclient with session data
218 app = new ClientHandle("uk.ac.vamsas.test.ExampleApplication", "0.1");
219 user = new UserHandle("arnolduser", "deathsdoor");
222 vorbaclient = clientfactory.getIClient(app, user);
224 catch (NoDefaultSessionException e)
227 .println("There appear to be several sessions to choose from :");
228 String[] sessions = clientfactory.getCurrentSessions();
229 for (int s = 0; s < sessions.length; s++)
230 System.err.println(sessions[s]);
233 addHandlers(vorbaclient);
236 vorbaclient.joinSession();
240 se.printStackTrace();
241 System.err.println(se + " when joining session.\n" + Usage);
244 // register an update listener and a close listener.
248 IClientDocument cdoc = vorbaclient.getClientDocument();
249 processVamsasDocument(cdoc);
254 .println("Unexpected exception when retrieving the client document for the first time!");
255 e.printStackTrace(System.err);
259 ExamplePicker picker = new ExamplePicker(vorbaclient.getClientHandle()
260 .getClientUrn(), vorbaclient.getPickManager());
263 picker.pm.registerMessageHandler(new IMessageHandler() {
265 public void handleMessage(Message message)
268 .println("Received |" + message.getRawMessage() + "|");
272 while (!isShuttingdown && update < 5)
274 // do something with data
275 // , update document, or something.
279 System.out.println("Update handler called " + (++update)
282 .println("******************************************");
283 isUpdated = false; // TODO: saner update det method.
288 IClientDocument cdoc = vorbaclient.getClientDocument();
289 processVamsasDocument(cdoc);
294 .println("Error when updating document after an even numbered update.");
295 e.printStackTrace(System.err);
300 try { Thread.sleep(15); }
301 catch (Exception e) {}
304 System.out.println("Shutting down picker.");
306 while (picker.isAlive())
308 System.out.println("Waiting for picker to die...");
319 System.out.println("Finalizing.");
320 // call finalizeClient
321 vorbaclient.finalizeClient();
322 // { meanwhile, eventHandlers are called to do any saves if need be }
323 // and all registered listeners will be deregistered to avoid deadlock.