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.stateless;
017:
018: import java.rmi.RemoteException;
019: import java.util.Hashtable;
020: import java.util.Properties;
021: import javax.ejb.EJBException;
022: import javax.ejb.SessionBean;
023: import javax.ejb.SessionContext;
024: import javax.ejb.TimedObject;
025: import javax.ejb.Timer;
026: import javax.naming.InitialContext;
027: import javax.naming.NamingException;
028:
029: import org.apache.openejb.test.ApplicationException;
030: import org.apache.openejb.test.beans.TimerSync;
031: import org.apache.openejb.test.object.OperationsPolicy;
032:
033: /**
034: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
035: * @author <a href="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
036: */
037: public class BasicStatelessBean implements SessionBean, TimedObject {
038:
039: private String name;
040: private SessionContext ejbContext;
041: private static Hashtable allowedOperationsTable = new Hashtable();
042:
043: //=============================
044: // Home interface methods
045: //
046: //
047: // Home interface methods
048: //=============================
049:
050: //=============================
051: // Remote interface methods
052: //
053:
054: /**
055: * Maps to BasicStatelessObject.businessMethod
056: *
057: * @return
058: * @see BasicStatelessObject#businessMethod
059: */
060: public String businessMethod(String text) {
061: testAllowedOperations("businessMethod");
062: StringBuffer b = new StringBuffer(text);
063: return b.reverse().toString();
064: }
065:
066: public void scheduleTimer(String name) {
067: ejbContext.getTimerService().createTimer(1, name);
068: }
069:
070: /**
071: * Throws an ApplicationException when invoked
072: */
073: public void throwApplicationException() throws ApplicationException {
074: throw new ApplicationException(
075: "Testing ability to throw Application Exceptions");
076: }
077:
078: /**
079: * Throws a java.lang.NullPointerException when invoked
080: * This is a system exception and should result in the
081: * destruction of the instance and invalidation of the
082: * remote reference.
083: */
084: public void throwSystemException_NullPointer() {
085: throw new NullPointerException(
086: "Testing ability to throw System Exceptions");
087: }
088:
089: /**
090: * Maps to BasicStatelessObject.getPermissionsReport
091: * <p/>
092: * Returns a report of the bean's
093: * runtime permissions
094: *
095: * @return
096: * @see BasicStatelessObject#getPermissionsReport
097: */
098: public Properties getPermissionsReport() {
099: /* TO DO: */
100: return null;
101: }
102:
103: /**
104: * Maps to BasicStatelessObject.getAllowedOperationsReport
105: * <p/>
106: * Returns a report of the allowed opperations
107: * for one of the bean's methods.
108: *
109: * @param methodName The method for which to get the allowed opperations report
110: * @return
111: * @see BasicStatelessObject#getAllowedOperationsReport
112: */
113: public OperationsPolicy getAllowedOperationsReport(String methodName) {
114: return (OperationsPolicy) allowedOperationsTable
115: .get(methodName);
116: }
117:
118: public String remove(String str) {
119: return str;
120: }
121:
122: //
123: // Remote interface methods
124: //=============================
125:
126: //================================
127: // SessionBean interface methods
128: //
129:
130: /**
131: * Set the associated session context. The container calls this method
132: * after the instance creation.
133: */
134: public void setSessionContext(SessionContext ctx)
135: throws EJBException, RemoteException {
136: ejbContext = ctx;
137: testAllowedOperations("setSessionContext");
138: }
139:
140: /**
141: * @throws javax.ejb.CreateException
142: */
143: public void ejbCreateObject() throws javax.ejb.CreateException {
144: testAllowedOperations("ejbCreate");
145: this .name = "nameless automaton";
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: // Should never called.
166: }
167:
168: /**
169: * The passivate method is called before the instance enters
170: * the "passive" state. The instance should release any resources that
171: * it can re-acquire later in the ejbActivate() method.
172: */
173: public void ejbPassivate() throws EJBException, RemoteException {
174: testAllowedOperations("ejbPassivate");
175: // Should never called.
176: }
177:
178: public void ejbTimeout(Timer timer) {
179: testAllowedOperations("ejbTimeout");
180: try {
181: String name = (String) timer.getInfo();
182: TimerSync timerSync = (TimerSync) ejbContext
183: .lookup("TimerSyncBeanBusinessRemote");
184: timerSync.countDown(name);
185: } catch (Exception e) {
186: e.printStackTrace();
187: }
188: }
189:
190: //
191: // SessionBean interface methods
192: //================================
193:
194: protected void testAllowedOperations(String methodName) {
195: OperationsPolicy policy = new OperationsPolicy();
196:
197: /*[0] Test getEJBHome /////////////////*/
198: try {
199: ejbContext.getEJBHome();
200: policy.allow(OperationsPolicy.Context_getEJBHome);
201: } catch (IllegalStateException ise) {
202: }
203:
204: /*[1] Test getCallerPrincipal /////////*/
205: try {
206: ejbContext.getCallerPrincipal();
207: policy.allow(OperationsPolicy.Context_getCallerPrincipal);
208: } catch (IllegalStateException ise) {
209: }
210:
211: /*[2] Test isCallerInRole /////////////*/
212: try {
213: ejbContext.isCallerInRole("TheMan");
214: policy.allow(OperationsPolicy.Context_isCallerInRole);
215: } catch (IllegalStateException ise) {
216: }
217:
218: /*[3] Test getRollbackOnly ////////////*/
219: try {
220: ejbContext.getRollbackOnly();
221: policy.allow(OperationsPolicy.Context_getRollbackOnly);
222: } catch (IllegalStateException ise) {
223: }
224:
225: /*[4] Test setRollbackOnly ////////////*/
226: try {
227: ejbContext.setRollbackOnly();
228: policy.allow(OperationsPolicy.Context_setRollbackOnly);
229: } catch (IllegalStateException ise) {
230: }
231:
232: /*[5] Test getUserTransaction /////////*/
233: try {
234: ejbContext.getUserTransaction();
235: policy.allow(OperationsPolicy.Context_getUserTransaction);
236: } catch (IllegalStateException ise) {
237: }
238:
239: /*[6] Test getEJBObject ///////////////*/
240: try {
241: ejbContext.getEJBObject();
242: policy.allow(OperationsPolicy.Context_getEJBObject);
243: } catch (IllegalStateException ise) {
244: }
245:
246: /*[7] Test Context_getPrimaryKey ///////////////
247: *
248: * Can't really do this
249: */
250:
251: /*[8] Test JNDI_access_to_java_comp_env ///////////////*/
252: try {
253: InitialContext jndiContext = new InitialContext();
254:
255: String actual = (String) jndiContext
256: .lookup("java:comp/env/stateless/references/JNDI_access_to_java_comp_env");
257:
258: policy.allow(OperationsPolicy.JNDI_access_to_java_comp_env);
259: } catch (IllegalStateException ise) {
260: } catch (NamingException ne) {
261: }
262:
263: /*[11] Test lookup /////////*/
264: try {
265: ejbContext
266: .lookup("stateless/references/JNDI_access_to_java_comp_env");
267: policy.allow(OperationsPolicy.Context_lookup);
268: } catch (IllegalArgumentException ise) {
269: }
270:
271: /*[12] Test getTimerService/////////*/
272: try {
273: ejbContext.getTimerService();
274: policy.allow(OperationsPolicy.Context_getTimerService);
275: } catch (IllegalStateException ise) {
276: }
277:
278: allowedOperationsTable.put(methodName, policy);
279: }
280:
281: }
|