KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > pss > demo1 > Client


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA & USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Christophe Demarey.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.pss.demo1;
28
29 /**
30  * This is the client application to access Form.
31  *
32  * @author <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</a>
33  *
34  * @version 0.1
35  */

36 public class Client
37 {
38     // ==================================================================
39
//
40
// Internal state.
41
//
42
// ==================================================================
43

44     // Reference to the remote object
45
private static Form form = null;
46
47     // ==================================================================
48
//
49
// Constructor.
50
//
51
// ==================================================================
52

53     // ==================================================================
54
//
55
// Internal methods.
56
//
57
// ==================================================================
58

59     // ==================================================================
60
//
61
// Public methods.
62
//
63
// ==================================================================
64

65     /**
66      * The application entry point.
67      *
68      * @param args - Application arguments.
69      */

70     public static void
71     main( String JavaDoc args[] )
72     {
73         String JavaDoc ns_name = null;
74
75         // Init the ORB.
76
args = org.objectweb.openccm.corba.TheORB.initialize(args);
77
78         // Obtain the ORB.
79
org.omg.CORBA.ORB JavaDoc orb = org.objectweb.openccm.corba.TheORB.getORB();
80
81         // Obtain the Name Service.
82
System.out.println("Obtaining the Name Service...");
83         org.omg.CORBA.Object JavaDoc obj = null;
84         try {
85             obj = orb.resolve_initial_references("NameService");
86         } catch(org.omg.CORBA.ORBPackage.InvalidName JavaDoc ex) {
87             ex.printStackTrace();
88         }
89         org.omg.CosNaming.NamingContext JavaDoc nc = org.omg.CosNaming.NamingContextHelper.narrow(obj);
90
91
92         // Resolving the pss::demo1::form instance
93
ns_name = "My_PSS_demo1_form";
94         System.out.println("Obtaining the " + ns_name + " Form instance...");
95         org.omg.CosNaming.NameComponent JavaDoc[] ncomp = new org.omg.CosNaming.NameComponent JavaDoc[1];
96         ncomp[0] = new org.omg.CosNaming.NameComponent JavaDoc(ns_name, "");
97         try {
98             obj = nc.resolve(ncomp);
99         } catch(org.omg.CosNaming.NamingContextPackage.NotFound JavaDoc ex1) {
100             ex1.printStackTrace();
101         } catch(org.omg.CosNaming.NamingContextPackage.CannotProceed JavaDoc ex2) {
102             ex2.printStackTrace();
103         } catch(org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc ex3){
104             ex3.printStackTrace();
105         }
106
107         // Narrow the object reference
108
form = FormHelper.narrow( obj );
109
110         int option = 0;
111
112         boolean stop = false;
113
114         while ( !stop )
115         {
116             option = menu();
117
118             switch ( option )
119             {
120
121             case 0 :
122                 stop = true;
123                 System.exit(0);
124                 break;
125
126             case 1 :
127                 createPerson();
128                 break;
129
130             case 2 :
131                 removePerson();
132                 break;
133
134             case 3 :
135                 displayPerson();
136                 break;
137             }
138         }
139
140     }
141
142     /**
143      * Create a new person
144      */

145     public static void
146     createPerson()
147     {
148         try
149         {
150             System.out.println( "[ Create a new person ]" );
151             System.out.print( "First Name : " );
152             String JavaDoc first_name = input();
153
154             System.out.print( "Last Name : " );
155             String JavaDoc last_name = input();
156
157             System.out.print( "Age : " );
158             int age = new Integer JavaDoc( input() ).intValue();
159
160             System.out.print( "Address Line1 : " );
161             String JavaDoc address_line1 = input();
162
163             System.out.print( "Address Line2 : " );
164             String JavaDoc address_line2 = input();
165
166             System.out.print( "Address City : " );
167             String JavaDoc address_city = input();
168
169             System.out.print( "Address Zip : " );
170             String JavaDoc address_zip = input();
171
172             System.out.println( "" );
173
174             form.createPerson( first_name, last_name, age,
175                                address_line1, address_line2, address_city, address_zip );
176         }
177         catch ( org.omg.CORBA.SystemException JavaDoc ex )
178         {
179             System.out.println( "A CORBA System exception has been intercepted" );
180             ex.printStackTrace();
181         }
182     }
183
184     /**
185      * This operation is used to remove a person.
186      */

187     public static void
188     removePerson()
189     {
190         try
191         {
192             System.out.println( "[ Remove a person ]" );
193             System.out.print( "First Name : " );
194             String JavaDoc first_name = input();
195
196             System.out.print( "Last Name : " );
197             String JavaDoc last_name = input();
198
199             System.out.println( "" );
200
201             form.removePerson( first_name, last_name );
202         }
203         catch ( org.omg.CORBA.SystemException JavaDoc ex )
204         {
205             System.out.println( "A CORBA System exception has been intercepted" );
206             ex.printStackTrace();
207         }
208     }
209
210     /**
211      * This operation is used to display person's data.
212      */

213     public static void
214     displayPerson()
215     {
216         try
217         {
218             System.out.println( "[ Display a person ]" );
219             System.out.print( "First Name : " );
220             String JavaDoc first_name = input();
221
222             System.out.print( "Last Name : " );
223             String JavaDoc last_name = input();
224
225             System.out.println( "" );
226
227             System.out.print( form.showPerson(first_name, last_name) );
228         }
229         catch ( org.omg.CORBA.SystemException JavaDoc ex )
230         {
231             System.out.println( "A CORBA System exception has been intercepted" );
232             ex.printStackTrace();
233         }
234     }
235
236     /**
237      * Read input from the keyboard.
238      *
239      * @return The string read.
240      */

241     public static String JavaDoc
242     input()
243     {
244         try
245         {
246             java.io.BufferedReader JavaDoc reader = new java.io.BufferedReader JavaDoc( new java.io.InputStreamReader JavaDoc( System.in ) );
247
248             return reader.readLine();
249         }
250         catch ( java.lang.Exception JavaDoc ex )
251         {
252             ex.printStackTrace();
253         }
254
255         return null;
256     }
257
258     /**
259      * Display the menu
260      *
261      * @return The menu identifier.
262      */

263     public static int menu()
264     {
265         System.out.println( "Select a choice : " );
266         System.out.println( "\t0. Quit" );
267         System.out.println( "\t1. Create a new person" );
268         System.out.println( "\t2. Remove a person" );
269         System.out.println( "\t3. Display all person's data" );
270
271         String JavaDoc choice = input();
272
273         return new Integer JavaDoc( choice ).intValue();
274     }
275 }
276
Popular Tags