View Javadoc

1   /*
2    * Copyright (c) 1998-2002 The Jgroup Team.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License version 2 as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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     *  Returns a proxy object for the specified server class, using the 
38     *  specified invocation handler.
39     *
40     *  If <CODE>serverClass</CODE> does not implement any external interface
41     *  (i.e., an interface that extends <tt>jgroup.core.ExternalGMIListener</tt>), a
42     *  <CODE>RemoteException</CODE> is thrown.
43     *
44     *  @exception RemoteException Thrown if for some reason, it is impossible to 
45     *    instantiate a proxy class.
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     *  Returns a proxy object for set of specified interfaces, using
71     *  the specified invocation handler.
72     *
73     *  @exception RemoteException Thrown if for some reason, it is impossible to 
74     *    instantiate a proxy class.
75     */
76    public static Remote getProxy(Class[] interfaces, InvocationHandler handler)
77      throws RemoteException
78    {
79      /* Warning message */
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  } // END Exporter