001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.test.beans;
017:
018: import javax.ejb.SessionBean;
019: import javax.ejb.SessionContext;
020: import javax.naming.Context;
021: import javax.naming.InitialContext;
022: import java.sql.Connection;
023: import java.sql.ResultSet;
024: import java.sql.SQLException;
025: import java.sql.Statement;
026:
027: public class ShoppingCartBean implements SessionBean,
028: javax.ejb.SessionSynchronization {
029:
030: String name;
031: SessionContext context;
032: Context jndiContext;
033: Context envContext;
034: Boolean useJdbc = Boolean.FALSE;
035:
036: public void ejbCreate(String name) throws javax.ejb.CreateException {
037: //testAllowedOperations("ejbCreate");
038: try {
039:
040: jndiContext = new InitialContext();
041:
042: String author = (String) jndiContext
043: .lookup("java:comp/env/author");
044:
045: Double price = (Double) jndiContext
046: .lookup("java:comp/env/price");
047:
048: } catch (javax.naming.NamingException re) {
049: throw new RuntimeException("Using JNDI failed");
050: }
051:
052: }
053:
054: public Calculator getCalculator() {
055:
056: try {
057:
058: boolean test = context.isCallerInRole("TheMan");
059:
060: jndiContext = new InitialContext();
061:
062: CalculatorHome home = (CalculatorHome) jndiContext
063: .lookup("java:comp/env/ejb/calculator");
064: Calculator calc = home.create();
065: return calc;
066:
067: } catch (java.rmi.RemoteException re) {
068: throw new RuntimeException("Getting calulator bean failed");
069: } catch (javax.naming.NamingException re) {
070: throw new RuntimeException("Using JNDI failed");
071: }
072:
073: }
074:
075: public void doJdbcCall() {
076:
077: Connection con = null;
078: try {
079:
080: javax.sql.DataSource ds = (javax.sql.DataSource) jndiContext
081: .lookup("java:comp/env/jdbc/orders");
082:
083: con = ds.getConnection();
084:
085: Statement stmt = con.createStatement();
086: try {
087: ResultSet rs = stmt
088: .executeQuery("select * from Employees");
089: while (rs.next())
090: System.out.println(rs.getString(2));
091:
092: Calculator calc = getCalculator();
093: calc.add(1, 1);
094: calc.sub(1, 2);
095:
096: int i = 1;
097: } finally {
098: stmt.close();
099: }
100:
101: } catch (java.rmi.RemoteException re) {
102: throw new RuntimeException(
103: "Accessing Calculator bean failed");
104: } catch (javax.naming.NamingException ne) {
105: throw new RuntimeException("Using JNDI failed");
106: } catch (java.sql.SQLException se) {
107: throw new RuntimeException(
108: "Getting JDBC data source failed");
109: } finally {
110: if (con != null) {
111: try {
112: con.close();
113: } catch (SQLException se) {
114: se.printStackTrace();
115: }
116: }
117: }
118:
119: }
120:
121: public String getName() {
122:
123: return name;
124: }
125:
126: public void setName(String name) {
127: //testAllowedOperations("setName");
128: this .name = name;
129: }
130:
131: public void setSessionContext(SessionContext cntx) {
132: context = cntx;
133: //testAllowedOperations("setSessionContext");
134: }
135:
136: public void ejbActivate() {
137: //testAllowedOperations("ejbActivate");
138: }
139:
140: public void ejbPassivate() {
141: //testAllowedOperations("ejbPassivate");
142: }
143:
144: public void ejbRemove() {
145: //testAllowedOperations("ejbRemove");
146: }
147:
148: public void afterBegin() {
149: // do nothing
150: }
151:
152: public void beforeCompletion() {
153: // do nothing
154: }
155:
156: public void afterCompletion(boolean commit) {
157: // do nothing
158: }
159:
160: private void testAllowedOperations(String methodName) {
161: System.out
162: .println("******************************************************");
163: System.out.println("\nTesting Allowed Operations for "
164: + methodName + "() method\n");
165: try {
166: context.getEJBObject();
167: System.out
168: .println("SessionContext.getEJBObject() ... Allowed");
169: } catch (IllegalStateException ise) {
170: System.out
171: .println("SessionContext.getEJBObject() ... Failed");
172: }
173: try {
174: context.getEJBHome();
175: System.out
176: .println("SessionContext.getEJBHome() ... Allowed");
177: } catch (IllegalStateException ise) {
178: System.out
179: .println("SessionContext.getEJBHome() ... Failed");
180: }
181: try {
182: context.getCallerPrincipal();
183: System.out
184: .println("SessionContext.getCallerPrincipal() ... Allowed");
185: } catch (IllegalStateException ise) {
186: System.out
187: .println("SessionContext.getCallerPrincipal() ... Failed");
188: }
189: try {
190: context.isCallerInRole("ROLE");
191: System.out
192: .println("SessionContext.isCallerInRole() ... Allowed");
193: } catch (IllegalStateException ise) {
194: System.out
195: .println("SessionContext.isCallerInRole() ... Failed");
196: }
197: try {
198: context.getRollbackOnly();
199: System.out
200: .println("SessionContext.getRollbackOnly() ... Allowed");
201: } catch (IllegalStateException ise) {
202: System.out
203: .println("SessionContext.getRollbackOnly() ... Failed");
204: }
205: try {
206: context.setRollbackOnly();
207: System.out
208: .println("SessionContext.setRollbackOnly() ... Allowed");
209: } catch (IllegalStateException ise) {
210: System.out
211: .println("SessionContext.setRollbackOnly() ... Failed");
212: }
213: try {
214: context.getUserTransaction();
215: System.out
216: .println("SessionContext.getUserTransaction() ... Allowed");
217: } catch (IllegalStateException ise) {
218: System.out
219: .println("SessionContext.getUserTransaction() ... Failed");
220: }
221: }
222: }
|