01: /* JFox, the OpenSource J2EE Application Server
02: *
03: * Copyright (C) 2002 huihoo.org
04: * Distributable under GNU LGPL license
05: * See the GNU Lesser General Public License for more details.
06: */
07:
08: package org.huihoo.jfox.pool;
09:
10: import java.lang.reflect.InvocationHandler;
11: import java.lang.reflect.Method;
12:
13: /**
14: * 需要拦截 PoolableObject 的方法调用的时候,使用 Proxy 机制来访问
15: * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
16: */
17:
18: public class PoolableObjectInvocationHandler implements
19: InvocationHandler {
20:
21: private PoolableObject poolableObject = null;
22: private boolean passivated = false;
23:
24: public PoolableObjectInvocationHandler() {
25:
26: }
27:
28: public PoolableObjectInvocationHandler(PoolableObject poolableObject) {
29: this .poolableObject = poolableObject;
30: }
31:
32: public Object invoke(Object proxy, Method method, Object[] args)
33: throws Throwable {
34: if (passivated)
35: throw new NullPointerException(
36: "pooled object had passviated.");
37:
38: // 默认只拦截 passivate 方法,如果要拦截其他方法,可以写一个子类
39: if (method.getName().equals("passivate")
40: && method.getParameterTypes().length == 0) {
41: passivated = true;
42: }
43: return method.invoke(poolableObject, args);
44: }
45:
46: public PoolableObject getPoolableObject() {
47: return poolableObject;
48: }
49:
50: }
|