KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > pool > PoolableObjectInvocationHandler


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
7 package org.jfox.pool;
8
9 import java.lang.reflect.InvocationHandler JavaDoc;
10 import java.lang.reflect.Method JavaDoc;
11
12 /**
13  * 需要拦截 PoolableObject 的方法调用的时候,使用 Proxy 机制来访问
14  *
15  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
16  */

17
18 public class PoolableObjectInvocationHandler implements InvocationHandler JavaDoc {
19
20     private PoolableObject poolableObject = null;
21     private boolean passivated = false;
22
23     public PoolableObjectInvocationHandler() {
24
25     }
26
27     public PoolableObjectInvocationHandler(PoolableObject poolableObject) {
28         this.poolableObject = poolableObject;
29     }
30
31     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
32         if(passivated) throw new NullPointerException JavaDoc("pooled object had passviated.");
33
34         // 默认只拦截 passivate 方法,如果要拦截其他方法,可以写一个子类
35
if(method.getName().equals("passivate") && method.getParameterTypes().length == 0) {
36             passivated = true;
37         }
38         return method.invoke(poolableObject, args);
39     }
40
41     public PoolableObject getPoolableObject() {
42         return poolableObject;
43     }
44
45 }
46
Popular Tags