响应的服务端骨架类:
package com.caucho.burlap.server;
import com.caucho.burlap.io.BurlapInput;
import com.caucho.burlap.io.BurlapOutput;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
public class BurlapSkeleton
{
public BurlapSkeleton(Object service)
{
_methodMap = new HashMap();
_service = service;
Method methodList[] = service.getClass().getMethods();
for(int i = 0; i < methodList.length; i++)
{
Method method = methodList[i];
if(_methodMap.get(method.getName()) == null)
_methodMap.put(method.getName(), methodList[i]);
Class param[] = method.getParameterTypes();
String mangledName = method.getName() + "__" + param.length;
_methodMap.put(mangledName, methodList[i]);
_methodMap.put(mangleName(method, false), methodList[i]);
}
}
protected String mangleName(Method method, boolean isFull)
{
StringBuffer sb = new StringBuffer();
sb.append(method.getName());
Class params[] = method.getParameterTypes();
for(int i = 0; i < params.length; i++)
{
sb.append('_');
mangleClass(sb, params[i], isFull);
}
return sb.toString();
}
private void mangleClass(StringBuffer sb, Class cl, boolean isFull)
{
String name = cl.getName();
if(name.equals("boolean") || name.equals("java.lang.Boolean"))
sb.append("boolean");
else
if(name.equals("int") || name.equals("java.lang.Integer") || name.equals("short") || name.equals("java.lang.Short") || name.equals("byte") || name.equals("java.lang.Byte"))
sb.append("int");
else
if(name.equals("long") || name.equals("java.lang.Long"))
sb.append("long");
else
if(name.equals("float") || name.equals("java.lang.Float") || name.equals("double") || name.equals("java.lang.Double"))
sb.append("double");
else
if(name.equals("java.lang.String") || name.equals("com.caucho.util.CharBuffer") || name.equals("char") || name.equals("java.lang.Character") || name.equals("java.io.Reader"))
sb.append("string");
else
if(name.equals("java.util.Date") || name.equals("com.caucho.util.QDate"))
sb.append("date");
else
if((java.io.InputStream.class).isAssignableFrom(cl) || name.equals("[B"))
sb.append("binary");
else
if(cl.isArray())
{
sb.append("[");
mangleClass(sb, cl.getComponentType(), isFull);
} else
if(name.equals("org.w3c.dom.Node") || name.equals("org.w3c.dom.Element") || name.equals("org.w3c.dom.Document"))
sb.append("xml");
else
if(isFull)
{
sb.append(name);
} else
{
int p = name.lastIndexOf('.');
if(p > 0)
sb.append(name.substring(p + 1));
else
sb.append(name);
}
}
public void invoke(BurlapInput in, BurlapOutput out)
throws Throwable
{
in.startCall();
String methodName = in.getMethod();
Method method = (Method)_methodMap.get(methodName);
if(method == null)
if(methodName.equals("_burlap_getAttribute"))
{
getBurlapAttribute(in, out);
return;
} else
{
throw new IOException("No server method matching:" + in.getMethod());
}
Class args[] = method.getParameterTypes();
Object values[] = new Object[args.length];
for(int i = 0; i < args.length; i++)
values[i] = in.readObject(args[i]);
in.completeCall();
Object result = null;
try
{
result = method.invoke(_service, values);
}
catch(Throwable e)
{
if(e instanceof InvocationTargetException)
e = ((InvocationTargetException)e).getTargetException();
out.startReply();
out.writeFault("ServiceException", e.getMessage(), e);
out.completeReply();
return;
}
out.startReply();
out.writeObject(result);
out.completeReply();
}
public void getBurlapAttribute(BurlapInput in, BurlapOutput out)
throws IOException
{
String name = in.readString();
in.completeCall();
out.startReply();
if(name.equals("home-class") || name.equals("remote-class"))
{
Class cl = _service.getClass();
Class interfaces[] = cl.getInterfaces();
String homeClass = null;
for(int i = 0; i < interfaces.length; i++)
if((java.rmi.Remote.class).isAssignableFrom(interfaces[i]) && (java.rmi.Remote.class) != interfaces[i])
homeClass = interfaces[i].getName();
if(homeClass == null)
homeClass = cl.getName();
out.writeString(homeClass);
} else
{
out.writeObject(null);
}
out.completeReply();
}
private HashMap _methodMap;
private Object _service;
}