KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > util > interceptor > Interceptor


1 package org.apache.ojb.broker.util.interceptor;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * 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 JavaDoc;
19
20 //#ifdef JDK13
21
import java.lang.reflect.InvocationHandler JavaDoc;
22 //#else
23
/*
24 import com.develop.java.lang.reflect.InvocationHandler;
25 */

26 //#endif
27
/**
28  * @author Thomas Mahler
29  */

30 public abstract class Interceptor implements InvocationHandler JavaDoc
31 {
32
33     private Object JavaDoc realSubject = null;
34
35     /**
36      * @see com.develop.java.lang.reflect.InvocationHandler#invoke(Object, Method, Object[])
37      */

38     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc methodToBeInvoked, Object JavaDoc[] args) throws Throwable JavaDoc
39     {
40         beforeInvoke(proxy, methodToBeInvoked, args);
41         Object JavaDoc 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 JavaDoc proxy, Method JavaDoc methodToBeInvoked, Object JavaDoc[] args)
51         throws Throwable JavaDoc;
52
53     /**
54      * this method will be invoked after methodToBeInvoked is invoked
55      */

56     protected abstract void afterInvoke(Object JavaDoc proxy, Method JavaDoc methodToBeInvoked, Object JavaDoc[] args)
57         throws Throwable JavaDoc;
58
59     /**
60      * this method will be invoked after methodToBeInvoked is invoked
61      */

62     protected Object JavaDoc doInvoke(Object JavaDoc proxy, Method JavaDoc methodToBeInvoked, Object JavaDoc[] args)
63         throws Throwable JavaDoc
64     {
65         Method JavaDoc m =
66             getRealSubject().getClass().getMethod(
67                 methodToBeInvoked.getName(),
68                 methodToBeInvoked.getParameterTypes());
69         return m.invoke(getRealSubject(), args);
70     }
71
72     /**
73      * Returns the realSubject.
74      * @return Object
75      */

76     public Object JavaDoc getRealSubject()
77     {
78         return realSubject;
79     }
80
81     /**
82      * Sets the realSubject.
83      * @param realSubject The realSubject to set
84      */

85     public void setRealSubject(Object JavaDoc realSubject)
86     {
87         this.realSubject = realSubject;
88     }
89
90 }
91
Popular Tags