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.mdb;
017:
018: import org.apache.openejb.test.ApplicationException;
019: import org.apache.openejb.test.object.OperationsPolicy;
020:
021: import javax.ejb.EJBException;
022: import javax.ejb.MessageDrivenBean;
023: import javax.ejb.MessageDrivenContext;
024: import javax.jms.ConnectionFactory;
025: import javax.jms.JMSException;
026: import javax.jms.Message;
027: import javax.jms.MessageListener;
028: import javax.naming.InitialContext;
029: import java.util.Hashtable;
030: import java.util.Properties;
031:
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 BasicMdbBean implements BasicMdbObject, MessageDrivenBean,
038: MessageListener {
039: private MessageDrivenContext mdbContext = null;
040: private static Hashtable allowedOperationsTable = new Hashtable();
041: protected MdbInvoker mdbInvoker;
042:
043: public void setMessageDrivenContext(MessageDrivenContext ctx)
044: throws EJBException {
045: this .mdbContext = ctx;
046: testAllowedOperations("setMessageDrivenContext");
047: try {
048: ConnectionFactory connectionFactory = (ConnectionFactory) new InitialContext()
049: .lookup("java:comp/env/jms");
050: mdbInvoker = new MdbInvoker(connectionFactory, this );
051: } catch (Exception e) {
052: throw new EJBException(e);
053: }
054: }
055:
056: public void onMessage(Message message) {
057: try {
058: // System.out.println("\n" +
059: // "***************************************\n" +
060: // "Got message: " + message + "\n" +
061: // "***************************************\n\n");
062: try {
063: message.acknowledge();
064: } catch (JMSException e) {
065: e.printStackTrace();
066: }
067: mdbInvoker.onMessage(message);
068: } catch (Throwable e) {
069: e.printStackTrace();
070: }
071: }
072:
073: //=============================
074: // Home interface methods
075: //
076: //
077: // Home interface methods
078: //=============================
079:
080: //=============================
081: // Remote interface methods
082: //
083:
084: /**
085: * Maps to BasicStatelessObject.businessMethod
086: */
087: public String businessMethod(String text) {
088: testAllowedOperations("businessMethod");
089: StringBuffer b = new StringBuffer(text);
090: return b.reverse().toString();
091: }
092:
093: /**
094: * Throws an ApplicationException when invoked
095: *
096: */
097: public void throwApplicationException() throws ApplicationException {
098: throw new ApplicationException(
099: "Testing ability to throw Application Exceptions");
100: }
101:
102: /**
103: * Throws a java.lang.NullPointerException when invoked
104: * This is a system exception and should result in the
105: * destruction of the instance and invalidation of the
106: * remote reference.
107: *
108: */
109: public void throwSystemException_NullPointer() {
110: throw new NullPointerException(
111: "Testing ability to throw System Exceptions");
112: }
113:
114: /**
115: * Maps to BasicStatelessObject.getPermissionsReport
116: *
117: * Returns a report of the bean's
118: * runtime permissions
119: *
120: * @return
121: */
122: public Properties getPermissionsReport() {
123: /* TO DO: */
124: return null;
125: }
126:
127: /**
128: * Maps to BasicStatelessObject.getAllowedOperationsReport
129: *
130: * Returns a report of the allowed opperations
131: * for one of the bean's methods.
132: *
133: * @param methodName The method for which to get the allowed opperations report
134: * @return
135: */
136: public OperationsPolicy getAllowedOperationsReport(String methodName) {
137: return (OperationsPolicy) allowedOperationsTable
138: .get(methodName);
139: }
140:
141: //
142: // Remote interface methods
143: //=============================
144:
145: //================================
146: // MessageDrivenBean interface methods
147: //
148:
149: public void ejbCreate() throws javax.ejb.CreateException {
150: testAllowedOperations("ejbCreate");
151: }
152:
153: public void ejbRemove() throws EJBException {
154: testAllowedOperations("ejbRemove");
155: }
156:
157: //
158: // SessionBean interface methods
159: //================================
160:
161: protected void testAllowedOperations(String methodName) {
162: OperationsPolicy policy = new OperationsPolicy();
163:
164: /*[0] Test getEJBHome /////////////////*/
165: try {
166: mdbContext.getEJBHome();
167: policy.allow(policy.Context_getEJBHome);
168: } catch (IllegalStateException ise) {
169: }
170:
171: /*[1] Test getCallerPrincipal /////////*/
172: try {
173: mdbContext.getCallerPrincipal();
174: policy.allow(policy.Context_getCallerPrincipal);
175: } catch (IllegalStateException ise) {
176: }
177:
178: /*[2] Test isCallerInRole /////////////*/
179: try {
180: mdbContext.isCallerInRole("TheMan");
181: policy.allow(policy.Context_isCallerInRole);
182: } catch (IllegalStateException ise) {
183: }
184:
185: /*[3] Test getRollbackOnly ////////////*/
186: try {
187: mdbContext.getRollbackOnly();
188: policy.allow(policy.Context_getRollbackOnly);
189: } catch (IllegalStateException ise) {
190: }
191:
192: /*[4] Test setRollbackOnly ////////////*/
193: // Rollback causes message redelivery
194: /*[5] Test getUserTransaction /////////*/
195: try {
196: mdbContext.getUserTransaction();
197: policy.allow(policy.Context_getUserTransaction);
198: } catch (IllegalStateException ise) {
199: }
200:
201: /*[6] Test getEJBObject ///////////////
202: *
203: * MDBs don't have an ejbObject
204: */
205:
206: /*[7] Test Context_getPrimaryKey ///////////////
207: *
208: * Can't really do this
209: */
210:
211: /*[8] Test JNDI_access_to_java_comp_env ///////////////*/
212: try {
213: InitialContext jndiContext = new InitialContext();
214:
215: String actual = (String) jndiContext
216: .lookup("java:comp/env/stateless/references/JNDI_access_to_java_comp_env");
217:
218: policy.allow(policy.JNDI_access_to_java_comp_env);
219: } catch (IllegalStateException ise) {
220: } catch (javax.naming.NamingException ne) {
221: }
222:
223: /*[11] Test lookup /////////*/
224: try {
225: mdbContext
226: .lookup("stateless/references/JNDI_access_to_java_comp_env");
227: policy.allow(policy.Context_lookup);
228: } catch (IllegalArgumentException ise) {
229: }
230:
231: allowedOperationsTable.put(methodName, policy);
232:
233: }
234:
235: }
|