KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jaasclient > ClientJAASOpContClientSwing


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): jonas team
22  *
23  * --------------------------------------------------------------------------
24  * $Id: ClientJAASOpContClientSwing.java,v 1.2 2004/04/19 06:39:29 benoitf Exp $
25  * --------------------------------------------------------------------------
26  */

27
28 package jaasclient;
29
30 import jaasclient.beans.secusb.JAASOp;
31 import jaasclient.beans.secusb.JAASOpHome;
32
33 import java.awt.event.ActionEvent;
34
35 import javax.naming.Context;
36 import javax.naming.InitialContext;
37 import javax.rmi.PortableRemoteObject;
38 import javax.swing.AbstractAction;
39 import javax.swing.Action;
40 import javax.swing.JButton;
41 import javax.swing.JFrame;
42 import javax.swing.WindowConstants;
43 import javax.transaction.UserTransaction;
44
45 /**
46  * Sample for Session Bean.
47  * @author jonas team
48  * @author Florent Benoit
49  * @author Markus Karg : Add Swing interface
50  */

51 public class ClientJAASOpContClientSwing {
52
53     /**
54      * First amount to buy
55      */

56     private static final int FIRST_BUY_AMOUNT = 10;
57
58     /**
59      * Second amount to buy
60      */

61     private static final int SECOND_BUY_AMOUNT = 20;
62
63     /**
64      * Third amount to buy (will be rollback)
65      */

66     private static final int THIRD_BUY_AMOUNT = 50;
67
68     /**
69      * Constructor. Hide constructor as it is an utility class
70      */

71     private ClientJAASOpContClientSwing() {
72
73     }
74
75     /**
76      * Main method
77      * @param args the arguments
78      */

79     public static void main(String[] args) {
80
81
82         final JFrame jf = new JFrame("Test Application");
83         jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
84         final JButton jb = new JButton();
85         final Action a = new AbstractAction("Click here to test!") {
86                 public final void actionPerformed(final ActionEvent v) {
87
88
89                     Context initialContext = null;
90                     try {
91                         initialContext = new InitialContext();
92                     } catch (Exception e) {
93                         System.err.println("Cannot get initial context for JNDI: " + e);
94                         System.exit(2);
95                     }
96
97                     // We want to start transactions from client: get UserTransaction
98
UserTransaction utx = null;
99                     try {
100                         utx = (UserTransaction) initialContext.lookup("java:comp/UserTransaction");
101                     } catch (Exception e) {
102                         System.err.println("Cannot lookup java:comp/UserTransaction: " + e);
103                         System.exit(2);
104                     }
105
106                     // Connecting to JAASOpHome thru JNDI
107
JAASOpHome home = null;
108                     try {
109                         home = (JAASOpHome) PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/JAASOp"), JAASOpHome.class);
110                     } catch (Exception e) {
111                         System.err.println("Cannot lookup java:comp/env/ejb/JAASOp: " + e + ". Maybe you haven't do the 'jonas admin -a jaasop.jar'");
112                         System.exit(2);
113                     }
114
115                     // JAASOpBean creation
116
JAASOp t1 = null;
117                     try {
118                         System.out.println("Create a bean");
119                         t1 = home.create("User1");
120                     } catch (Exception e) {
121                         System.err.println("Cannot create JAASOpBean: " + e);
122                         System.exit(2);
123                     }
124
125                     // First transaction (committed)
126
try {
127                         System.out.println("Start a first transaction");
128                         utx.begin();
129                         System.out.println("First request on the new bean");
130                         t1.buy(FIRST_BUY_AMOUNT);
131                         System.out.println("Second request on the bean");
132                         t1.buy(SECOND_BUY_AMOUNT);
133                         System.out.println("Commit the transaction");
134                         utx.commit();
135                     } catch (Exception e) {
136                         System.err.println("exception during 1st Tx: " + e);
137                         System.exit(2);
138                     }
139                     // Start another transaction (rolled back)
140
try {
141                         System.out.println("Start a second transaction");
142                         utx.begin();
143                         t1.buy(THIRD_BUY_AMOUNT);
144                         System.out.println("Rollback the transaction");
145                         utx.rollback();
146                     } catch (Exception e) {
147                         System.err.println("exception during 2nd Tx: " + e);
148                         System.exit(2);
149                     }
150
151                     // Get the total bought, outside the transaction
152
int val = 0;
153                     try {
154                         System.out.println("Request outside any transaction");
155                         val = t1.read();
156                     } catch (Exception e) {
157                         System.err.println("Cannot read value on t1 : " + e);
158                         System.exit(2);
159                     }
160                     if (val != FIRST_BUY_AMOUNT + SECOND_BUY_AMOUNT) {
161                         System.err.println("Bad value read: " + val);
162                         System.exit(2);
163                     }
164
165                     // Remove Session bean
166
try {
167                         t1.remove();
168                     } catch (Exception e) {
169                         System.out.println("Exception on buy: " + e);
170                         System.exit(2);
171                     }
172                     System.out.println("ClientJAASOpContClientSwing OK. Exiting.");
173                     System.exit(0);
174
175
176
177                 }
178             };
179         jb.setAction(a);
180         jf.getContentPane().add(jb);
181         jf.pack();
182         jf.setVisible(true);
183     }
184 }
185
Popular Tags