KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > enhancer > interceptors > business > bean > SingleMethodInterceptor


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: SingleMethodInterceptor.java 450 2006-05-12 15:30:25Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.tests.enhancer.interceptors.business.bean;
27
28 import java.lang.reflect.Method JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.interceptor.AroundInvoke;
32 import javax.interceptor.InvocationContext;
33
34 /**
35  * Simple interceptor class which should be launched only on single method.
36  * @author Florent Benoit
37  */

38 public class SingleMethodInterceptor {
39
40     /**
41      * Test that the interceptor is only called on a stateless
42      * bean and on singleMethodIntercepted method.
43      * @param invocationContext contains attributes of invocation
44      * @return method's invocation result
45      * @throws Exception if invocation fails
46      */

47     @AroundInvoke
48     public Object JavaDoc onlySingleMethod(final InvocationContext invocationContext) throws Exception JavaDoc {
49         Method JavaDoc m = invocationContext.getMethod();
50         // Change the returned value for add method
51
if (!m.getName().equals("singleMethodIntercepted")) {
52             throw new Exception JavaDoc("Interceptor should only be called on a single method");
53         }
54
55         // increment the counter
56
Object JavaDoc o = invocationContext.getTarget();
57         if (o instanceof StatelessBean) {
58             StatelessBean bean = (StatelessBean) o;
59             bean.incrementSingleMethodInterceptedCounter();
60         } else {
61             throw new Exception JavaDoc("Bean is not a stateless bean");
62         }
63
64         // Also, check that there is a key shared across all interceptors.
65
Map JavaDoc contextData = invocationContext.getContextData();
66         Object JavaDoc obj = contextData.get("KEY");
67         if (obj == null) {
68             throw new Exception JavaDoc("Objects are not propagated in the Map ContextData");
69         }
70         if (obj instanceof String JavaDoc) {
71             String JavaDoc value = (String JavaDoc) obj;
72             if (!value.equals("TEST")) {
73                 throw new Exception JavaDoc("Object value has been changed in the map ContextData.");
74             }
75         } else {
76             throw new Exception JavaDoc("Object type has been changed in the map ContextData.");
77         }
78
79         return invocationContext.proceed();
80     }
81
82 }
83
Popular Tags