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.stateless;
017:
018: import java.util.Map;
019: import java.util.LinkedHashMap;
020:
021: import javax.annotation.PostConstruct;
022: import javax.annotation.PreDestroy;
023: import javax.ejb.Stateless;
024: import javax.interceptor.AroundInvoke;
025: import javax.interceptor.ExcludeClassInterceptors;
026: import javax.interceptor.Interceptors;
027: import javax.interceptor.InvocationContext;
028:
029: import org.apache.openejb.test.SuperInterceptedBean;
030: import org.apache.openejb.test.interceptor.ClassInterceptor;
031: import org.apache.openejb.test.interceptor.Interceptor;
032: import org.apache.openejb.test.interceptor.MethodInterceptor;
033:
034: /**
035: * @version $Rev: 607077 $ $Date: 2007-12-27 06:55:23 -0800 $
036: */
037: @Stateless(name="BasicStatelessIntercepted")
038: @Interceptors({ClassInterceptor.class})
039: public class BasicStatelessInterceptedBean extends SuperInterceptedBean
040: implements BasicStatelessInterceptedLocal,
041: BasicStatelessInterceptedRemote {
042:
043: private static Map<String, Object> contextData = new LinkedHashMap<String, Object>();
044:
045: /**
046: * A simple dummy business method to concat 2 strings
047: */
048: public String concat(String str1, String str2) {
049: return str1.concat(str2);
050: }
051:
052: /**
053: * A simple dummy busines method to reverse a string
054: */
055: @Interceptors({MethodInterceptor.class})
056: public String reverse(String str) {
057: StringBuffer b = new StringBuffer(str);
058: return b.reverse().toString();
059: }
060:
061: /**
062: * @param ctxData the contextData to set
063: */
064: private void setContextData(Map<String, Object> ctxData) {
065: BasicStatelessInterceptedBean.contextData.putAll(ctxData);
066: }
067:
068: /**
069: * <code>ClassInterceptor</code> should not intercept this.
070: *
071: * @return the contextData
072: */
073: @ExcludeClassInterceptors
074: public Map<String, Object> getContextData() {
075: return contextData;
076: }
077:
078: /**
079: * The interceptor method.
080: * This should intercept all business methods in this bean class.
081: * It cannot exclude even those annotated with <code>@ExcludeClassInterceptors</code>
082: *
083: * @param ctx - InvocationContext
084: *
085: * @return - the result of the next method invoked. If a method returns void, proceed returns null.
086: * For lifecycle callback interceptor methods, if there is no callback method defined on the bean class,
087: * the invocation of proceed in the last interceptor method in the chain is a no-op, and null is returned.
088: * If there is more than one such interceptor method, the invocation of proceed causes the container to execute those methods in order.
089: *
090: * @throws Exception runtime exceptions or application exceptions that are allowed in the throws clause of the business method.
091: */
092: @AroundInvoke
093: public Object inBeanInterceptor(InvocationContext ctx)
094: throws Exception {
095: Map<String, Object> ctxData = Interceptor.profile(ctx,
096: "inBeanInterceptor");
097: setContextData(ctxData);
098: return ctx.proceed();
099: }
100:
101: /**
102: * The interceptor method.
103: * This should intercept postConstruct of the bean
104: *
105: * @throws Exception runtime exceptions.
106: */
107: @PostConstruct
108: public void inBeanInterceptorPostConstruct() throws Exception {
109: Map<String, Object> ctxData = Interceptor.profile(this ,
110: "inBeanInterceptorPostConstruct");
111: setContextData(ctxData);
112: }
113:
114: /**
115: * The interceptor method.
116: * This should intercept preDestroy of the bean.
117: *
118: * @throws Exception runtime exceptions.
119: */
120: @PreDestroy
121: public void inBeanInterceptorPreDestroy() throws Exception {
122: Map<String, Object> ctxData = Interceptor.profile(this ,
123: "inBeanInterceptorPreDestroy");
124: setContextData(ctxData);
125: }
126:
127: }
|