test clientsFile handlers.
[vamsas.git] / src / org / vamsas / test / ExampleApplication.java
1 /**
2  * Created on 14-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.test;
8
9 import org.vamsas.client.*;
10
11 import java.awt.Event;
12 import java.beans.PropertyChangeEvent;
13 import java.beans.PropertyChangeListener;
14 import java.io.IOException;
15 import java.util.Vector;
16 /**
17  * Toy vamsas command line client application demonstrating the API.
18  * 
19  * @author jimp
20  */
21
22 public class ExampleApplication {
23   private static ClientHandle app;
24   private static UserHandle user;
25   private static IClientFactory clientfactory;
26   private static IClient vorbaclient;
27   private static byte[] mydata;
28   private static Vector vamsasObjects;
29   private static boolean isUpdated = false;
30   private static boolean isShuttingdown = false;
31   private static boolean isFinalizing = false;
32   private static void processVamsasDocument(IClientDocument doc) {
33     // merge vamsasObjects with vamsas objects in document
34     // get this apps 'mydata' if it hasn't got it already.
35     // .. access this application's 'public' mydata' if there is any.
36   }
37   private static void addHandlers(IClient avorbaclient) {
38     // make a non-volatile reference to the client instance.
39     final IClient vorbaclient = avorbaclient;
40     // register update handler
41     vorbaclient.addDocumentUpdateHandler(new PropertyChangeListener() {
42       public void propertyChange(PropertyChangeEvent evt) {
43         System.out.println("Vamsas document update for "+evt.getPropertyName()
44             +": "+evt.getOldValue()+" to "+evt.getNewValue());
45         // merge new data into ours.
46         isUpdated=true; // tell main thread to reflect change...
47       }
48     });
49     // register close handler
50     vorbaclient.addVorbaEventHandler(Events.DOCUMENT_REQUESTTOCLOSE, 
51         new PropertyChangeListener() {
52         public void propertyChange(PropertyChangeEvent evt) {
53           System.out.println("Received request to close vamsas document.");
54           // ask user for a filename to save it to. 
55           // Then pass it to the vorba object...
56           vorbaclient.storeDocument(new java.io.File("UserLocation"));
57         }
58     });
59     
60     // register some more handlers to monitor the session :
61     
62     vorbaclient.addVorbaEventHandler(Events.CLIENT_CREATION, 
63         new PropertyChangeListener() {
64       public void propertyChange(PropertyChangeEvent evt) {
65         System.out.println("New Vamsas client for "+evt.getPropertyName()
66             +": "+evt.getOldValue()+" to "+evt.getNewValue());
67         // tell app add new client to its list of clients.
68       }
69     });
70     vorbaclient.addVorbaEventHandler(Events.CLIENT_FINALIZATION, 
71         new PropertyChangeListener() {
72       public void propertyChange(PropertyChangeEvent evt) {
73         System.out.println("Vamsas client finalizing for "+evt.getPropertyName()
74             +": "+evt.getOldValue()+" to "+evt.getNewValue());
75         // tell app to update its list of clients to communicate with.
76       }
77     });
78     vorbaclient.addVorbaEventHandler(Events.SESSION_SHUTDOWN, 
79         new PropertyChangeListener() {
80       public void propertyChange(PropertyChangeEvent evt) {
81         System.out.println("Session "+evt.getPropertyName()+" is shutting down.");
82         // tell app to finalize its session data before shutdown.
83       }
84     });
85     vorbaclient.addVorbaEventHandler(Events.DOCUMENT_FINALIZEAPPDATA, 
86         new PropertyChangeListener() {
87       public void propertyChange(PropertyChangeEvent evt) {
88         System.out.println("Application received a DOCUMENT_FINALIZEAPPDATA event.");
89         // tell app to finalize its session data prior to the storage of the current session as an archive.
90       }
91     });
92     
93   }
94   public static String 
95         Usage="ExampleApplication <vamsasFileDirectory> <vamsasSessionURN> <action> [+<arguments>]\n"
96           +"<action> is one of :\n\tsave,update,close,watch";
97   
98   private static boolean parseArgs(String args[]) {
99     return true; // incorrect arguments.
100   }
101   public static void main(String[] args) {
102     if ((args.length<=2) || parseArgs(args)) {
103       System.err.print(Usage);
104     }
105
106     // get IClientFactory
107     try {
108       clientfactory = new org.vamsas.client.SimpleClientFactory(args[0]);
109     } catch (IOException e) {
110       System.err.println(e+"\n"+Usage);
111       System.exit(1);
112     }
113     
114     // get an Iclient with session data
115     app = new ClientHandle("org.vamsas.test.ExampleApplication","0.1");
116     user = new UserHandle("arnolduser","deathsdoor");
117     vorbaclient = clientfactory.getIClient(app, user);
118     addHandlers(vorbaclient);
119     try {
120       vorbaclient.joinSession();
121     }
122     catch (Exception se) {
123       se.printStackTrace();
124       System.err.println(se+" when joining session.\n"+Usage);
125       System.exit(1);
126     }
127     // register an update listener and a close listener.
128     // get document data
129     processVamsasDocument(vorbaclient.getClientDocument());
130
131     // Main application event loop - wait around and do stuff...
132     while (!isShuttingdown) {
133       // do something with data
134       // , update document, or something.
135       // ..
136       
137     }
138     // call finalizeClient
139     vorbaclient.finalizeClient();
140     // { meanwhile, eventHandlers are called to do any saves if need be }
141     // and all registered listeners will be deregistered to avoid deadlock.
142     
143     // finish
144   }
145 }