001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.test.entity.cmp;
018:
019: import org.apache.openejb.test.ApplicationException;
020: import org.apache.openejb.test.object.OperationsPolicy;
021:
022: import javax.ejb.EntityBean;
023: import javax.ejb.EntityContext;
024: import javax.ejb.CreateException;
025: import java.util.StringTokenizer;
026: import java.util.Properties;
027: import java.util.Map;
028: import java.util.HashMap;
029:
030: public abstract class ComplexCmp2Bean implements EntityBean {
031: public EntityContext ejbContext;
032: public Map<String, OperationsPolicy> allowedOperationsTable = new HashMap<String, OperationsPolicy>();
033:
034: public abstract String getFirstName();
035:
036: public abstract void setFirstName(String firstName);
037:
038: public abstract String getLastName();
039:
040: public abstract void setLastName(String lastName);
041:
042: //=============================
043: // Home interface methods
044: //
045:
046: /**
047: * Maps to BasicCmpHome.sum
048: *
049: * Adds x and y and returns the result.
050: */
051: public int ejbHomeSum(int x, int y) {
052: testAllowedOperations("ejbHome");
053: return x + y;
054: }
055:
056: /**
057: * Maps to BasicCmpHome.create(String name)
058: */
059: public ComplexCmpBeanPk ejbCreateObject(String name)
060: throws CreateException {
061: StringTokenizer st = new StringTokenizer(name, " ");
062: setFirstName(st.nextToken());
063: setLastName(st.nextToken());
064: return null;
065: }
066:
067: public void ejbPostCreateObject(String name) {
068: }
069:
070: //
071: // Home interface methods
072: //=============================
073:
074: //=============================
075: // Remote interface methods
076: //
077:
078: /**
079: * Maps to BasicCmpObject.businessMethod
080: */
081: public String businessMethod(String text) {
082: testAllowedOperations("businessMethod");
083: StringBuffer b = new StringBuffer(text);
084: return b.reverse().toString();
085: }
086:
087: /**
088: * Throws an ApplicationException when invoked
089: */
090: public void throwApplicationException() throws ApplicationException {
091: throw new ApplicationException(
092: "Testing ability to throw Application Exceptions");
093: }
094:
095: /**
096: * Throws a java.lang.NullPointerException when invoked
097: * This is a system exception and should result in the
098: * destruction of the instance and invalidation of the
099: * remote reference.
100: */
101: public void throwSystemException_NullPointer() {
102: throw new NullPointerException(
103: "Testing ability to throw System Exceptions");
104: }
105:
106: /**
107: * Maps to BasicCmpObject.getPermissionsReport
108: *
109: * Returns a report of the bean's
110: * runtime permissions
111: */
112: public Properties getPermissionsReport() {
113: /* TO DO: */
114: return null;
115: }
116:
117: /**
118: * Maps to BasicCmpObject.getAllowedOperationsReport
119: *
120: * Returns a report of the allowed opperations
121: * for one of the bean's methods.
122: *
123: * @param methodName The method for which to get the allowed opperations report
124: */
125: public OperationsPolicy getAllowedOperationsReport(String methodName) {
126: return allowedOperationsTable.get(methodName);
127: }
128:
129: //
130: // Remote interface methods
131: //=============================
132:
133: //================================
134: // EntityBean interface methods
135: //
136:
137: /**
138: * A container invokes this method to instruct the
139: * instance to synchronize its state by loading it state from the
140: * underlying database.
141: */
142: public void ejbLoad() {
143: }
144:
145: /**
146: * Set the associated entity context. The container invokes this method
147: * on an instance after the instance has been created.
148: */
149: public void setEntityContext(EntityContext ctx) {
150: ejbContext = ctx;
151: testAllowedOperations("setEntityContext");
152: }
153:
154: /**
155: * Unset the associated entity context. The container calls this method
156: * before removing the instance.
157: */
158: public void unsetEntityContext() {
159: testAllowedOperations("unsetEntityContext");
160: }
161:
162: /**
163: * A container invokes this method to instruct the
164: * instance to synchronize its state by storing it to the underlying
165: * database.
166: */
167: public void ejbStore() {
168: }
169:
170: /**
171: * A container invokes this method before it removes the EJB object
172: * that is currently associated with the instance. This method
173: * is invoked when a client invokes a remove operation on the
174: * enterprise Bean's home interface or the EJB object's remote interface.
175: * This method transitions the instance from the ready state to the pool
176: * of available instances.
177: */
178: public void ejbRemove() {
179: }
180:
181: /**
182: * A container invokes this method when the instance
183: * is taken out of the pool of available instances to become associated
184: * with a specific EJB object. This method transitions the instance to
185: * the ready state.
186: */
187: public void ejbActivate() {
188: testAllowedOperations("ejbActivate");
189: }
190:
191: /**
192: * A container invokes this method on an instance before the instance
193: * becomes disassociated with a specific EJB object. After this method
194: * completes, the container will place the instance into the pool of
195: * available instances.
196: */
197: public void ejbPassivate() {
198: testAllowedOperations("ejbPassivate");
199: }
200:
201: //
202: // EntityBean interface methods
203: //================================
204:
205: protected void testAllowedOperations(String methodName) {
206: OperationsPolicy policy = new OperationsPolicy();
207:
208: /*[1] Test getEJBHome /////////////////*/
209: try {
210: ejbContext.getEJBHome();
211: policy.allow(OperationsPolicy.Context_getEJBHome);
212: } catch (IllegalStateException ise) {
213: }
214:
215: /*[2] Test getCallerPrincipal /////////*/
216: try {
217: ejbContext.getCallerPrincipal();
218: policy.allow(OperationsPolicy.Context_getCallerPrincipal);
219: } catch (IllegalStateException ise) {
220: }
221:
222: /*[3] Test isCallerInRole /////////////*/
223: try {
224: ejbContext.isCallerInRole("TheMan");
225: policy.allow(OperationsPolicy.Context_isCallerInRole);
226: } catch (IllegalStateException ise) {
227: }
228:
229: /*[4] Test getRollbackOnly ////////////*/
230: try {
231: ejbContext.getRollbackOnly();
232: policy.allow(OperationsPolicy.Context_getRollbackOnly);
233: } catch (IllegalStateException ise) {
234: }
235:
236: /*[5] Test setRollbackOnly ////////////*/
237: try {
238: ejbContext.setRollbackOnly();
239: policy.allow(OperationsPolicy.Context_setRollbackOnly);
240: } catch (IllegalStateException ise) {
241: }
242:
243: /*[6] Test getUserTransaction /////////*/
244: try {
245: ejbContext.getUserTransaction();
246: policy.allow(OperationsPolicy.Context_getUserTransaction);
247: } catch (Exception e) {
248: }
249:
250: /*[7] Test getEJBObject ///////////////*/
251: try {
252: ejbContext.getEJBObject();
253: policy.allow(OperationsPolicy.Context_getEJBObject);
254: } catch (IllegalStateException ise) {
255: }
256:
257: /*[8] Test getPrimaryKey //////////////*/
258: try {
259: ejbContext.getPrimaryKey();
260: policy.allow(OperationsPolicy.Context_getPrimaryKey);
261: } catch (IllegalStateException ise) {
262: }
263:
264: /* TO DO:
265: * Check for policy.Enterprise_bean_access
266: * Check for policy.JNDI_access_to_java_comp_env
267: * Check for policy.Resource_manager_access
268: */
269: allowedOperationsTable.put(methodName, policy);
270: }
271: }
|