六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 45|回复: 0

远程技术的研究

[复制链接]

升级  73.8%

273

主题

273

主题

273

主题

进士

Rank: 4

积分
869
 楼主| 发表于 2013-1-15 03:00:08 | 显示全部楼层 |阅读模式
动态代理必须实现的方式

    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()))

            {

                return new Boolean(false);

            } else

            {

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

                return new Boolean(_url.equals(handler.getURL()));

            }

        }

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

            return new 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)

                {

                    throw new 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)

                    {

                        throw new BurlapRuntimeException(String.valueOf(e));

                    }

                    catch(IOException e) { }

                    if(is != null)

                        is.close();

                    throw new BurlapProtocolException(sb.toString());

                }


            }<
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表