01: package org.apache.ojb.broker.util.interceptor;
02:
03: /* Copyright 2002-2005 The Apache Software Foundation
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: import java.lang.reflect.Method;
19:
20: //#ifdef JDK13
21: import java.lang.reflect.InvocationHandler;
22:
23: //#else
24: /*
25: import com.develop.java.lang.reflect.InvocationHandler;
26: */
27: //#endif
28: /**
29: * @author Thomas Mahler
30: */
31: public abstract class Interceptor implements InvocationHandler {
32:
33: private Object realSubject = null;
34:
35: /**
36: * @see com.develop.java.lang.reflect.InvocationHandler#invoke(Object, Method, Object[])
37: */
38: public Object invoke(Object proxy, Method methodToBeInvoked,
39: Object[] args) throws Throwable {
40: beforeInvoke(proxy, methodToBeInvoked, args);
41: Object result = null;
42: result = doInvoke(proxy, methodToBeInvoked, args);
43: afterInvoke(proxy, methodToBeInvoked, args);
44: return result;
45: }
46:
47: /**
48: * this method will be invoked before methodToBeInvoked is invoked
49: */
50: protected abstract void beforeInvoke(Object proxy,
51: Method methodToBeInvoked, Object[] args) throws Throwable;
52:
53: /**
54: * this method will be invoked after methodToBeInvoked is invoked
55: */
56: protected abstract void afterInvoke(Object proxy,
57: Method methodToBeInvoked, Object[] args) throws Throwable;
58:
59: /**
60: * this method will be invoked after methodToBeInvoked is invoked
61: */
62: protected Object doInvoke(Object proxy, Method methodToBeInvoked,
63: Object[] args) throws Throwable {
64: Method m = getRealSubject().getClass().getMethod(
65: methodToBeInvoked.getName(),
66: methodToBeInvoked.getParameterTypes());
67: return m.invoke(getRealSubject(), args);
68: }
69:
70: /**
71: * Returns the realSubject.
72: * @return Object
73: */
74: public Object getRealSubject() {
75: return realSubject;
76: }
77:
78: /**
79: * Sets the realSubject.
80: * @param realSubject The realSubject to set
81: */
82: public void setRealSubject(Object realSubject) {
83: this.realSubject = realSubject;
84: }
85:
86: }
|