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