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