KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > net > sockets > RMIMultiSocketServer


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.net.sockets;
23
24 import java.rmi.server.UnicastRemoteObject JavaDoc;
25 import java.rmi.Remote JavaDoc;
26 import java.lang.reflect.Proxy JavaDoc;
27 import java.rmi.server.RMIClientSocketFactory JavaDoc;
28 import java.rmi.server.RMIServerSocketFactory JavaDoc;
29 import java.rmi.RemoteException JavaDoc;
30 import java.lang.reflect.Method JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.rmi.NoSuchObjectException JavaDoc;
33 /**
34  *
35  * @author bill@jboss.org
36  * @version $Revision: 1958 $
37  */

38 public class RMIMultiSocketServer
39 {
40    private static HashMap JavaDoc handlermap = new HashMap JavaDoc();
41    private static HashMap JavaDoc stubmap = new HashMap JavaDoc();
42
43    public static Remote JavaDoc exportObject(Remote JavaDoc obj,
44                                      int port,
45                                      RMIClientSocketFactory JavaDoc csf,
46                                      RMIServerSocketFactory JavaDoc ssf,
47                                      Class JavaDoc[] interfaces,
48                                      int numSockets)
49       throws RemoteException JavaDoc
50    {
51       Remote JavaDoc[] stubs = new Remote JavaDoc[numSockets];
52
53       Method JavaDoc[] methods = obj.getClass().getMethods();
54       
55       HashMap JavaDoc invokerMap = new HashMap JavaDoc();
56       for (int i = 0; i < methods.length; i++) {
57          Long JavaDoc methodkey = new Long JavaDoc(MethodHash.calculateHash(methods[i]));
58          invokerMap.put(methodkey, methods[i]);
59       }
60       
61       RMIMultiSocketHandler[] handlers = new RMIMultiSocketHandler[numSockets];
62       for (int i = 0; i < numSockets; i++)
63       {
64          int theport = (port == 0) ? 0 : port + i;
65          handlers[i] = new RMIMultiSocketHandler(obj, invokerMap);
66          stubs[i] = UnicastRemoteObject.exportObject(handlers[i], theport, csf, ssf);
67       }
68
69       Remote JavaDoc remote = (Remote JavaDoc)Proxy.newProxyInstance(
70          obj.getClass().getClassLoader(),
71          interfaces,
72          new RMIMultiSocketClient(stubs));
73       stubmap.put(remote, stubs);
74       handlermap.put(remote, handlers);
75       return remote;
76    }
77
78    public static Remote JavaDoc exportObject(Remote JavaDoc obj,
79                                      int port,
80                                      RMIClientSocketFactory JavaDoc csf,
81                                      RMIServerSocketFactory JavaDoc ssf,
82                                      int numSockets)
83       throws RemoteException JavaDoc
84    {
85       return exportObject(obj, port, csf, ssf, obj.getClass().getInterfaces(), numSockets);
86    }
87
88    public static boolean unexportObject(Remote JavaDoc obj, boolean force)
89       throws NoSuchObjectException JavaDoc
90    {
91       handlermap.remove(obj);
92       Remote JavaDoc[] stubs = (Remote JavaDoc[])stubmap.remove(obj);
93       for (int i = 0; i < stubs.length; i++)
94       {
95          UnicastRemoteObject.unexportObject(stubs[i], force);
96       }
97       
98       return true;
99    }
100 }
101
Popular Tags