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