1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package jgroup.relacs.rmi;
20
21 import java.lang.reflect.InvocationHandler;
22 import java.lang.reflect.Proxy;
23 import java.rmi.Remote;
24 import java.rmi.RemoteException;
25 import java.util.ArrayList;
26
27 import jgroup.core.ExternalGMIListener;
28
29
30
31
32 public class Exporter
33 {
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public static Remote getProxy(Class serverClass, InvocationHandler handler)
48 throws RemoteException
49 {
50 Class[] interfaces = serverClass.getInterfaces();
51 ArrayList array = new ArrayList();
52 for (int i=0; i < interfaces.length; i++)
53 if (ExternalGMIListener.class.isAssignableFrom(interfaces[i]))
54 array.add(interfaces[i]);
55 interfaces = (Class[]) array.toArray(new Class[array.size()]);
56 if (interfaces.length == 0)
57 throw new RemoteException("Unable to instantiate a dynamic proxy for class " +
58 serverClass.getName() + ": does not implement any external GMI interface");
59
60 try {
61 return (Remote) Proxy.newProxyInstance(serverClass.getClassLoader(),
62 interfaces, handler);
63 } catch (Exception e) {
64 throw new RemoteException("Unable to instantiate a dynamic proxy " +
65 "for class " + serverClass.getName(), e);
66 }
67 }
68
69
70
71
72
73
74
75
76 public static Remote getProxy(Class[] interfaces, InvocationHandler handler)
77 throws RemoteException
78 {
79
80 if (interfaces == null || interfaces.length == 0)
81 throw new RemoteException(
82 "Unable to instantiate a dynamic proxy: no interfaces provided");
83 try {
84 return (Remote) Proxy.newProxyInstance(interfaces[0].getClassLoader(),
85 interfaces, handler);
86 } catch (Exception e) {
87 StringBuilder buffer = new StringBuilder();
88 buffer.append("Unable to instantiate a dynamic proxy for interfaces ");
89 for (int i=0; i<interfaces.length; i++) {
90 buffer.append(interfaces[i].getName());
91 if (i < interfaces.length - 1)
92 buffer.append(", ");
93 }
94 throw new RemoteException(buffer.toString(), e);
95 }
96 }
97
98 }