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