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.mdb;
018:
019: import javax.annotation.PostConstruct;
020: import javax.annotation.Resource;
021: import javax.ejb.ActivationConfigProperty;
022: import javax.ejb.EJBException;
023: import javax.ejb.MessageDriven;
024: import javax.ejb.MessageDrivenBean;
025: import javax.ejb.MessageDrivenContext;
026: import javax.interceptor.AroundInvoke;
027: import javax.interceptor.Interceptors;
028: import javax.interceptor.InvocationContext;
029: import javax.jms.Connection;
030: import javax.jms.ConnectionFactory;
031: import javax.jms.JMSException;
032: import javax.jms.Message;
033: import javax.jms.MessageListener;
034: import javax.jms.MessageProducer;
035: import javax.jms.Session;
036: import javax.naming.InitialContext;
037:
038: import junit.framework.Assert;
039: import junit.framework.AssertionFailedError;
040:
041: import org.apache.openejb.test.TestFailureException;
042:
043: @Interceptors({MdbInterceptor.class})
044: @MessageDriven(activationConfig={@ActivationConfigProperty(propertyName="destinationType",propertyValue="javax.jms.Queue"),@ActivationConfigProperty(propertyName="destination",propertyValue="InterceptorMdbBean")})
045: public class InterceptorMdbBean implements MessageListener,
046: MessageDrivenBean {
047:
048: private boolean classLevelBusinessMethodInterception = false;
049: private boolean methodLevelBusinessMethodInterception = false;
050: protected static boolean classLevelCreateMethodInterception = false;
051: private boolean methodLevelCreateMethodInterception = false;
052: private MessageDrivenContext mdbContext;
053: private Session session;
054: private Connection connection;
055: protected MdbInvoker mdbInvoker;
056: @Resource(name="jms",type=javax.jms.QueueConnectionFactory.class)
057: private ConnectionFactory connectionFactory;
058:
059: public void onMessage(Message msg) {
060: try {
061: classLevelBusinessMethodInterception = msg
062: .getBooleanProperty("ClassLevelBusinessMethodInterception");
063: methodLevelBusinessMethodInterception = msg
064: .getBooleanProperty("MethodLevelBusinessMethodInterception");
065: try {
066: msg.acknowledge();
067: } catch (JMSException e) {
068: e.printStackTrace();
069: }
070: mdbInvoker.onMessage(msg);
071: } catch (Throwable e) {
072: e.printStackTrace();
073: }
074: }
075:
076: @AroundInvoke
077: public Object mdbInterceptor(InvocationContext ctx)
078: throws Exception {
079: Object[] objArr = ctx.getParameters();
080: Message msg = (Message) objArr[0];
081: msg.setBooleanProperty("MethodLevelBusinessMethodInterception",
082: true);
083: ctx.setParameters(objArr);
084: return ctx.proceed();
085: }
086:
087: @PostConstruct
088: public void ejbCreate() throws EJBException {
089: methodLevelCreateMethodInterception = true;
090: }
091:
092: public void checkMethodLevelBusinessMethodInterception()
093: throws TestFailureException {
094: try {
095: Assert
096: .assertTrue(
097: "Method Level Business Method Interception failed for Mdb",
098: methodLevelBusinessMethodInterception);
099: } catch (AssertionFailedError afe) {
100: throw new TestFailureException(afe);
101: }
102: }
103:
104: public void checkMethodLevelCreateMethodInterception()
105: throws TestFailureException {
106: try {
107: Assert
108: .assertTrue(
109: "Method Level Business Method Interception failed for Mdb",
110: methodLevelCreateMethodInterception);
111: } catch (AssertionFailedError afe) {
112: throw new TestFailureException(afe);
113: }
114: }
115:
116: public void checkClassLevelBusinessMethodInterception()
117: throws TestFailureException {
118: try {
119: Assert
120: .assertTrue(
121: "Class Level Business Method Interception failed for Mdb",
122: classLevelBusinessMethodInterception);
123: } catch (AssertionFailedError afe) {
124: throw new TestFailureException(afe);
125: }
126: }
127:
128: public void checkClassLevelCreateMethodInterception()
129: throws TestFailureException {
130: try {
131: Assert
132: .assertTrue(
133: "Class Level Business Method Interception failed for Mdb",
134: classLevelCreateMethodInterception);
135: } catch (AssertionFailedError afe) {
136: throw new TestFailureException(afe);
137: }
138: }
139:
140: public void ejbRemove() throws EJBException {
141: // TODO Auto-generated method stub
142:
143: }
144:
145: public void setMessageDrivenContext(MessageDrivenContext ctx)
146: throws EJBException {
147: this .mdbContext = ctx;
148: try {
149: mdbInvoker = new MdbInvoker(connectionFactory, this );
150: } catch (Exception e) {
151: throw new EJBException(e);
152: }
153: }
154:
155: }
|