Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / gnu / java / rmi / server / UnicastServer.java
bloba8da72563203f518ea77cfbba1551abfcee96241
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., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 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.Collections;
55 import java.util.Map;
56 import java.util.Hashtable;
57 import java.util.IdentityHashMap;
59 public class UnicastServer
60 implements ProtocolConstants {
62 static private Hashtable objects = new Hashtable(); //mapping OBJID to server ref
63 static private Map refcache = Collections.synchronizedMap(new IdentityHashMap()); //mapping obj itself to server ref
64 static private DGCImpl dgc;
66 public static void exportObject(UnicastServerRef obj) {
67 startDGC();
68 objects.put(obj.objid, obj);
69 refcache.put(obj.myself, obj);
70 obj.manager.startServer();
73 // FIX ME: I haven't handle force parameter
74 public static boolean unexportObject(UnicastServerRef obj, boolean force) {
75 objects.remove(obj.objid);
76 refcache.remove(obj.myself);
77 obj.manager.stopServer();
78 return true;
81 public static UnicastServerRef getExportedRef(Remote remote){
82 return (UnicastServerRef)refcache.get(remote);
85 private static synchronized void startDGC() {
86 if (dgc == null) {
87 try {
88 dgc = new DGCImpl();
89 // Changed DGCImpl to inherit UnicastServerRef directly
90 //((UnicastServerRef)dgc.getRef()).exportObject(dgc);
91 dgc.exportObject(dgc);
93 catch (RemoteException e) {
94 e.printStackTrace();
99 public static void dispatch(UnicastConnection conn) throws Exception {
100 switch (conn.getDataInputStream().readUnsignedByte()) {
101 case MESSAGE_CALL:
102 incomingMessageCall(conn);
103 break;
104 case MESSAGE_PING:
105 // jdk sends a ping before each method call -> answer it!
106 DataOutputStream out = conn.getDataOutputStream();
107 out.writeByte(MESSAGE_PING_ACK);
108 out.flush();
109 break;
110 default:
111 throw new Exception("bad method type");
115 private static void incomingMessageCall(UnicastConnection conn) throws IOException {
116 ObjectInputStream in = conn.startObjectInputStream(); // (re)start ObjectInputStream
118 ObjID objid = ObjID.read(in);
119 int method = in.readInt();
120 long hash = in.readLong();
122 //System.out.println("ObjID: " + objid + ", method: " + method + ", hash: " + hash);
124 // Use the objid to locate the relevant UnicastServerRef
125 UnicastServerRef uref = (UnicastServerRef)objects.get(objid);
126 Object returnval;
127 int returncode = RETURN_ACK;
128 // returnval is from Method.invoke(), so we must check the return class to see
129 // if it's primitive type
130 Class returncls = null;
131 if (uref != null) {
132 try {
133 // Dispatch the call to it.
134 returnval = uref.incomingMessageCall(conn, method, hash);
135 returncls = uref.getMethodReturnType(method, hash);
137 catch (Exception e) {
138 returnval = e;
139 returncode = RETURN_NACK;
141 catch (Error e) {
142 returnval = new ServerError ("An Error is thrown while processing the invocation on the server", e);
143 returncode = RETURN_NACK;
146 else {
147 returnval = new NoSuchObjectException("");
148 returncode = RETURN_NACK;
151 conn.getDataOutputStream().writeByte(MESSAGE_CALL_ACK);
153 ObjectOutputStream out = conn.startObjectOutputStream(); // (re)start ObjectOutputStream
155 out.writeByte(returncode);
156 (new UID()).write(out);
158 //System.out.println("returnval=" + returnval + " returncls=" + returncls);
160 if(returnval != null && returncls != null)
161 ((RMIObjectOutputStream)out).writeValue(returnval, returncls);
163 // 1.1/1.2 void return type detection:
164 else if (!(returnval instanceof RMIVoidValue || returncls == Void.TYPE))
165 out.writeObject(returnval);
167 out.flush();