Burlap中hessian协议研究(六)协议的服务端骨架的实现

响应的服务端骨架类:

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值