KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > test > beans > ShoppingCartBean


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 1999 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: ShoppingCartBean.java 1096 2004-03-26 21:41:16Z dblevins $
44  */

45 package org.openejb.test.beans;
46
47 import java.sql.Connection JavaDoc;
48 import java.sql.ResultSet JavaDoc;
49 import java.sql.SQLException JavaDoc;
50 import java.sql.Statement JavaDoc;
51
52 import javax.ejb.SessionBean JavaDoc;
53 import javax.ejb.SessionContext JavaDoc;
54 import javax.naming.Context JavaDoc;
55 import javax.naming.InitialContext JavaDoc;
56
57 public class ShoppingCartBean implements SessionBean JavaDoc, javax.ejb.SessionSynchronization JavaDoc{
58     
59     String JavaDoc name;
60     SessionContext JavaDoc context;
61     Context JavaDoc jndiContext;
62     Context JavaDoc envContext;
63     Boolean JavaDoc useJdbc = Boolean.FALSE;
64     
65     public void ejbCreate(String JavaDoc name)throws javax.ejb.CreateException JavaDoc{
66         //testAllowedOperations("ejbCreate");
67
try{
68         
69         jndiContext = new InitialContext JavaDoc();
70         
71         String JavaDoc author = (String JavaDoc)jndiContext.lookup("java:comp/env/author");
72      
73         Double JavaDoc price = (Double JavaDoc)jndiContext.lookup("java:comp/env/price");
74         
75         }catch(javax.naming.NamingException JavaDoc re){
76             throw new RuntimeException JavaDoc("Using JNDI failed");
77         }
78         
79     }
80     public Calculator getCalculator(){
81         
82         try{
83         
84         boolean test = context.isCallerInRole("TheMan");
85         
86         jndiContext = new InitialContext JavaDoc( );
87         
88         CalculatorHome home = (CalculatorHome)jndiContext.lookup("java:comp/env/ejb/calculator");
89         Calculator calc = home.create();
90         return calc;
91         
92         }catch(java.rmi.RemoteException JavaDoc re){
93             throw new RuntimeException JavaDoc("Getting calulator bean failed");
94         }catch(javax.naming.NamingException JavaDoc re){
95             throw new RuntimeException JavaDoc("Using JNDI failed");
96         }
97         
98         
99     }
100     public void doJdbcCall(){
101
102         Connection JavaDoc con = null;
103         try{
104             
105         javax.sql.DataSource JavaDoc ds =
106         (javax.sql.DataSource JavaDoc)jndiContext.lookup("java:comp/env/jdbc/orders");
107         
108         con = ds.getConnection();
109         
110         
111         Statement JavaDoc stmt = con.createStatement();
112         ResultSet JavaDoc rs = stmt.executeQuery("select * from Employees");
113         while(rs.next())
114             System.out.println(rs.getString(2));
115    
116         Calculator calc = getCalculator();
117         calc.add(1, 1);
118         calc.sub(1,2);
119         
120         int i = 1;
121         
122         }catch(java.rmi.RemoteException JavaDoc re){
123             throw new RuntimeException JavaDoc("Accessing Calculator bean failed");
124         }catch(javax.naming.NamingException JavaDoc ne){
125             throw new RuntimeException JavaDoc("Using JNDI failed");
126         }catch(java.sql.SQLException JavaDoc se){
127             throw new RuntimeException JavaDoc("Getting JDBC data source failed");
128         }finally{
129             if(con!=null){
130                 try{
131                     con.close();
132                 }catch(SQLException JavaDoc se){se.printStackTrace();}
133             }
134         }
135         
136     }
137     
138     public String JavaDoc getName( ){
139  
140         return name;
141     }
142     public void setName(String JavaDoc name){
143         //testAllowedOperations("setName");
144
this.name = name;
145     }
146     
147     public void setSessionContext(SessionContext JavaDoc cntx){
148         context = cntx;
149         //testAllowedOperations("setSessionContext");
150
}
151     
152     public void ejbActivate( ){
153         //testAllowedOperations("ejbActivate");
154
}
155     
156     public void ejbPassivate( ){
157         //testAllowedOperations("ejbPassivate");
158
}
159     public void ejbRemove( ){
160         //testAllowedOperations("ejbRemove");
161
}
162     
163     public void afterBegin( ){
164         // do nothing
165
}
166     public void beforeCompletion(){
167         // do nothing
168
}
169     public void afterCompletion(boolean commit){
170         // do nothing
171
}
172     private void testAllowedOperations(String JavaDoc methodName){
173         System.out.println("******************************************************");
174         System.out.println("\nTesting Allowed Operations for "+methodName+"() method\n");
175         try{
176             context.getEJBObject();
177             System.out.println("SessionContext.getEJBObject() ... Allowed");
178         }catch(IllegalStateException JavaDoc ise){
179             System.out.println("SessionContext.getEJBObject() ... Failed");
180         }
181         try{
182             context.getEJBHome();
183             System.out.println("SessionContext.getEJBHome() ... Allowed");
184         }catch(IllegalStateException JavaDoc ise){
185             System.out.println("SessionContext.getEJBHome() ... Failed");
186         }
187         try{
188             context.getCallerPrincipal();
189             System.out.println("SessionContext.getCallerPrincipal() ... Allowed");
190         }catch(IllegalStateException JavaDoc ise){
191             System.out.println("SessionContext.getCallerPrincipal() ... Failed");
192         }
193         try{
194             context.isCallerInRole("ROLE");
195             System.out.println("SessionContext.isCallerInRole() ... Allowed");
196         }catch(IllegalStateException JavaDoc ise){
197             System.out.println("SessionContext.isCallerInRole() ... Failed");
198         }
199         try{
200             context.getRollbackOnly();
201             System.out.println("SessionContext.getRollbackOnly() ... Allowed");
202         }catch(IllegalStateException JavaDoc ise){
203             System.out.println("SessionContext.getRollbackOnly() ... Failed");
204         }
205         try{
206             context.setRollbackOnly();
207             System.out.println("SessionContext.setRollbackOnly() ... Allowed");
208         }catch(IllegalStateException JavaDoc ise){
209             System.out.println("SessionContext.setRollbackOnly() ... Failed");
210         }
211         try{
212             context.getUserTransaction();
213             System.out.println("SessionContext.getUserTransaction() ... Allowed");
214         }catch(IllegalStateException JavaDoc ise){
215             System.out.println("SessionContext.getUserTransaction() ... Failed");
216         }
217     }
218 }
219     
Popular Tags