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: * Initial developer: Florent Benoit
022: *
023: * --------------------------------------------------------------------------
024: * $Id: Client.java 4618 2004-04-19 06:39:30Z benoitf $
025: * --------------------------------------------------------------------------
026: */package org.objectweb.earsample.clients;
027:
028: import java.net.URL;
029:
030: import javax.naming.Context;
031: import javax.naming.InitialContext;
032: import javax.transaction.UserTransaction;
033: import javax.rmi.PortableRemoteObject;
034:
035: import org.objectweb.earsample.beans.secusb.Op;
036: import org.objectweb.earsample.beans.secusb.OpHome;
037:
038: /**
039: * Heavy client doing some operations on a bean
040: * @author jonas team
041: */
042: public class Client {
043:
044: /**
045: * First amount to buy
046: */
047: private static final int FIRST_BUY_AMOUNT = 10;
048:
049: /**
050: * Second amount to buy
051: */
052: private static final int SECOND_BUY_AMOUNT = 20;
053:
054: /**
055: * Third amount to buy (will be rollback)
056: */
057: private static final int THIRD_BUY_AMOUNT = 50;
058:
059: /**
060: * Constructor. Hide constructor as it is an utility class
061: */
062: private Client() {
063:
064: }
065:
066: /**
067: * Main method
068: * @param args arguments of the client
069: */
070: public static void main(String[] args) {
071:
072: Context initialContext = null;
073: System.out.print("Building a new InitialContext...");
074: try {
075: initialContext = new InitialContext();
076: } catch (Exception e) {
077: System.err.println("Cannot get initial context for JNDI: "
078: + e);
079: System.exit(2);
080: }
081: System.out.println("done !");
082:
083: System.out.print("Looking up java:comp/UserTransaction ...");
084: // We want to start transactions from client: get UserTransaction
085: UserTransaction utx = null;
086: try {
087: utx = (UserTransaction) initialContext
088: .lookup("java:comp/UserTransaction");
089: } catch (Exception e) {
090: System.err.println("Cannot lookup UserTransaction: " + e);
091: System.exit(2);
092: }
093: System.out.println("done !");
094:
095: String envEntry = null;
096: System.out.print("Looking up java:comp/env/envEntryString ...");
097: try {
098: envEntry = (String) initialContext
099: .lookup("java:comp/env/envEntryString");
100: } catch (Exception e) {
101: System.err.println("Cannot get env-entry on JNDI " + e);
102: System.exit(2);
103: }
104: System.out.println("done !");
105: System.out.println("Env entry is : " + envEntry);
106:
107: URL url = null;
108: System.out.print("Looking up java:comp/env/url/jonas ...");
109: try {
110: url = (URL) initialContext
111: .lookup("java:comp/env/url/jonas");
112: } catch (Exception e) {
113: System.err.println("Cannot get URL on JNDI " + e);
114: System.exit(2);
115: }
116: System.out.println("done !");
117: System.out.println("Web site of jonas is at :" + url);
118:
119: System.out.print("Looking up java:comp/env/ejb/Op ...");
120: // Connecting to OpHome thru JNDI
121: OpHome home = null;
122: try {
123: home = (OpHome) PortableRemoteObject.narrow(initialContext
124: .lookup("java:comp/env/ejb/Op"), OpHome.class);
125: System.out.println("done !");
126: } catch (Exception e) {
127: e.printStackTrace();
128: System.err.println("Cannot lookup OpHome: " + e);
129: System.exit(2);
130: }
131:
132: // OpBean creation
133: Op t1 = null;
134: try {
135: System.out.println("Create a bean");
136: t1 = home.create("User1");
137: } catch (Exception e) {
138: System.err.println("Cannot create OpBean: " + e);
139: System.exit(2);
140: }
141:
142: // First transaction (committed)
143: try {
144: System.out.println("Start a first transaction");
145: utx.begin();
146: System.out.println("First request on the new bean");
147: t1.buy(FIRST_BUY_AMOUNT);
148: System.out.println("Second request on the bean");
149: t1.buy(SECOND_BUY_AMOUNT);
150: System.out.println("Commit the transaction");
151: utx.commit();
152: } catch (Exception e) {
153: System.err.println("exception during 1st Tx: " + e);
154: System.exit(2);
155: }
156: // Start another transaction (rolled back)
157: try {
158: System.out.println("Start a second transaction");
159: utx.begin();
160: t1.buy(THIRD_BUY_AMOUNT);
161: System.out.println("Rollback the transaction");
162: utx.rollback();
163: } catch (Exception e) {
164: System.err.println("exception during 2nd Tx: " + e);
165: System.exit(2);
166: }
167:
168: // Get the total bought, outside the transaction
169: int val = 0;
170: try {
171: System.out.println("Request outside any transaction");
172: val = t1.read();
173: } catch (Exception e) {
174: System.err.println("Cannot read value on t1 : " + e);
175: System.exit(2);
176: }
177: if (val != FIRST_BUY_AMOUNT + SECOND_BUY_AMOUNT) {
178: System.err.println("Bad value read: " + val);
179: System.exit(2);
180: }
181:
182: // Remove Session bean
183: try {
184: t1.remove();
185: } catch (Exception e) {
186: System.out.println("Exception on buy: " + e);
187: System.exit(2);
188: }
189: System.out.println("ClientOp OK. Exiting.");
190: }
191: }
|