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: import javax.ejb.SessionSynchronization;
025:
026: import org.apache.openejb.test.ApplicationException;
027: import org.apache.openejb.test.object.OperationsPolicy;
028:
029: /**
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 BasicStatefulBean implements javax.ejb.SessionBean,
035: SessionSynchronization {
036:
037: private String name;
038: private SessionContext ejbContext;
039: private Hashtable allowedOperationsTable = new Hashtable();
040:
041: //=============================
042: // Home interface methods
043: //
044: /**
045: * Maps to BasicStatefulHome.create
046: *
047: * @param name
048: * @exception javax.ejb.CreateException
049: * @see BasicStatefulHome#createObject
050: */
051: public void ejbCreateObject(String name)
052: throws javax.ejb.CreateException {
053: testAllowedOperations("ejbCreate");
054: this .name = name;
055: }
056:
057: //
058: // Home interface methods
059: //=============================
060:
061: //=============================
062: // Remote interface methods
063: //
064:
065: /**
066: * Maps to BasicStatefulObject.businessMethod
067: *
068: * @return
069: * @see BasicStatefulObject#businessMethod
070: */
071: public String businessMethod(String text) {
072: testAllowedOperations("businessMethod");
073: StringBuffer b = new StringBuffer(text);
074: return b.reverse().toString();
075: }
076:
077: /**
078: * Throws an ApplicationException when invoked
079: *
080: */
081: public void throwApplicationException() throws ApplicationException {
082: throw new ApplicationException(
083: "Testing ability to throw Application Exceptions");
084: }
085:
086: /**
087: * Throws a java.lang.NullPointerException when invoked
088: * This is a system exception and should result in the
089: * destruction of the instance and invalidation of the
090: * remote reference.
091: *
092: */
093: public void throwSystemException_NullPointer() {
094: throw new NullPointerException(
095: "Testing ability to throw System Exceptions");
096: }
097:
098: /**
099: * Maps to BasicStatefulObject.getPermissionsReport
100: *
101: * Returns a report of the bean's
102: * runtime permissions
103: *
104: * @return
105: * @see BasicStatefulObject#getPermissionsReport
106: */
107: public Properties getPermissionsReport() {
108: /* TO DO: */
109: return null;
110: }
111:
112: /**
113: * Maps to BasicStatefulObject.getAllowedOperationsReport
114: *
115: * Returns a report of the allowed opperations
116: * for one of the bean's methods.
117: *
118: * @param methodName The method for which to get the allowed opperations report
119: * @return
120: * @see BasicStatefulObject#getAllowedOperationsReport
121: */
122: public OperationsPolicy getAllowedOperationsReport(String methodName) {
123: return (OperationsPolicy) allowedOperationsTable
124: .get(methodName);
125: }
126:
127: public String remove(String str) {
128: return str;
129: }
130:
131: //
132: // Remote interface methods
133: //=============================
134:
135: //=================================
136: // SessionBean interface methods
137: //
138: /**
139: * Set the associated session context. The container calls this method
140: * after the instance creation.
141: */
142: public void setSessionContext(SessionContext ctx)
143: throws EJBException, RemoteException {
144: ejbContext = ctx;
145: testAllowedOperations("setSessionContext");
146: }
147:
148: /**
149: * A container invokes this method before it ends the life of the session
150: * object. This happens as a result of a client's invoking a remove
151: * operation, or when a container decides to terminate the session object
152: * after a timeout.
153: */
154: public void ejbRemove() throws EJBException, RemoteException {
155: testAllowedOperations("ejbRemove");
156: }
157:
158: /**
159: * The activate method is called when the instance is activated
160: * from its "passive" state. The instance should acquire any resource
161: * that it has released earlier in the ejbPassivate() method.
162: */
163: public void ejbActivate() throws EJBException, RemoteException {
164: testAllowedOperations("ejbActivate");
165: }
166:
167: /**
168: * The passivate method is called before the instance enters
169: * the "passive" state. The instance should release any resources that
170: * it can re-acquire later in the ejbActivate() method.
171: */
172: public void ejbPassivate() throws EJBException, RemoteException {
173: testAllowedOperations("ejbPassivate");
174: }
175:
176: //
177: // SessionBean interface methods
178: //==================================
179:
180: //============================================
181: // SessionSynchronization interface methods
182: //
183: /**
184: * The afterBegin method notifies a session Bean instance that a new
185: * transaction has started, and that the subsequent business methods on the
186: * instance will be invoked in the context of the transaction.
187: */
188: public void afterBegin() throws EJBException, RemoteException {
189: testAllowedOperations("afterBegin");
190: }
191:
192: /**
193: * The beforeCompletion method notifies a session Bean instance that
194: * a transaction is about to be committed. The instance can use this
195: * method, for example, to write any cached data to a database.
196: */
197: public void beforeCompletion() throws EJBException, RemoteException {
198: testAllowedOperations("beforeCompletion");
199: }
200:
201: /**
202: * The afterCompletion method notifies a session Bean instance that a
203: * transaction commit protocol has completed, and tells the instance
204: * whether the transaction has been committed or rolled back.
205: */
206: public void afterCompletion(boolean committed) throws EJBException,
207: RemoteException {
208: testAllowedOperations("afterCompletion");
209: }
210:
211: //
212: // SessionSynchronization interface methods
213: //============================================
214:
215: protected void testAllowedOperations(String methodName) {
216: OperationsPolicy policy = new OperationsPolicy();
217:
218: /*[1] Test getEJBHome /////////////////*/
219: try {
220: ejbContext.getEJBHome();
221: policy.allow(policy.Context_getEJBHome);
222: } catch (IllegalStateException ise) {
223: }
224:
225: /*[2] Test getCallerPrincipal /////////*/
226: try {
227: ejbContext.getCallerPrincipal();
228: policy.allow(policy.Context_getCallerPrincipal);
229: } catch (IllegalStateException ise) {
230: }
231:
232: /*[3] Test isCallerInRole /////////////*/
233: try {
234: ejbContext.isCallerInRole("TheMan");
235: policy.allow(policy.Context_isCallerInRole);
236: } catch (IllegalStateException ise) {
237: }
238:
239: /*[4] Test getRollbackOnly ////////////*/
240: try {
241: ejbContext.getRollbackOnly();
242: policy.allow(policy.Context_getRollbackOnly);
243: } catch (IllegalStateException ise) {
244: }
245:
246: /*[5] Test setRollbackOnly ////////////*/
247: try {
248: ejbContext.setRollbackOnly();
249: policy.allow(policy.Context_setRollbackOnly);
250: } catch (IllegalStateException ise) {
251: }
252:
253: /*[6] Test getUserTransaction /////////*/
254: try {
255: ejbContext.getUserTransaction();
256: policy.allow(policy.Context_getUserTransaction);
257: } catch (IllegalStateException ise) {
258: }
259:
260: /*[7] Test getEJBObject ///////////////*/
261: try {
262: ejbContext.getEJBObject();
263: policy.allow(policy.Context_getEJBObject);
264: } catch (IllegalStateException ise) {
265: }
266:
267: /* TO DO:
268: * Check for policy.Enterprise_bean_access
269: * Check for policy.JNDI_access_to_java_comp_env
270: * Check for policy.Resource_manager_access
271: */
272: allowedOperationsTable.put(methodName, policy);
273: }
274:
275: }
|