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