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