quick update
[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   }
86   public static String 
87         Usage="ExampleApplication <vamsasFileDirectory> <vamsasSessionURN> <action> [+<arguments>]\n"
88           +"<action> is one of :\n\tsave,update,close,watch";
89   
90   private static boolean parseArgs(String args[]) {
91     return true; // incorrect arguments.
92   }
93   public static void main(String[] args) {
94     if ((args.length<=2) || parseArgs(args)) {
95       System.err.print(Usage);
96     }
97
98     // get IClientFactory
99     try {
100       clientfactory = new org.vamsas.client.SimpleClientFactory(args[0]);
101     } catch (IOException e) {
102       System.err.println(e+"\n"+Usage);
103       System.exit(1);
104     }
105     
106     // get an Iclient with session data
107     app = new ClientHandle("org.vamsas.test.ExampleApplication","0.1");
108     user = new UserHandle("arnolduser","deathsdoor");
109     vorbaclient = clientfactory.getIClient(app, user);
110     addHandlers(vorbaclient);
111     vorbaclient.joinSession();
112     // register an update listener and a close listener.
113     // get document data
114     processVamsasDocument(vorbaclient.getClientDocument());
115
116     // Main application event loop - wait around and do stuff...
117     while (!isShuttingdown) {
118       // do something with data
119       // , update document, or something.
120       // ..
121       
122     }
123     // call finalizeClient
124     vorbaclient.finalizeClient();
125     // { meanwhile, eventHandlers are called to do any saves if need be }
126     // and all registered listeners will be deregistered to avoid deadlock.
127     
128     // finish
129   }
130 }