Source code: com/mjh/switchrmi/RmiInvocationHandlerBase.java
1 //
2 // SwitchRMI Framework
3 // Copyright (c) 2000-2002 by Michael J. Henderson & Associates.
4 //
5 // Michael Henderson
6 // http://switchrmi.sf.net
7 // mailto:mikehenderson@dunelm.org.uk
8 //
9 // This library is free software.
10 //
11 // You may redistribute it and/or modify it under the terms of the GNU
12 // Lesser General Public License as published by the Free Software Foundation.
13 //
14 // Version 2.1 of the license should be included with this distribution in
15 // the file LICENSE, as well as License.html. If the license is not
16 // included with this distribution, you may find a copy at the FSF web
17 // site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
18 // Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
19 //
20 // This library is distributed in the hope that it will be useful,
21 // but WITHOUT ANY WARRANTY; without even the implied waranty of
22 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 // Lesser General Public License for more details.
24 //
25 // $Id: RmiInvocationHandlerBase.java,v 1.1 2002/11/11 22:19:48 mikehenderson Exp $
26 package com.mjh.switchrmi;
27
28 import java.lang.reflect.*;
29
30 import java.util.*;
31
32 import org.apache.log4j.*;
33
34 public abstract class RmiInvocationHandlerBase
35 implements InvocationHandler
36 {
37 private static final Logger log =
38 Logger.getLogger(RmiInvocationHandlerBase.class.getName());
39 private static HashMap illegalMethods = new HashMap();
40
41 static
42 {
43 illegalMethods.put("toString", "x");
44 illegalMethods.put("wait", "x");
45 illegalMethods.put("clone", "x");
46 illegalMethods.put("getClass", "x");
47 illegalMethods.put("notify", "x");
48 illegalMethods.put("hashCode", "x");
49 illegalMethods.put("finalize", "x");
50 illegalMethods.put("equals", "x");
51 }
52
53 private Class[] interfaces;
54
55 public RmiInvocationHandlerBase(Class[] interfaces)
56 {
57 this.interfaces = interfaces;
58 }
59
60 /**
61 * @see java.lang.reflect.InvocationHandler#invoke(Object, Method, Object[])
62 */
63 public Object invoke(Object target, Method method, Object[] args)
64 throws Throwable
65 {
66 Object result = null;
67
68 if ((illegalMethods.get(method.getName()) != null)
69 || !methodIsInInterfaces(method))
70 {
71 return method.invoke(this, args);
72 }
73
74 return rmiInvoke(target, method, args);
75 }
76
77 public abstract Object rmiInvoke(Object target, Method method,
78 Object[] args)
79 throws Throwable;
80
81 private boolean methodIsInInterfaces(Method method)
82 {
83 boolean result = false;
84 Class declaringClass = method.getDeclaringClass();
85
86 for (int i = 0; i < interfaces.length; i++)
87 {
88 Class interfaze = interfaces[i];
89
90 result = interfaze.getName().equals(declaringClass.getName());
91
92 if (result == true)
93 {
94 break;
95 }
96 }
97
98 return result;
99 }
100 }