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.object.OperationsPolicy;
020: import org.apache.openejb.test.ApplicationException;
021:
022: import javax.ejb.EntityBean;
023: import javax.ejb.EntityContext;
024: import javax.ejb.CreateException;
025: import java.util.Map;
026: import java.util.HashMap;
027: import java.util.StringTokenizer;
028: import java.util.Properties;
029:
030: public abstract class UnknownCmp2Bean 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 Object ejbCreateObject(String name) throws CreateException {
060: StringTokenizer st = new StringTokenizer(name, " ");
061: setFirstName(st.nextToken());
062: setLastName(st.nextToken());
063: return null;
064: }
065:
066: public void ejbPostCreateObject(String name) {
067: }
068:
069: //
070: // Home interface methods
071: //=============================
072:
073: //=============================
074: // Remote interface methods
075: //
076:
077: /**
078: * Maps to BasicCmpObject.businessMethod
079: */
080: public String businessMethod(String text) {
081: testAllowedOperations("businessMethod");
082: StringBuffer b = new StringBuffer(text);
083: return b.reverse().toString();
084: }
085:
086: /**
087: * Throws an ApplicationException when invoked
088: */
089: public void throwApplicationException() throws ApplicationException {
090: throw new ApplicationException(
091: "Testing ability to throw Application Exceptions");
092: }
093:
094: /**
095: * Throws a java.lang.NullPointerException when invoked
096: * This is a system exception and should result in the
097: * destruction of the instance and invalidation of the
098: * remote reference.
099: */
100: public void throwSystemException_NullPointer() {
101: throw new NullPointerException(
102: "Testing ability to throw System Exceptions");
103: }
104:
105: /**
106: * Maps to BasicCmpObject.getPermissionsReport
107: *
108: * Returns a report of the bean's
109: * runtime permissions
110: */
111: public Properties getPermissionsReport() {
112: /* TO DO: */
113: return null;
114: }
115:
116: /**
117: * Maps to BasicCmpObject.getAllowedOperationsReport
118: *
119: * Returns a report of the allowed opperations
120: * for one of the bean's methods.
121: *
122: * @param methodName The method for which to get the allowed opperations report
123: */
124: public OperationsPolicy getAllowedOperationsReport(String methodName) {
125: return allowedOperationsTable.get(methodName);
126: }
127:
128: //
129: // Remote interface methods
130: //=============================
131:
132: //================================
133: // EntityBean interface methods
134: //
135:
136: /**
137: * A container invokes this method to instruct the
138: * instance to synchronize its state by loading it state from the
139: * underlying database.
140: */
141: public void ejbLoad() {
142: }
143:
144: /**
145: * Set the associated entity context. The container invokes this method
146: * on an instance after the instance has been created.
147: */
148: public void setEntityContext(EntityContext ctx) {
149: ejbContext = ctx;
150: testAllowedOperations("setEntityContext");
151: }
152:
153: /**
154: * Unset the associated entity context. The container calls this method
155: * before removing the instance.
156: */
157: public void unsetEntityContext() {
158: testAllowedOperations("unsetEntityContext");
159: }
160:
161: /**
162: * A container invokes this method to instruct the
163: * instance to synchronize its state by storing it to the underlying
164: * database.
165: */
166: public void ejbStore() {
167: }
168:
169: /**
170: * A container invokes this method before it removes the EJB object
171: * that is currently associated with the instance. This method
172: * is invoked when a client invokes a remove operation on the
173: * enterprise Bean's home interface or the EJB object's remote interface.
174: * This method transitions the instance from the ready state to the pool
175: * of available instances.
176: */
177: public void ejbRemove() {
178: }
179:
180: /**
181: * A container invokes this method when the instance
182: * is taken out of the pool of available instances to become associated
183: * with a specific EJB object. This method transitions the instance to
184: * the ready state.
185: */
186: public void ejbActivate() {
187: testAllowedOperations("ejbActivate");
188: }
189:
190: /**
191: * A container invokes this method on an instance before the instance
192: * becomes disassociated with a specific EJB object. After this method
193: * completes, the container will place the instance into the pool of
194: * available instances.
195: */
196: public void ejbPassivate() {
197: testAllowedOperations("ejbPassivate");
198: }
199:
200: //
201: // EntityBean interface methods
202: //================================
203:
204: protected void testAllowedOperations(String methodName) {
205: OperationsPolicy policy = new OperationsPolicy();
206:
207: /*[1] Test getEJBHome /////////////////*/
208: try {
209: ejbContext.getEJBHome();
210: policy.allow(OperationsPolicy.Context_getEJBHome);
211: } catch (IllegalStateException ise) {
212: }
213:
214: /*[2] Test getCallerPrincipal /////////*/
215: try {
216: ejbContext.getCallerPrincipal();
217: policy.allow(OperationsPolicy.Context_getCallerPrincipal);
218: } catch (IllegalStateException ise) {
219: }
220:
221: /*[3] Test isCallerInRole /////////////*/
222: try {
223: ejbContext.isCallerInRole("TheMan");
224: policy.allow(OperationsPolicy.Context_isCallerInRole);
225: } catch (IllegalStateException ise) {
226: }
227:
228: /*[4] Test getRollbackOnly ////////////*/
229: try {
230: ejbContext.getRollbackOnly();
231: policy.allow(OperationsPolicy.Context_getRollbackOnly);
232: } catch (IllegalStateException ise) {
233: }
234:
235: /*[5] Test setRollbackOnly ////////////*/
236: try {
237: ejbContext.setRollbackOnly();
238: policy.allow(OperationsPolicy.Context_setRollbackOnly);
239: } catch (IllegalStateException ise) {
240: }
241:
242: /*[6] Test getUserTransaction /////////*/
243: try {
244: ejbContext.getUserTransaction();
245: policy.allow(OperationsPolicy.Context_getUserTransaction);
246: } catch (Exception e) {
247: }
248:
249: /*[7] Test getEJBObject ///////////////*/
250: try {
251: ejbContext.getEJBObject();
252: policy.allow(OperationsPolicy.Context_getEJBObject);
253: } catch (IllegalStateException ise) {
254: }
255:
256: /*[8] Test getPrimaryKey //////////////*/
257: try {
258: ejbContext.getPrimaryKey();
259: policy.allow(OperationsPolicy.Context_getPrimaryKey);
260: } catch (IllegalStateException ise) {
261: }
262:
263: /* TO DO:
264: * Check for policy.Enterprise_bean_access
265: * Check for policy.JNDI_access_to_java_comp_env
266: * Check for policy.Resource_manager_access
267: */
268: allowedOperationsTable.put(methodName, policy);
269: }
270: }
|