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.objects.core.VAMSAS;
11 import uk.ac.vamsas.test.objects.Core;
13 import java.awt.Event;
14 import java.beans.PropertyChangeEvent;
15 import java.beans.PropertyChangeListener;
16 import java.io.IOException;
17 import java.util.Vector;
19 * Toy vamsas command line client application demonstrating the API.
20 * TODO: test appData get/set methods
21 * TODO: verify and test pickManager and interaction between it and other session events
22 * TODO: add more session interaction events (currently: modifies document on start up, then modifies every 2 updates before finalizing after 5 updates.
26 public class ExampleApplication {
27 private static ClientHandle app;
28 private static UserHandle user; // TODO: make this something defined by the api
29 private static IClientFactory clientfactory;
30 private static IClient vorbaclient;
31 private static byte[] mydata;
32 private static Vector vamsasObjects;
33 private static boolean isUpdated = false;
34 private static boolean isShuttingdown = false;
35 private static boolean isFinalizing = false;
36 private static void processVamsasDocument(IClientDocument doc) {
37 doc.addVamsasRoot(Core.getDemoVamsas());
38 vorbaclient.updateDocument(doc);
39 // merge vamsasObjects with vamsas objects in document
40 // get this apps 'mydata' if it hasn't got it already.
41 // .. access this application's 'public' mydata' if there is any.
43 private static void addHandlers(IClient avorbaclient) {
44 // make a non-volatile reference to the client instance.
45 final IClient vorbaclient = avorbaclient;
46 // register update handler
47 vorbaclient.addDocumentUpdateHandler(new PropertyChangeListener() {
48 public void propertyChange(PropertyChangeEvent evt) {
49 System.out.println("Vamsas document update for "+evt.getPropertyName()
50 +": "+evt.getOldValue()+" to "+evt.getNewValue());
51 // merge new data into ours.
52 // example - output doc
54 IClientDocument cdoc = vorbaclient.getClientDocument();
55 uk.ac.vamsas.test.simpleclient.ArchiveReports.rootReport(cdoc.getVamsasRoots(), true, System.out);
56 vorbaclient.updateDocument(cdoc);
57 } catch (Exception e) {
58 System.err.println("Exception whilst dumping document tree after an update.");
59 e.printStackTrace(System.err);
61 isUpdated=true; // tell main thread to reflect change...
64 // register close handler
65 vorbaclient.addVorbaEventHandler(Events.DOCUMENT_REQUESTTOCLOSE,
66 new PropertyChangeListener() {
67 public void propertyChange(PropertyChangeEvent evt) {
68 System.out.println("Received request to close vamsas document.");
69 // ask user for a filename to save it to.
70 // Then pass it to the vorba object...
71 vorbaclient.storeDocument(new java.io.File("UserLocation"));
75 // register some more handlers to monitor the session :
77 vorbaclient.addVorbaEventHandler(Events.CLIENT_CREATION,
78 new PropertyChangeListener() {
79 public void propertyChange(PropertyChangeEvent evt) {
80 System.out.println("New Vamsas client for "+evt.getPropertyName()
81 +": "+evt.getOldValue()+" to "+evt.getNewValue());
82 // tell app add new client to its list of clients.
85 vorbaclient.addVorbaEventHandler(Events.CLIENT_FINALIZATION,
86 new PropertyChangeListener() {
87 public void propertyChange(PropertyChangeEvent evt) {
88 System.out.println("Vamsas client finalizing for "+evt.getPropertyName()
89 +": "+evt.getOldValue()+" to "+evt.getNewValue());
90 // tell app to update its list of clients to communicate with.
93 vorbaclient.addVorbaEventHandler(Events.SESSION_SHUTDOWN,
94 new PropertyChangeListener() {
95 public void propertyChange(PropertyChangeEvent evt) {
96 System.out.println("Session "+evt.getPropertyName()+" is shutting down.");
97 // tell app to finalize its session data before shutdown.
100 vorbaclient.addVorbaEventHandler(Events.DOCUMENT_FINALIZEAPPDATA,
101 new PropertyChangeListener() {
102 public void propertyChange(PropertyChangeEvent evt) {
103 System.out.println("Application received a DOCUMENT_FINALIZEAPPDATA event.");
104 // tell app to finalize its session data prior to the storage of the current session as an archive.
110 Usage="ExampleApplication [session urn] watch/n( future usage is :/n <vamsasFileDirectory> <vamsasSessionURN> <action> [+<arguments>]\n"
111 +"<action> is one of :\n\tsave,update,close,watch";
112 static String sess=null;
113 private static boolean parseArgs(String args[]) {
114 if (args.length==0) {
117 if (!args[0].toLowerCase().equals("watch")) {
122 public static void main(String[] args) {
124 if (!parseArgs(args)) {
125 System.err.print(Usage);
127 // get IClientFactory
129 clientfactory = new uk.ac.vamsas.client.simpleclient.SimpleClientFactory();
130 } catch (IOException e) {
131 System.err.println(e+"\n"+Usage);
135 // get an Iclient with session data
136 app = new ClientHandle("uk.ac.vamsas.test.ExampleApplication","0.1");
137 user = new UserHandle("arnolduser","deathsdoor");
139 vorbaclient = clientfactory.getIClient(app, user);
140 } catch (NoDefaultSessionException e) {
141 System.err.println("There appear to be several sessions to choose from :");
142 String[] sessions = clientfactory.getCurrentSessions();
143 for (int s=0;s<sessions.length; s++)
144 System.err.println(sessions[s]);
147 addHandlers(vorbaclient);
149 vorbaclient.joinSession();
151 catch (Exception se) {
152 se.printStackTrace();
153 System.err.println(se+" when joining session.\n"+Usage);
156 // register an update listener and a close listener.
159 IClientDocument cdoc = vorbaclient.getClientDocument();
160 processVamsasDocument(cdoc);
161 } catch (Exception e) {
162 System.err.println("Unexpected exception when retrieving the client document for the first time!");
163 e.printStackTrace(System.err);
167 // Main application event loop - wait around and do stuff...
168 while (!isShuttingdown && update<5) {
169 // do something with data
170 // , update document, or something.
173 System.out.println("Update handler called "+(++update)+" times");
174 System.out.println("******************************************");
175 isUpdated=false; // TODO: saner update det method.
178 IClientDocument cdoc = vorbaclient.getClientDocument();
179 processVamsasDocument(cdoc);
181 catch (Exception e) {
182 System.err.println("Error when updating document after an even numbered update.");
183 e.printStackTrace(System.err);
187 try { Thread.sleep(15); } catch (Exception e) {};
189 System.out.println("Finalizing.");
190 // call finalizeClient
191 vorbaclient.finalizeClient();
192 // { meanwhile, eventHandlers are called to do any saves if need be }
193 // and all registered listeners will be deregistered to avoid deadlock.