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)
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
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
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
;
43 import gnu
.java
.util
.WeakIdentityHashMap
;
45 import java
.io
.DataOutputStream
;
46 import java
.io
.IOException
;
47 import java
.io
.ObjectInputStream
;
48 import java
.io
.ObjectOutputStream
;
49 import java
.rmi
.NoSuchObjectException
;
50 import java
.rmi
.Remote
;
51 import java
.rmi
.RemoteException
;
52 import java
.rmi
.ServerError
;
53 import java
.rmi
.activation
.ActivationException
;
54 import java
.rmi
.activation
.ActivationID
;
55 import java
.rmi
.server
.ObjID
;
56 import java
.rmi
.server
.UID
;
57 import java
.util
.ArrayList
;
58 import java
.util
.Collection
;
59 import java
.util
.Collections
;
60 import java
.util
.Hashtable
;
61 import java
.util
.Iterator
;
63 import java
.util
.WeakHashMap
;
65 public class UnicastServer
66 implements ProtocolConstants
70 * Mapping OBJID to server ref by .equals().
72 static private Map objects
= Collections
.synchronizedMap(new WeakHashMap());
75 * Mapping obj itself to server ref by identity.
77 static private Map refcache
= Collections
.synchronizedMap(new WeakIdentityHashMap());
80 * Mapping the registered activatable objects into they server references.
82 public static Map actIds
= new Hashtable();
85 * The reference to the local distributed garbage collector.
87 static private DGCImpl dgc
;
90 * Connect this server reference to the server, allowing the local
91 * implementation, associated with this object, to receive remote calls.
93 * @param obj the server reference, encloses the (usually local) remote
96 public static void exportObject(UnicastServerRef obj
)
99 objects
.put(obj
.objid
, obj
);
100 refcache
.put(obj
.myself
, obj
);
101 obj
.manager
.startServer();
105 * Register the activatable object into the table of the activatable
108 public static void registerActivatable(ActivatableServerRef ref
)
110 actIds
.put(ref
.actId
, ref
);
114 * Export tha activatable object. The object id is placed into the map,
115 * but the object itself not. This is enough to deliver call to
116 * the ref.incomingMessageCall where the object will be instantiated,
119 public static void exportActivatableObject(ActivatableServerRef ref
)
122 objects
.put(ref
.objid
, ref
);
123 ref
.manager
.startServer();
124 actIds
.put(ref
.actId
, ref
);
129 * Get the activatable server reference that is handling activation of the
130 * given activation id.
132 public static ActivatableServerRef
getActivatableRef(ActivationID id
)
133 throws ActivationException
135 ActivatableServerRef ref
= (ActivatableServerRef
) actIds
.get(id
);
137 throw new ActivationException(id
+ " was not registered with this server");
142 * Unregister the previously registered activatable server reference.
144 public static void unregisterActivatable(ActivationID id
)
149 // FIX ME: I haven't handle force parameter
151 * Remove the given server reference. The remote object, associated with
152 * this reference, will no longer receive remote calls via this server.
154 public static boolean unexportObject(UnicastServerRef obj
, boolean force
)
156 objects
.remove(obj
.objid
);
157 refcache
.remove(obj
.myself
);
158 obj
.manager
.stopServer();
160 if (obj
instanceof ActivatableServerRef
)
162 ActivationID id
= ((ActivatableServerRef
) obj
).actId
;
163 unregisterActivatable(id
);
169 * Get the exported reference of the given Remote. The identity map is used,
170 * the non-null value will only be returned if exactly the passed remote
171 * is part of the registered UnicastServerRef.
173 * @param remote the Remote that is connected to this server via
174 * {@link UnicastServerRef}.
176 * @return the UnicastServerRef that is used to connect the passed
177 * remote with this server or null, if this Remote is not connected
180 public static UnicastServerRef
getExportedRef(Remote remote
)
182 return (UnicastServerRef
) refcache
.get(remote
);
186 * Get the server references to the object, previously exported via this
187 * server. As the identity map is scanned, more than one reference may match
190 * @param id the id of the exported object
191 * @return the server reference to this object, null if none.
193 public static Collection
getExported(Object id
)
195 synchronized (objects
)
197 ArrayList list
= new ArrayList();
198 Iterator iter
= objects
.entrySet().iterator();
201 while (iter
.hasNext())
203 e
= (Map
.Entry
) iter
.next();
205 if (key
!= null && key
.equals(id
))
206 list
.add(e
.getValue());
212 private static synchronized void startDGC()
219 // Changed DGCImpl to inherit UnicastServerRef directly
220 // ((UnicastServerRef)dgc.getRef()).exportObject(dgc);
221 dgc
.exportObject(dgc
);
223 catch (RemoteException e
)
230 public static void dispatch(UnicastConnection conn
) throws Exception
232 switch (conn
.getDataInputStream().readUnsignedByte())
235 incomingMessageCall(conn
);
238 // jdk sends a ping before each method call -> answer it!
239 DataOutputStream out
= conn
.getDataOutputStream();
240 out
.writeByte(MESSAGE_PING_ACK
);
244 throw new Exception("bad method type");
249 * This method is invoked when the remote call is received. The method
250 * dispatches the call to the responsible object, connected to this
251 * server via UnicastServerReference.
253 private static void incomingMessageCall(UnicastConnection conn
)
256 ObjectInputStream in
= conn
.startObjectInputStream(); // (re)start
259 ObjID objid
= ObjID
.read(in
);
260 int method
= in
.readInt();
261 long hash
= in
.readLong();
263 // System.out.println("ObjID: " + objid + ", method: " + method + ", hash: "
266 // Use the objid to locate the relevant UnicastServerRef
267 UnicastServerRef uref
= (UnicastServerRef
) objects
.get(objid
);
269 int returncode
= RETURN_ACK
;
270 // returnval is from Method.invoke(), so we must check the return class to
272 // if it's primitive type
273 Class returncls
= null;
278 // Dispatch the call to it.
279 returnval
= uref
.incomingMessageCall(conn
, method
, hash
);
280 returncls
= uref
.getMethodReturnType(method
, hash
);
285 returncode
= RETURN_NACK
;
289 returnval
= new ServerError(
290 "Server error, ObjID: " + objid
+
291 ", method: " + method
+ ", hash: "+ hash
, e
);
292 returncode
= RETURN_NACK
;
297 returnval
= new NoSuchObjectException("ObjID: " + objid
);
298 returncode
= RETURN_NACK
;
301 conn
.getDataOutputStream().writeByte(MESSAGE_CALL_ACK
);
303 ObjectOutputStream out
= conn
.startObjectOutputStream(); // (re)start
304 // ObjectOutputStream
306 out
.writeByte(returncode
);
307 (new UID()).write(out
);
309 // System.out.println("returnval=" + returnval + " returncls=" + returncls);
311 if (returnval
!= null && returncls
!= null)
312 ((RMIObjectOutputStream
) out
).writeValue(returnval
, returncls
);
314 // 1.1/1.2 void return type detection:
315 else if (! (returnval
instanceof RMIVoidValue
|| returncls
== Void
.TYPE
))
316 out
.writeObject(returnval
);