refactoring org to uk - never use f**ng eclipse
[vamsas.git] / src / uk / ac / 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 uk.ac.vamsas.test;
8
9 import uk.ac.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; // TODO: make this something defined by the api
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 uk.ac.vamsas.client.simpleclient.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("uk.ac.vamsas.test.ExampleApplication","0.1");
116     user = new UserHandle("arnolduser","deathsdoor");
117     try {
118       vorbaclient = clientfactory.getIClient(app, user);
119     } catch (NoDefaultSessionException e) {
120       System.err.println("There appear to be several sessions to choose from :");
121       String[] sessions = clientfactory.getCurrentSessions();
122       for (int s=0;s<sessions.length; s++)
123         System.err.println(sessions[s]);
124       System.exit(2);
125     }
126     addHandlers(vorbaclient);
127     try {
128       vorbaclient.joinSession();
129     }
130     catch (Exception se) {
131       se.printStackTrace();
132       System.err.println(se+" when joining session.\n"+Usage);
133       System.exit(1);
134     }
135     // register an update listener and a close listener.
136     // get document data
137     try {
138       IClientDocument cdoc = vorbaclient.getClientDocument();
139       processVamsasDocument(cdoc);
140     } catch (Exception e) {
141       System.err.println("Unexpected exception when retrieving the client document for the first time!");
142       e.printStackTrace(System.err);
143       System.exit(1);
144     }
145
146     // Main application event loop - wait around and do stuff...
147     while (!isShuttingdown) {
148       // do something with data
149       // , update document, or something.
150       // ..
151       
152     }
153     // call finalizeClient
154     vorbaclient.finalizeClient();
155     // { meanwhile, eventHandlers are called to do any saves if need be }
156     // and all registered listeners will be deregistered to avoid deadlock.
157     
158     // finish
159   }
160 }