001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: Client.java 4618 2004-04-19 06:39:30Z benoitf $
023: * --------------------------------------------------------------------------
024: */package lb;
025:
026: import java.rmi.RemoteException;
027:
028: import javax.naming.Context;
029: import javax.naming.InitialContext;
030: import javax.naming.NamingException;
031:
032: /**
033: * Client of the lb JOnAS example
034: * @author Helene Joanin
035: */
036: public class Client {
037:
038: static Context initialContext = null;
039:
040: static ManagerHome home = null;
041:
042: static Manager manager = null;
043:
044: static boolean m_noinit = false;
045:
046: static int m_accounts = 4;
047:
048: static int m_valmove = 100;
049:
050: static int m_loops = 10;
051:
052: static int inival = 1000;
053:
054: public static void main(String[] args) {
055:
056: // Get command args
057: for (int argn = 0; argn < args.length; argn++) {
058: String s_arg = args[argn];
059: Integer i_arg;
060: if (s_arg.equals("-ni")) {
061: m_noinit = true;
062: } else if (s_arg.equals("-a")) {
063: s_arg = args[++argn];
064: i_arg = java.lang.Integer.valueOf(s_arg);
065: m_accounts = i_arg.intValue();
066: } else if (s_arg.equals("-m")) {
067: s_arg = args[++argn];
068: i_arg = java.lang.Integer.valueOf(s_arg);
069: m_valmove = i_arg.intValue();
070: } else if (s_arg.equals("-l")) {
071: s_arg = args[++argn];
072: i_arg = java.lang.Integer.valueOf(s_arg);
073: m_loops = i_arg.intValue();
074: } else {
075: usage();
076: fatalError("wrong usage");
077: }
078: }
079:
080: info("Calling lb.Client with : -a " + m_accounts + " -m "
081: + m_valmove + " -l " + m_loops);
082:
083: // Get InitialContext
084: try {
085: initialContext = new InitialContext();
086: } catch (NamingException e) {
087: fatalError("Cannot get InitialContext: " + e);
088: }
089:
090: // Create manager session bean
091: try {
092: home = (ManagerHome) javax.rmi.PortableRemoteObject.narrow(
093: initialContext.lookup("ManagerHome"),
094: ManagerHome.class);
095: manager = home.create(inival);
096: } catch (Exception e) {
097: fatalError("Cannot create manager: " + e);
098: }
099:
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: }
|