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.entity.cmp;
017:
018: import java.rmi.RemoteException;
019: import java.util.Properties;
020: import java.util.StringTokenizer;
021: import java.util.Map;
022: import java.util.TreeMap;
023:
024: import javax.ejb.EJBException;
025: import javax.ejb.EntityContext;
026: import javax.ejb.RemoveException;
027: import javax.ejb.CreateException;
028: import javax.ejb.EntityBean;
029: import javax.naming.InitialContext;
030: import javax.naming.NamingException;
031:
032: import org.apache.openejb.test.object.OperationsPolicy;
033:
034: /**
035: *
036: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
037: * @author <a href="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
038: */
039: public class AllowedOperationsCmpBean implements EntityBean {
040: private static int nextId;
041: public Integer primaryKey;
042: public String firstName;
043: public String lastName;
044: public int number;
045: public EntityContext ejbContext;
046: public static Map<String, OperationsPolicy> allowedOperationsTable = new TreeMap<String, OperationsPolicy>();
047:
048: //=============================
049: // Home interface methods
050: //
051:
052: /**
053: * Maps to BasicCmpHome.sum
054: *
055: * Adds x and y and returns the result.
056: *
057: * @param x
058: * @param y
059: * @return x + y
060: * @see BasicCmpHome#sum
061: */
062: public int ejbHomeSum(int x, int y) {
063: testAllowedOperations("ejbHome");
064: return x + y;
065: }
066:
067: public void ejbHomeVoidSelect() {
068: }
069:
070: /**
071: * Maps to BasicCmpHome.create
072: */
073: public Integer ejbCreateObject(String name) throws CreateException {
074: primaryKey = nextId++;
075: testAllowedOperations("ejbCreate");
076: StringTokenizer st = new StringTokenizer(name, " ");
077: firstName = st.nextToken();
078: lastName = st.nextToken();
079: return new Integer(primaryKey);
080: }
081:
082: public void ejbPostCreateObject(String name) throws CreateException {
083: testAllowedOperations("ejbPostCreate");
084: }
085:
086: //
087: // Home interface methods
088: //=============================
089:
090: //=============================
091: // Remote interface methods
092: //
093:
094: /**
095: * Maps to BasicCmpObject.businessMethod
096: *
097: * @return
098: * @see BasicCmpObject#businessMethod
099: */
100: public String businessMethod(String text) {
101: testAllowedOperations("businessMethod");
102: number++;
103: StringBuffer b = new StringBuffer(text);
104: return b.reverse().toString();
105: }
106:
107: /**
108: * Throws an ApplicationException when invoked
109: *
110: */
111: public void throwApplicationException()
112: throws org.apache.openejb.test.ApplicationException {
113: throw new org.apache.openejb.test.ApplicationException(
114: "Don't Panic");
115: }
116:
117: /**
118: * Throws a java.lang.NullPointerException when invoked
119: * This is a system exception and should result in the
120: * destruction of the instance and invalidation of the
121: * remote reference.
122: *
123: */
124: public void throwSystemException_NullPointer() {
125: throw new NullPointerException("Panic");
126: }
127:
128: /**
129: * Maps to BasicCmpObject.getPermissionsReport
130: *
131: * Returns a report of the bean's
132: * runtime permissions
133: *
134: * @return
135: * @see BasicCmpObject#getPermissionsReport
136: */
137: public Properties getPermissionsReport() {
138: /* TO DO: */
139: return null;
140: }
141:
142: /**
143: * Maps to BasicCmpObject.getAllowedOperationsReport
144: *
145: * Returns a report of the allowed opperations
146: * for one of the bean's methods.
147: *
148: * @param methodName The method for which to get the allowed opperations report
149: * @return
150: * @see BasicCmpObject#getAllowedOperationsReport
151: */
152: public OperationsPolicy getAllowedOperationsReport(String methodName) {
153: return allowedOperationsTable.get(methodName);
154: }
155:
156: //
157: // Remote interface methods
158: //=============================
159:
160: //================================
161: // EntityBean interface methods
162: //
163:
164: /**
165: * A container invokes this method to instruct the
166: * instance to synchronize its state by loading it state from the
167: * underlying database.
168: */
169: public void ejbLoad() throws EJBException, RemoteException {
170: testAllowedOperations("ejbLoad");
171: }
172:
173: /**
174: * Set the associated entity context. The container invokes this method
175: * on an instance after the instance has been created.
176: */
177: public void setEntityContext(EntityContext ctx)
178: throws EJBException, RemoteException {
179: ejbContext = ctx;
180: testAllowedOperations("setEntityContext");
181: }
182:
183: /**
184: * Unset the associated entity context. The container calls this method
185: * before removing the instance.
186: */
187: public void unsetEntityContext() throws EJBException,
188: RemoteException {
189: testAllowedOperations("unsetEntityContext");
190: }
191:
192: /**
193: * A container invokes this method to instruct the
194: * instance to synchronize its state by storing it to the underlying
195: * database.
196: */
197: public void ejbStore() throws EJBException, RemoteException {
198: testAllowedOperations("ejbStore");
199: }
200:
201: /**
202: * A container invokes this method before it removes the EJB object
203: * that is currently associated with the instance. This method
204: * is invoked when a client invokes a remove operation on the
205: * enterprise Bean's home interface or the EJB object's remote interface.
206: * This method transitions the instance from the ready state to the pool
207: * of available instances.
208: */
209: public void ejbRemove() throws RemoveException, EJBException,
210: RemoteException {
211: testAllowedOperations("ejbRemove");
212: }
213:
214: /**
215: * A container invokes this method when the instance
216: * is taken out of the pool of available instances to become associated
217: * with a specific EJB object. This method transitions the instance to
218: * the ready state.
219: */
220: public void ejbActivate() throws EJBException, RemoteException {
221: testAllowedOperations("ejbActivate");
222: }
223:
224: /**
225: * A container invokes this method on an instance before the instance
226: * becomes disassociated with a specific EJB object. After this method
227: * completes, the container will place the instance into the pool of
228: * available instances.
229: */
230: public void ejbPassivate() throws EJBException, RemoteException {
231:
232: testAllowedOperations("ejbPassivate");
233: }
234:
235: //
236: // EntityBean interface methods
237: //================================
238: protected void testAllowedOperations(String methodName) {
239: OperationsPolicy policy = new OperationsPolicy();
240:
241: /*[0] Test getEJBHome /////////////////*/
242: try {
243: ejbContext.getEJBHome();
244: policy.allow(OperationsPolicy.Context_getEJBHome);
245: } catch (IllegalStateException ise) {
246: }
247:
248: /*[1] Test getCallerPrincipal /////////*/
249: try {
250: ejbContext.getCallerPrincipal();
251: policy.allow(OperationsPolicy.Context_getCallerPrincipal);
252: } catch (IllegalStateException ise) {
253: }
254:
255: /*[2] Test isCallerInRole /////////////*/
256: try {
257: ejbContext.isCallerInRole("TheMan");
258: policy.allow(OperationsPolicy.Context_isCallerInRole);
259: } catch (IllegalStateException ise) {
260: }
261:
262: /*[3] Test getRollbackOnly ////////////*/
263: try {
264: ejbContext.getRollbackOnly();
265: policy.allow(OperationsPolicy.Context_getRollbackOnly);
266: } catch (IllegalStateException ise) {
267: }
268:
269: /*[4] Test setRollbackOnly ////////////*/
270: // try {
271: // ejbContext.setRollbackOnly();
272: // policy.allow(OperationsPolicy.Context_setRollbackOnly );
273: // } catch (IllegalStateException ise) {
274: // }
275: /*[5] Test getUserTransaction /////////*/
276: try {
277: ejbContext.getUserTransaction();
278: policy.allow(OperationsPolicy.Context_getUserTransaction);
279: } catch (IllegalStateException ise) {
280: }
281:
282: /*[6] Test getEJBObject ///////////////*/
283: try {
284: ejbContext.getEJBObject();
285: policy.allow(OperationsPolicy.Context_getEJBObject);
286: } catch (IllegalStateException ise) {
287: }
288:
289: /*[7] Test Context_getPrimaryKey ///////////////
290: *
291: * TODO: Write this test.
292: */
293: try {
294: ejbContext.getPrimaryKey();
295: policy.allow(OperationsPolicy.Context_getPrimaryKey);
296: } catch (IllegalStateException ise) {
297: }
298:
299: /*[8] Test JNDI_access_to_java_comp_env ///////////////*/
300: try {
301: InitialContext jndiContext = new InitialContext();
302:
303: jndiContext
304: .lookup("java:comp/env/stateless/references/JNDI_access_to_java_comp_env");
305:
306: policy.allow(OperationsPolicy.JNDI_access_to_java_comp_env);
307: } catch (IllegalStateException ise) {
308: } catch (NamingException ne) {
309: }
310:
311: allowedOperationsTable.put(methodName, policy);
312: }
313: }
|