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 org.vamsas.test;
9 import org.vamsas.client.*;
11 import java.awt.Event;
12 import java.beans.PropertyChangeEvent;
13 import java.beans.PropertyChangeListener;
14 import java.util.Vector;
16 * Toy vamsas command line client application demonstrating the API.
21 public class ExampleApplication {
22 private static ClientHandle app;
23 private static UserHandle user;
24 private static IClientFactory clientfactory;
25 private static IClient vorbaclient;
26 private static byte[] mydata;
27 private static Vector vamsasObjects;
28 private static boolean isUpdated = false;
29 private static boolean isShuttingdown = false;
30 private static boolean isFinalizing = false;
31 private static void processVamsasDocument(IClientDocument doc) {
32 // merge vamsasObjects with vamsas objects in document
33 // get this apps 'mydata' if it hasn't got it already.
34 // .. access this application's 'public' mydata' if there is any.
36 private static void addHandlers(IClient avorbaclient) {
37 // make a non-volatile reference to the client instance.
38 final IClient vorbaclient = avorbaclient;
39 // register update handler
40 vorbaclient.addDocumentUpdateHandler(new PropertyChangeListener() {
41 public void propertyChange(PropertyChangeEvent evt) {
42 System.out.println("Vamsas document update for "+evt.getPropertyName()
43 +": "+evt.getOldValue()+" to "+evt.getNewValue());
44 // merge new data into ours.
45 isUpdated=true; // tell main thread to reflect change...
48 // register close handler
49 vorbaclient.addVorbaEventHandler(Events.DOCUMENT_REQUESTTOCLOSE,
50 new PropertyChangeListener() {
51 public void propertyChange(PropertyChangeEvent evt) {
52 System.out.println("Received request to close vamsas document.");
53 // ask user for a filename to save it to.
54 // Then pass it to the vorba object...
55 vorbaclient.storeDocument(new java.io.File("UserLocation"));
59 // register some more handlers to monitor the session :
61 vorbaclient.addVorbaEventHandler(Events.CLIENT_CREATION,
62 new PropertyChangeListener() {
63 public void propertyChange(PropertyChangeEvent evt) {
64 System.out.println("New Vamsas client for "+evt.getPropertyName()
65 +": "+evt.getOldValue()+" to "+evt.getNewValue());
66 // tell app add new client to its list of clients.
69 vorbaclient.addVorbaEventHandler(Events.CLIENT_FINALIZATION,
70 new PropertyChangeListener() {
71 public void propertyChange(PropertyChangeEvent evt) {
72 System.out.println("Vamsas client finalizing for "+evt.getPropertyName()
73 +": "+evt.getOldValue()+" to "+evt.getNewValue());
74 // tell app to update its list of clients to communicate with.
77 vorbaclient.addVorbaEventHandler(Events.SESSION_SHUTDOWN,
78 new PropertyChangeListener() {
79 public void propertyChange(PropertyChangeEvent evt) {
80 System.out.println("Session "+evt.getPropertyName()+" is shutting down.");
81 // tell app to finalize its session data before shutdown.
86 Usage="ExampleApplication <vamsasFileDirectory> <vamsasSessionURN> <action> [+<arguments>]\n"
87 +"<action> is one of :\n\tsave,update,close,watch";
89 private static boolean parseArgs(String args[]) {
90 return true; // incorrect arguments.
92 public static void main(String[] args) {
93 if ((args.length<=2) || parseArgs(args)) {
94 System.err.print(Usage);
98 clientfactory = new org.vamsas.client.SimpleClientFactory(args[0]);
100 // get an Iclient with session data
101 app = new ClientHandle("org.vamsas.test.ExampleApplication","0.1");
102 user = new UserHandle("arnolduser","deathsdoor");
103 vorbaclient = clientfactory.getIClient(app, user);
104 addHandlers(vorbaclient);
105 // register an update listener and a close listener.
107 processVamsasDocument(vorbaclient.getClientDocument());
109 // Main application event loop - wait around and do stuff...
110 while (!isShuttingdown) {
111 // do something with data
112 // , update document, or something.
116 // call finalizeClient
117 vorbaclient.finalizeClient();
118 // { meanwhile, eventHandlers are called to do any saves if need be }
119 // and all registered listeners will be deregistered to avoid deadlock.