2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / gnu / java / rmi / server / UnicastServer.java
blobfb6ec1f35a5e61ebef712becf2c7b3316597162d
1 /*
2 Copyright (c) 1996, 1997, 1998, 1999, 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.java.rmi.server;
40 import java.io.DataInputStream;
41 import java.io.DataOutputStream;
42 import java.io.ObjectInputStream;
43 import java.io.ObjectOutputStream;
44 import java.io.IOException;
45 import java.net.InetAddress;
46 import java.util.Hashtable;
47 import java.net.UnknownHostException;
48 import java.rmi.Remote;
49 import java.rmi.server.ObjID;
50 import java.rmi.server.UnicastRemoteObject;
51 import java.rmi.server.UID;
52 import java.rmi.server.RemoteRef;
53 import java.rmi.RemoteException;
54 import java.rmi.NoSuchObjectException;
55 import gnu.java.rmi.dgc.DGCImpl;
57 public class UnicastServer
58 implements ProtocolConstants {
60 static private Hashtable objects = new Hashtable(); //mapping OBJID to server ref
61 static private Hashtable refcache = new Hashtable(); //mapping obj itself to server ref
62 static private DGCImpl dgc;
64 public static void exportObject(UnicastServerRef obj) {
65 startDGC();
66 objects.put(obj.objid, obj);
67 refcache.put(obj.myself, obj);
68 obj.manager.startServer();
71 // FIX ME: I haven't handle force parameter
72 public static boolean unexportObject(UnicastServerRef obj, boolean force) {
73 objects.remove(obj.objid);
74 refcache.remove(obj.myself);
75 obj.manager.stopServer();
76 return true;
79 public static UnicastServerRef getExportedRef(Remote remote){
80 return (UnicastServerRef)refcache.get(remote);
83 private static synchronized void startDGC() {
84 if (dgc == null) {
85 try {
86 dgc = new DGCImpl();
87 // Changed DGCImpl to inherit UnicastServerRef directly
88 //((UnicastServerRef)dgc.getRef()).exportObject(dgc);
89 dgc.exportObject(dgc);
91 catch (RemoteException e) {
92 e.printStackTrace();
97 public static void dispatch(UnicastConnection conn) throws Exception {
98 switch (conn.getDataInputStream().readUnsignedByte()) {
99 case MESSAGE_CALL:
100 incomingMessageCall(conn);
101 break;
102 default:
103 throw new Exception("bad method type");
107 private static void incomingMessageCall(UnicastConnection conn) throws IOException {
108 ObjectInputStream in = conn.getObjectInputStream();
110 ObjID objid = ObjID.read(in);
111 int method = in.readInt();
112 long hash = in.readLong();
114 //System.out.println("ObjID: " + objid + ", method: " + method + ", hash: " + hash);
116 // Use the objid to locate the relevant UnicastServerRef
117 UnicastServerRef uref = (UnicastServerRef)objects.get(objid);
118 Object returnval;
119 int returncode = RETURN_ACK;
120 // returnval is from Method.invoke(), so we must check the return class to see
121 // if it's primitive type
122 Class returncls = null;
123 if (uref != null) {
124 try {
125 // Dispatch the call to it.
126 returnval = uref.incomingMessageCall(conn, method, hash);
127 returncls = uref.getMethodReturnType(method, hash);
129 catch (Exception e) {
130 returnval = e;
131 returncode = RETURN_NACK;
134 else {
135 returnval = new NoSuchObjectException("");
136 returncode = RETURN_NACK;
139 conn.getDataOutputStream().writeByte(MESSAGE_CALL_ACK);
141 ObjectOutputStream out = conn.getObjectOutputStream();
143 out.writeByte(returncode);
144 (new UID()).write(out);
145 if(returnval != null && returncls != null)
146 ((RMIObjectOutputStream)out).writeValue(returnval, returncls);
147 else if (!(returnval instanceof RMIVoidValue))
148 out.writeObject(returnval);
150 out.flush();