more comments.
[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.util.Vector;
15 /**
16  * Toy vamsas command line client application demonstrating the API.
17  * 
18  * @author jimp
19  */
20
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.
35   }
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...
46       }
47     });
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"));
56         }
57     });
58     
59     // register some more handlers to monitor the session :
60     
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.
67       }
68     });
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.
75       }
76     });
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.
82       }
83     });        
84   }
85   public static String 
86         Usage="ExampleApplication <vamsasFileDirectory> <vamsasSessionURN> <action> [+<arguments>]\n"
87           +"<action> is one of :\n\tsave,update,close,watch";
88   
89   private static boolean parseArgs(String args[]) {
90     return true; // incorrect arguments.
91   }
92   public static void main(String[] args) {
93     if ((args.length<=2) || parseArgs(args)) {
94       System.err.print(Usage);
95     }
96
97     // get IClientFactory
98     clientfactory = new org.vamsas.client.SimpleClientFactory(args[0]);
99     
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.
106     // get document data
107     processVamsasDocument(vorbaclient.getClientDocument());
108
109     // Main application event loop - wait around and do stuff...
110     while (!isShuttingdown) {
111       // do something with data
112       // , update document, or something.
113       // ..
114       
115     }
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.
120     
121     // finish
122   }
123 }