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.stateful;
017:
018: import java.rmi.RemoteException;
019: import java.util.Hashtable;
020: import java.util.Properties;
021:
022: import javax.ejb.EJBException;
023: import javax.ejb.SessionContext;
024:
025: import org.apache.openejb.test.ApplicationException;
026: import org.apache.openejb.test.object.OperationsPolicy;
027:
028: /**
029: * A Stateful SessionBean with bean-managed transaction demarcation
030: *
031: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
032: * @author <a href="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
033: */
034: public class BMTStatefulBean implements javax.ejb.SessionBean {
035:
036: private String name;
037: private SessionContext ejbContext;
038: private Hashtable allowedOperationsTable = new Hashtable();
039:
040: //=============================
041: // Home interface methods
042: //
043: /**
044: * Maps to BasicStatefulHome.create
045: *
046: * @param name
047: * @exception javax.ejb.CreateException
048: * @see BasicStatefulHome#createObject
049: */
050: public void ejbCreateObject(String name)
051: throws javax.ejb.CreateException {
052: testAllowedOperations("ejbCreate");
053: this .name = name;
054: }
055:
056: //
057: // Home interface methods
058: //=============================
059:
060: //=============================
061: // Remote interface methods
062: //
063:
064: /**
065: * Maps to BasicStatefulObject.businessMethod
066: *
067: * @return
068: * @see BasicStatefulObject#businessMethod
069: */
070: public String businessMethod(String text) {
071: testAllowedOperations("businessMethod");
072: StringBuffer b = new StringBuffer(text);
073: return b.reverse().toString();
074: }
075:
076: /**
077: * Throws an ApplicationException when invoked
078: *
079: */
080: public void throwApplicationException() throws ApplicationException {
081: throw new ApplicationException("Don't Panic");
082: }
083:
084: /**
085: * Throws a java.lang.NullPointerException when invoked
086: * This is a system exception and should result in the
087: * destruction of the instance and invalidation of the
088: * remote reference.
089: *
090: */
091: public void throwSystemException_NullPointer() {
092: throw new NullPointerException("Panic");
093: }
094:
095: /**
096: * Maps to BasicStatefulObject.getPermissionsReport
097: *
098: * Returns a report of the bean's
099: * runtime permissions
100: *
101: * @return
102: * @see BasicStatefulObject#getPermissionsReport
103: */
104: public Properties getPermissionsReport() {
105: /* TO DO: */
106: return null;
107: }
108:
109: /**
110: * Maps to BasicStatefulObject.getAllowedOperationsReport
111: *
112: * Returns a report of the allowed opperations
113: * for one of the bean's methods.
114: *
115: * @param methodName The method for which to get the allowed opperations report
116: * @return
117: * @see BasicStatefulObject#getAllowedOperationsReport
118: */
119: public OperationsPolicy getAllowedOperationsReport(String methodName) {
120: return (OperationsPolicy) allowedOperationsTable
121: .get(methodName);
122: }
123:
124: public String remove(String arg) {
125: return arg;
126: }
127:
128: //
129: // Remote interface methods
130: //=============================
131:
132: //=================================
133: // SessionBean interface methods
134: //
135: /**
136: * Set the associated session context. The container calls this method
137: * after the instance creation.
138: */
139: public void setSessionContext(SessionContext ctx)
140: throws EJBException, RemoteException {
141: ejbContext = ctx;
142: testAllowedOperations("setSessionContext");
143: }
144:
145: /**
146: * A container invokes this method before it ends the life of the session
147: * object. This happens as a result of a client's invoking a remove
148: * operation, or when a container decides to terminate the session object
149: * after a timeout.
150: */
151: public void ejbRemove() throws EJBException, RemoteException {
152: testAllowedOperations("ejbRemove");
153: }
154:
155: /**
156: * The activate method is called when the instance is activated
157: * from its "passive" state. The instance should acquire any resource
158: * that it has released earlier in the ejbPassivate() method.
159: */
160: public void ejbActivate() throws EJBException, RemoteException {
161: testAllowedOperations("ejbActivate");
162: }
163:
164: /**
165: * The passivate method is called before the instance enters
166: * the "passive" state. The instance should release any resources that
167: * it can re-acquire later in the ejbActivate() method.
168: */
169: public void ejbPassivate() throws EJBException, RemoteException {
170: testAllowedOperations("ejbPassivate");
171: }
172:
173: //
174: // StatefulBean interface methods
175: //==================================
176:
177: protected void testAllowedOperations(String methodName) {
178: OperationsPolicy policy = new OperationsPolicy();
179:
180: /*[1] Test getEJBHome /////////////////*/
181: try {
182: ejbContext.getEJBHome();
183: policy.allow(policy.Context_getEJBHome);
184: } catch (IllegalStateException ise) {
185: }
186:
187: /*[2] Test getCallerPrincipal /////////*/
188: try {
189: ejbContext.getCallerPrincipal();
190: policy.allow(policy.Context_getCallerPrincipal);
191: } catch (IllegalStateException ise) {
192: }
193:
194: /*[3] Test isCallerInRole /////////////*/
195: try {
196: ejbContext.isCallerInRole("TheMan");
197: policy.allow(policy.Context_isCallerInRole);
198: } catch (IllegalStateException ise) {
199: }
200:
201: /*[4] Test getRollbackOnly ////////////*/
202: try {
203: ejbContext.getRollbackOnly();
204: policy.allow(policy.Context_getRollbackOnly);
205: } catch (IllegalStateException ise) {
206: }
207:
208: /*[5] Test setRollbackOnly ////////////*/
209: try {
210: ejbContext.setRollbackOnly();
211: policy.allow(policy.Context_setRollbackOnly);
212: } catch (IllegalStateException ise) {
213: }
214:
215: /*[6] Test getUserTransaction /////////*/
216: try {
217: ejbContext.getUserTransaction();
218: policy.allow(policy.Context_getUserTransaction);
219: } catch (IllegalStateException ise) {
220: }
221:
222: /*[7] Test getEJBObject ///////////////*/
223: try {
224: ejbContext.getEJBObject();
225: policy.allow(policy.Context_getEJBObject);
226: } catch (IllegalStateException ise) {
227: }
228:
229: /* TO DO:
230: * Check for policy.Enterprise_bean_access
231: * Check for policy.JNDI_access_to_java_comp_env
232: * Check for policy.Resource_manager_access
233: */
234: allowedOperationsTable.put(methodName, policy);
235: }
236: }
|