001 package org.apache.ojb.broker.util.interceptor;
002
003 /* Copyright 2002-2005 The Apache Software Foundation
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 import java.lang.reflect.Method;
019
020 //#ifdef JDK13
021 import java.lang.reflect.InvocationHandler;
022 //#else
023 /*
024 import com.develop.java.lang.reflect.InvocationHandler;
025 */
026 //#endif
027 /**
028 * @author Thomas Mahler
029 */
030 public abstract class Interceptor implements InvocationHandler
031 {
032
033 private Object realSubject = null;
034
035 /**
036 * @see com.develop.java.lang.reflect.InvocationHandler#invoke(Object, Method, Object[])
037 */
038 public Object invoke(Object proxy, Method methodToBeInvoked, Object[] args) throws Throwable
039 {
040 beforeInvoke(proxy, methodToBeInvoked, args);
041 Object result = null;
042 result = doInvoke(proxy, methodToBeInvoked, args);
043 afterInvoke(proxy, methodToBeInvoked, args);
044 return result;
045 }
046
047 /**
048 * this method will be invoked before methodToBeInvoked is invoked
049 */
050 protected abstract void beforeInvoke(Object proxy, Method methodToBeInvoked, Object[] args)
051 throws Throwable;
052
053 /**
054 * this method will be invoked after methodToBeInvoked is invoked
055 */
056 protected abstract void afterInvoke(Object proxy, Method methodToBeInvoked, Object[] args)
057 throws Throwable;
058
059 /**
060 * this method will be invoked after methodToBeInvoked is invoked
061 */
062 protected Object doInvoke(Object proxy, Method methodToBeInvoked, Object[] args)
063 throws Throwable
064 {
065 Method m =
066 getRealSubject().getClass().getMethod(
067 methodToBeInvoked.getName(),
068 methodToBeInvoked.getParameterTypes());
069 return m.invoke(getRealSubject(), args);
070 }
071
072 /**
073 * Returns the realSubject.
074 * @return Object
075 */
076 public Object getRealSubject()
077 {
078 return realSubject;
079 }
080
081 /**
082 * Sets the realSubject.
083 * @param realSubject The realSubject to set
084 */
085 public void setRealSubject(Object realSubject)
086 {
087 this.realSubject = realSubject;
088 }
089
090 }