Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / gnu / java / rmi / server / UnicastServer.java
blob99aef483f9c5f1f2df05064899c333123a1a0e5b
1 /* UnicastServer.java --
2 Copyright (c) 1996, 1997, 1998, 1999, 2002, 2004
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA.
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
25 combination.
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
40 package gnu.java.rmi.server;
42 import gnu.java.rmi.dgc.DGCImpl;
44 import java.io.DataOutputStream;
45 import java.io.IOException;
46 import java.io.ObjectInputStream;
47 import java.io.ObjectOutputStream;
48 import java.rmi.NoSuchObjectException;
49 import java.rmi.Remote;
50 import java.rmi.RemoteException;
51 import java.rmi.ServerError;
52 import java.rmi.server.ObjID;
53 import java.rmi.server.UID;
54 import java.util.Hashtable;
56 public class UnicastServer
57 implements ProtocolConstants {
59 static private Hashtable objects = new Hashtable(); //mapping OBJID to server ref
60 static private Hashtable refcache = new Hashtable(); //mapping obj itself to server ref
61 static private DGCImpl dgc;
63 public static void exportObject(UnicastServerRef obj) {
64 startDGC();
65 objects.put(obj.objid, obj);
66 refcache.put(obj.myself, obj);
67 obj.manager.startServer();
70 // FIX ME: I haven't handle force parameter
71 public static boolean unexportObject(UnicastServerRef obj, boolean force) {
72 objects.remove(obj.objid);
73 refcache.remove(obj.myself);
74 obj.manager.stopServer();
75 return true;
78 public static UnicastServerRef getExportedRef(Remote remote){
79 return (UnicastServerRef)refcache.get(remote);
82 private static synchronized void startDGC() {
83 if (dgc == null) {
84 try {
85 dgc = new DGCImpl();
86 // Changed DGCImpl to inherit UnicastServerRef directly
87 //((UnicastServerRef)dgc.getRef()).exportObject(dgc);
88 dgc.exportObject(dgc);
90 catch (RemoteException e) {
91 e.printStackTrace();
96 public static void dispatch(UnicastConnection conn) throws Exception {
97 switch (conn.getDataInputStream().readUnsignedByte()) {
98 case MESSAGE_CALL:
99 incomingMessageCall(conn);
100 break;
101 case MESSAGE_PING:
102 // jdk sends a ping before each method call -> answer it!
103 DataOutputStream out = conn.getDataOutputStream();
104 out.writeByte(MESSAGE_PING_ACK);
105 out.flush();
106 break;
107 default:
108 throw new Exception("bad method type");
112 private static void incomingMessageCall(UnicastConnection conn) throws IOException {
113 ObjectInputStream in = conn.startObjectInputStream(); // (re)start ObjectInputStream
115 ObjID objid = ObjID.read(in);
116 int method = in.readInt();
117 long hash = in.readLong();
119 //System.out.println("ObjID: " + objid + ", method: " + method + ", hash: " + hash);
121 // Use the objid to locate the relevant UnicastServerRef
122 UnicastServerRef uref = (UnicastServerRef)objects.get(objid);
123 Object returnval;
124 int returncode = RETURN_ACK;
125 // returnval is from Method.invoke(), so we must check the return class to see
126 // if it's primitive type
127 Class returncls = null;
128 if (uref != null) {
129 try {
130 // Dispatch the call to it.
131 returnval = uref.incomingMessageCall(conn, method, hash);
132 returncls = uref.getMethodReturnType(method, hash);
134 catch (Exception e) {
135 returnval = e;
136 returncode = RETURN_NACK;
138 catch (Error e) {
139 returnval = new ServerError ("An Error is thrown while processing the invocation on the server", e);
140 returncode = RETURN_NACK;
143 else {
144 returnval = new NoSuchObjectException("");
145 returncode = RETURN_NACK;
148 conn.getDataOutputStream().writeByte(MESSAGE_CALL_ACK);
150 ObjectOutputStream out = conn.startObjectOutputStream(); // (re)start ObjectOutputStream
152 out.writeByte(returncode);
153 (new UID()).write(out);
155 //System.out.println("returnval=" + returnval + " returncls=" + returncls);
157 if(returnval != null && returncls != null)
158 ((RMIObjectOutputStream)out).writeValue(returnval, returncls);
160 // 1.1/1.2 void return type detection:
161 else if (!(returnval instanceof RMIVoidValue || returncls == Void.TYPE))
162 out.writeObject(returnval);
164 out.flush();