KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > lb > Client


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: Client.java,v 1.2 2004/04/19 06:39:29 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package lb;
27
28 import java.rmi.RemoteException;
29
30 import javax.naming.Context;
31 import javax.naming.InitialContext;
32 import javax.naming.NamingException;
33
34 /**
35  * Client of the lb JOnAS example
36  * @author Helene Joanin
37  */

38 public class Client {
39
40     static Context initialContext = null;
41
42     static ManagerHome home = null;
43
44     static Manager manager = null;
45
46     static boolean m_noinit = false;
47
48     static int m_accounts = 4;
49
50     static int m_valmove = 100;
51
52     static int m_loops = 10;
53
54     static int inival = 1000;
55
56     public static void main(String[] args) {
57
58         // Get command args
59
for (int argn = 0; argn < args.length; argn++) {
60             String s_arg = args[argn];
61             Integer i_arg;
62             if (s_arg.equals("-ni")) {
63                 m_noinit = true;
64             } else if (s_arg.equals("-a")) {
65                 s_arg = args[++argn];
66                 i_arg = java.lang.Integer.valueOf(s_arg);
67                 m_accounts = i_arg.intValue();
68             } else if (s_arg.equals("-m")) {
69                 s_arg = args[++argn];
70                 i_arg = java.lang.Integer.valueOf(s_arg);
71                 m_valmove = i_arg.intValue();
72             } else if (s_arg.equals("-l")) {
73                 s_arg = args[++argn];
74                 i_arg = java.lang.Integer.valueOf(s_arg);
75                 m_loops = i_arg.intValue();
76             } else {
77                 usage();
78                 fatalError("wrong usage");
79             }
80         }
81
82         info("Calling lb.Client with : -a " + m_accounts + " -m " + m_valmove + " -l " + m_loops);
83
84         // Get InitialContext
85
try {
86             initialContext = new InitialContext();
87         } catch (NamingException e) {
88             fatalError("Cannot get InitialContext: " + e);
89         }
90
91         // Create manager session bean
92
try {
93             home = (ManagerHome) javax.rmi.PortableRemoteObject.narrow(initialContext.lookup("ManagerHome"),
94                     ManagerHome.class);
95             manager = home.create(inival);
96         } catch (Exception e) {
97             fatalError("Cannot create manager: " + e);
98         }
99
100         // Create all accounts if noinit==false
101
if (!m_noinit) {
102             info("Re-initialization of the accounts database table");
103             try {
104                 manager.createAll(m_accounts);
105             } catch (Exception e) {
106                 fatalError("Cannot create initial accounts: " + e);
107             }
108         }
109
110         // Set move value
111
try {
112             manager.setValue(m_valmove);
113         } catch (Exception e) {
114             fatalError("Cannot init Session: " + e);
115         }
116
117         // main loop
118
try {
119             for (int i = 0; i < m_loops; i++) {
120                 // Choose the 2 accounts randomly
121
int d1 = random(m_accounts);
122                 int c1 = random(m_accounts);
123                 info(" Movement " + d1 + " -> " + c1 + "");
124                 // Set these accounts in session bean
125
manager.setAccounts(d1, c1);
126                 // make the transfert
127
try {
128                     manager.movement();
129                 } catch (RemoteException r) {
130                     error("movement raised exception. ignoring...");
131                 }
132                 // Check account that was debited
133
if (manager.checkAccount(d1) == false) {
134                     error("Bad Account after move on account " + d1);
135                     error("Stopping this session because some accounts are not ok");
136                     break;
137                 }
138             }
139         } catch (Exception e) {
140             fatalError("Exception in main loop :" + e);
141         }
142
143         // Check all accounts
144
try {
145             if (manager.checkAll() == false) {
146                 error("FAIL when checking all accounts");
147             } else {
148                 info("PASS when checking all accounts");
149             }
150         } catch (RemoteException e) {
151             error("checkAll() :" + e);
152         }
153
154         // remove session bean
155
try {
156             manager.remove();
157         } catch (Exception e) {
158             error("Cannot remove manager session: " + e);
159         }
160
161     }
162
163     /**
164      * Returns an integer between 0 and max-1
165      */

166     static int random(int max) {
167
168         double d = Math.random();
169         int ret = (int) (max * d);
170         return ret;
171     }
172
173     /**
174      * Display the usage
175      */

176     static void usage() {
177         info("lb.Client [-ni] [-a accounts] [-m value] [-l loops]");
178     }
179
180     /**
181      * Display the given information message
182      */

183     static void info(String infoMsg) {
184         System.out.println(infoMsg);
185     }
186
187     /**
188      * Display the given error message
189      */

190     static void error(String errMsg) {
191         System.out.println("lb.Client error: " + errMsg);
192     }
193
194     /**
195      * Display the given error message and exits
196      */

197     static void fatalError(String errMsg) {
198         System.out.println("lb.Client fatal error: " + errMsg);
199         System.exit(2);
200     }
201
202 }
203
204
Popular Tags