Burlap中hessian协议研究(五)协议的代理的实现

   在远程访问中服务端生成响应的骨架,在客户端访问远程服务必须使用响应的代理,

我们(⊙o⊙)看一看远程骨架类和远程代理的源代码分析吧。

  注意在看源代码时你必须学会反射机制哦,如果不会可以首先查看

  JDK API慢慢看也可以(⊙o⊙)哦

  反射机制使用很广泛希望不会的同时可以参考我的  Java基础有类似反射基础的东东个(⊙o⊙)哦

 同时必须Java的网络编程否则可能头疼?(+﹏+)~狂晕

 

  开始分析了~\(≧▽≦)/~啦啦啦~\(≧▽≦)/~啦啦啦

  客户端必须使用BurlapProxyFactory类

 

远程服务代理

package com.caucho.burlap.client;

 

import com.caucho.burlap.io.AbstractBurlapInput;

import com.caucho.burlap.io.BurlapOutput;

import java.io.*;

import java.lang.reflect.*;

import java.net.*;

使用JDK动态代理的实现代理

publicclass BurlapProxy

    implements InvocationHandler

{

 

    BurlapProxy(BurlapProxyFactory factory, URL url)

    {

        _factory = factory;

        _url = url;

    }

 

    public URL getURL()

    {

        return_url;

    }

 

   

动态代理必须实现的方式

    public Object invoke(Object proxy, Method method, Object args[])

        throws Throwable

    {

        String methodName;

        InputStream is;

        methodName = method.getName();

        Class params[] = method.getParameterTypes();

        if(methodName.equals("equals") && params.length == 1 && params[0].equals(java.lang.Object.class))

        {

            Object value = args[0];

            if(value == null || !Proxy.isProxyClass(value.getClass()))

            {

                returnnew Boolean(false);

            } else

            {

                BurlapProxy handler = (BurlapProxy)Proxy.getInvocationHandler(value);

                returnnew Boolean(_url.equals(handler.getURL()));

            }

        }

        if(methodName.equals("hashCode") && params.length == 0)

            returnnew Integer(_url.hashCode());

        if(methodName.equals("getBurlapType"))

            return proxy.getClass().getInterfaces()[0].getName();

        if(methodName.equals("getBurlapURL"))

            return_url.toString();

        if(methodName.equals("toString") && params.length == 0)

            return"BurlapProxy[" + _url + "]";

        is = null;

        Object obj;

        try

        {

            创建URLConnection对象根据url获取相应的响应流由上面的MicroBurlapOutput可以知道输出的流一定是一个关于服务信息的xml文件。

            URLConnection conn = _factory.openConnection(_url);

            conn.setRequestProperty("Content-Type", "text/xml");

            获取响应的信息

            OutputStream os = conn.getOutputStream();

            将响应的信息进行封装

            BurlapOutput out = _factory.getBurlapOutput(os);

            if(_factory.isOverloadEnabled())

                if(args != null)

                    methodName = methodName + "__" + args.length;

                else

                    methodName = methodName + "__0";

            out.call(methodName, args);

            os.flush();

            if(conn instanceof HttpURLConnection)

            {

                HttpURLConnection httpConn = (HttpURLConnection)conn;

                int code = 500;

                try

                {

                    code = httpConn.getResponseCode();

                }

                catch(Exception e)

                {

                    thrownew BurlapRuntimeException(String.valueOf(e));

                }

                注意响应的代码号非200即出现异常信息必须是处理异常,封装发送值客户端信息

                if(code != 200)

                {

                    StringBuffer sb = new StringBuffer();

                    try

                    {

                        is = httpConn.getInputStream();

                        int ch;

                        if(is != null)

                        {

                            while((ch = is.read()) >= 0)

                                sb.append((char)ch);

                            is.close();

                        }

                        is = httpConn.getErrorStream();

                        if(is != null)

                            while((ch = is.read()) >= 0)

                                sb.append((char)ch);

                    }

                    catch(FileNotFoundException e)

                    {

                        thrownew BurlapRuntimeException(String.valueOf(e));

                    }

                    catch(IOException e) { }

                    if(is != null)

                        is.close();

                    thrownew BurlapProtocolException(sb.toString());

                }

            }

catch(IOException e) { }

                    if(is != null)

                        is.close();

                    thrownew BurlapProtocolException(sb.toString());

                }

            }

            is = conn.getInputStream();

            AbstractBurlapInput in = _factory.getBurlapInput(is);

            obj = in.readReply(method.getReturnType());

        }

        catch(BurlapProtocolException e)

        {

            thrownew BurlapRuntimeException(e);

        }

        if(is != null)

            is.close();

        return obj;

 

    }

 

    private BurlapProxyFactory _factory;

    private URL _url;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值