KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > ext > InterceptableComponent


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6 package org.jfox.ioc.ext;
7
8 import java.lang.reflect.Method JavaDoc;
9
10 /**
11  * 可进行方法拦截的组件。实现该接口的组件,在执行器方法的时候,可以在方法执行前后,进行额外的操作
12  * <br><br>
13  * <b>注意:</b>该功能通过Java动态代理技术实现,所以,为了使用该功能,必须首先定义该Component的接口,
14  * 返回的Component实例也只能造型成接口类型:如:
15  * <pre>
16  * public interface TestComponentInterface extends Component{
17  * public String getName();
18  * }
19  *
20  * public class TestComponent implements TestComponentInterface, InterceptableComponent {
21  * public String getName() {
22  * return "getName body";
23  * }
24  * public void preInvoke(Method method, Object[] params){
25  * if(method.getName().equals("getName")) {
26  * System.out.println("preInvoke " + method.getName());
27  * }
28  * }
29  * public void postInvoke(Method method, Object result){
30  * if(method.getName().equals("getName")) {
31  * System.out.println("postInvoke " + method.getName());
32  * }
33  * return result;
34  * }
35  * }
36  *
37  * Registry registry = Registry.getInstance();
38  * registry.registerComponent(TestComponent.class);
39  * TestComponentInterface tc = (TestComponentInterface)registry.getFirstComponentMetabyImplemetation(TestComponent.class);
40  * system.out.println(tc.getName());
41  * ...
42  *
43  * 执行结果:
44  * preInvoke getName
45  * getName body
46  * postInvoke getName
47  *
48  * </pre>
49  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
50  */

51
52 public interface InterceptableComponent extends InstantiatedComponent {
53
54     /**
55      * do something before invoke a method defined in component interface
56      * example: want to log, or repack the params
57      *
58      * @param method
59      * @param params
60      */

61     void preInvoke(Method JavaDoc method, Object JavaDoc[] params);
62
63     /**
64      * do something after invoke a method
65      * example: want to repack the result
66      *
67      * @param method
68      * @param result result return by the method
69      * @return the last object return, may not be the result parameter
70      */

71     Object JavaDoc postInvoke(Method JavaDoc method, Object JavaDoc result);
72
73 }
Popular Tags