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