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.SessionContext;
019: import javax.naming.InitialContext;
020: import java.sql.Connection;
021: import java.sql.ResultSet;
022: import java.sql.SQLException;
023: import java.sql.Statement;
024:
025: public class CalculatorBean implements javax.ejb.SessionBean {
026:
027: public SessionContext context;
028: public InitialContext jndiContext;
029: boolean testCreate, testAdd, testSub, testSetSessionContext,
030: testRemove;
031:
032: public void ejbCreate() {
033: }
034:
035: public int add(int a, int b) {
036: return a + b;
037: }
038:
039: protected void doJdbcCall() {
040:
041: Connection con = null;
042: try {
043:
044: javax.sql.DataSource ds = (javax.sql.DataSource) jndiContext
045: .lookup("java:comp/env/jdbc/mydb");
046:
047: con = ds.getConnection();
048:
049: Statement stmt = con.createStatement();
050: try {
051: ResultSet rs = stmt
052: .executeQuery("select * from Employees");
053: while (rs.next())
054: System.out.println(rs.getString(2));
055: } finally {
056: stmt.close();
057: }
058:
059: } catch (javax.naming.NamingException re) {
060: throw new RuntimeException("Using JNDI failed");
061: } catch (java.sql.SQLException se) {
062: throw new RuntimeException(
063: "Getting JDBC data source failed");
064: } finally {
065: if (con != null) {
066: try {
067: con.close();
068: } catch (SQLException se) {
069: se.printStackTrace();
070: }
071: }
072: }
073:
074: }
075:
076: public int sub(int a, int b) {
077: return a - b;
078: }
079:
080: public void ejbPassivate() {
081: // never called
082: }
083:
084: public void ejbActivate() {
085: // never called
086: }
087:
088: public void ejbRemove() {
089: if (testRemove)
090: testAllowedOperations("ejbRemove");
091: }
092:
093: public void setSessionContext(javax.ejb.SessionContext cntx) {
094: context = cntx;
095: if (testSetSessionContext)
096: testAllowedOperations("setSessionContext");
097:
098: }
099:
100: private void testAllowedOperations(String methodName) {
101: System.out
102: .println("******************************************************");
103: System.out.println("\nTesting Allowed Operations for "
104: + methodName + "() method\n");
105: try {
106: context.getEJBObject();
107: System.out
108: .println("SessionContext.getEJBObject() ......... Allowed");
109: } catch (IllegalStateException ise) {
110: System.out
111: .println("SessionContext.getEJBObject() ......... Failed");
112: }
113: try {
114: context.getEJBHome();
115: System.out
116: .println("SessionContext.getEJBHome() ........... Allowed");
117: } catch (IllegalStateException ise) {
118: System.out
119: .println("SessionContext.getEJBHome() ........... Failed");
120: }
121: try {
122: context.getCallerPrincipal();
123: System.out
124: .println("SessionContext.getCallerPrincipal() ... Allowed");
125: } catch (IllegalStateException ise) {
126: System.out
127: .println("SessionContext.getCallerPrincipal() ... Failed");
128: }
129: try {
130: context.isCallerInRole("ROLE");
131: System.out
132: .println("SessionContext.isCallerInRole() ....... Allowed");
133: } catch (IllegalStateException ise) {
134: System.out
135: .println("SessionContext.isCallerInRole() ....... Failed");
136: }
137: try {
138: context.getRollbackOnly();
139: System.out
140: .println("SessionContext.getRollbackOnly() ...... Allowed");
141: } catch (IllegalStateException ise) {
142: System.out
143: .println("SessionContext.getRollbackOnly() ...... Failed");
144: }
145: try {
146: context.setRollbackOnly();
147: System.out
148: .println("SessionContext.setRollbackOnly() ...... Allowed");
149: } catch (IllegalStateException ise) {
150: System.out
151: .println("SessionContext.setRollbackOnly() ...... Failed");
152: }
153: try {
154: context.getUserTransaction();
155: System.out
156: .println("SessionContext.getUserTransaction() ... Allowed");
157: } catch (IllegalStateException ise) {
158: System.out
159: .println("SessionContext.getUserTransaction() ... Failed");
160: }
161: }
162:
163: }
|