UnicastServerRef.java [plain text]
package gnu.java.rmi.server;
import java.io.ObjectInputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.RemoteStub;
import java.rmi.server.ObjID;
import java.rmi.server.ServerRef;
import java.rmi.server.RemoteServer;
import java.rmi.server.RemoteRef;
import java.rmi.server.ServerNotActiveException;
import java.rmi.server.RMIServerSocketFactory;
import java.rmi.server.Skeleton;
import java.util.Hashtable;
public class UnicastServerRef
extends UnicastRef
implements ServerRef{
final static private Class[] stubprototype = new Class[] { RemoteRef.class };
Remote myself; private Skeleton skel;
private RemoteStub stub;
private Hashtable methods = new Hashtable();
UnicastServerRef()
{
}
public UnicastServerRef(ObjID id, int port, RMIServerSocketFactory ssf) throws RemoteException {
super(id);
manager = UnicastConnectionManager.getInstance(port, ssf);
}
public RemoteStub exportObject(Remote obj) throws RemoteException {
if (myself == null) {
myself = obj;
manager.serverobj = obj;
Class cls = obj.getClass();
Class expCls;
try {
expCls = findStubSkelClass(cls);
} catch (Exception ex) {
throw new RemoteException("can not find stubs for class: " + cls, ex);
}
stub = (RemoteStub)getHelperClass(expCls, "_Stub");
if (stub == null) {
throw new RemoteException("failed to export: " + cls);
}
skel = (Skeleton)getHelperClass(expCls, "_Skel");
buildMethodHash(obj.getClass(), true);
UnicastServer.exportObject(this);
}
return (stub);
}
public RemoteStub exportObject(Remote remote, Object obj)
throws RemoteException
{
return exportObject(remote);
}
public RemoteStub getStub(){
return stub;
}
public boolean unexportObject(Remote obj, boolean force) {
buildMethodHash(obj.getClass(), false);
return UnicastServer.unexportObject(this, force);
}
private Class findStubSkelClass(Class startCls) throws Exception {
Class cls = startCls;
while (true) {
try {
String stubClassname = cls.getName() + "_Stub";
ClassLoader cl = cls.getClassLoader();
Class scls = cl == null ? Class.forName(stubClassname)
: cl.loadClass(stubClassname);
return cls; } catch (ClassNotFoundException e) {
Class superCls = cls.getSuperclass();
if (superCls == null
|| superCls == java.rmi.server.UnicastRemoteObject.class)
{
throw new Exception("Neither " + startCls
+ " nor one of their superclasses (like" + cls + ")"
+ " has a _Stub");
}
cls = superCls;
}
}
}
private Object getHelperClass(Class cls, String type) {
try {
String classname = cls.getName();
ClassLoader cl = cls.getClassLoader();
Class scls = cl == null ? Class.forName(classname + type)
: cl.loadClass(classname + type);
if (type.equals("_Stub")) {
try {
Constructor con = scls.getConstructor(stubprototype);
return (con.newInstance(new Object[]{this}));
}
catch (NoSuchMethodException e) {
}
catch (InstantiationException e) {
}
catch (IllegalAccessException e) {
}
catch (IllegalArgumentException e) {
}
catch (InvocationTargetException e) {
}
RemoteStub stub = (RemoteStub)scls.newInstance();
UnicastRemoteStub.setStubRef(stub, this);
return (stub);
}
else {
return (scls.newInstance());
}
}
catch (ClassNotFoundException e) {
}
catch (InstantiationException e) {
}
catch (IllegalAccessException e) {
}
return (null);
}
public String getClientHost() throws ServerNotActiveException {
return RemoteServer.getClientHost();
}
private void buildMethodHash(Class cls, boolean build) {
Method[] meths = cls.getMethods();
for (int i = 0; i < meths.length; i++) {
if (meths[i].getDeclaringClass().getName().startsWith("java.")) {
continue;
}
long hash = RMIHashes.getMethodHash(meths[i]);
if(build)
methods.put(new Long (hash), meths[i]);
else
methods.remove(new Long (hash));
}
}
Class getMethodReturnType(int method, long hash) throws Exception
{
if (method == -1) {
Method meth = (Method)methods.get(new Long (hash));
return meth.getReturnType();
}else
return null;
}
public Object incomingMessageCall(UnicastConnection conn, int method, long hash) throws Exception {
if (method == -1) {
Method meth = (Method)methods.get(new Long (hash));
if (meth == null) {
throw new NoSuchMethodException();
}
ObjectInputStream in = conn.getObjectInputStream();
int nrargs = meth.getParameterTypes().length;
Object[] args = new Object[nrargs];
for (int i = 0; i < nrargs; i++) {
try {
args[i] = ((RMIObjectInputStream)in).readValue(meth.getParameterTypes()[i]);
}
catch (Exception t) {
t.printStackTrace();
throw t;
}
}
Object ret = null;
try{
ret = meth.invoke(myself, args);
}catch(InvocationTargetException e){
Throwable cause = e.getTargetException();
if (cause instanceof Exception) {
throw (Exception)cause;
}
else if (cause instanceof Error) {
throw (Error)cause;
}
else {
throw new Error("The remote method threw a java.lang.Throwable that is neither java.lang.Exception nor java.lang.Error.", e);
}
}
return ret;
}
else {
if (skel == null) {
throw new NoSuchMethodException();
}
UnicastRemoteCall call = new UnicastRemoteCall(conn);
skel.dispatch(myself, call, method, hash);
if (!call.isReturnValue())
return RMIVoidValue.INSTANCE;
else
return (call.returnValue());
}
}
}