`
goon
  • 浏览: 181115 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

hessian——源码分析(二)------ HessianProxy《转》

阅读更多

Hessian在客户端一块采用Proxy模式,当客户端调用远程接口时,HessianProxy会代理这个动作,在invoke方法中,把客户端请求的方法和参数序列化为预订格式的输出流,主要流程如下图所示:

 

下面我将详细解析一下invoke源码:

 

[java] view plaincopy
  1. public Object invoke(Object proxy, Method method, Object []args)  
  2.     throws Throwable  
  3.   {  
  4.     String mangleName;  
  5.     synchronized (_mangleMap) {  
  6.       mangleName = _mangleMap.get(method);  
  7.     }  
  8.     if (mangleName == null) {  
  9.       String methodName = method.getName();  
  10.       Class []params = method.getParameterTypes();  
  11.       // equals and hashCode are special cased  
  12.       if (methodName.equals("equals")  
  13.       && params.length == 1 && params[0].equals(Object.class)) {  
  14.     Object value = args[0];  
  15.     if (value == null || ! Proxy.isProxyClass(value.getClass()))  
  16.       return new Boolean(false);  
  17.     HessianProxy handler = (HessianProxy) Proxy.getInvocationHandler(value);  
  18.     return new Boolean(_url.equals(handler.getURL()));  
  19.       }  
  20.       else if (methodName.equals("hashCode") && params.length == 0)  
  21.     return new Integer(_url.hashCode());  
  22.       else if (methodName.equals("getHessianType"))  
  23.     return proxy.getClass().getInterfaces()[0].getName();  
  24.       else if (methodName.equals("getHessianURL"))  
  25.     return _url.toString();  
  26.       else if (methodName.equals("toString") && params.length == 0)  
  27.     return "HessianProxy[" + _url + "]";  
  28.         
  29.       if (! _factory.isOverloadEnabled())  
  30.     mangleName = method.getName();  
  31.       else  
  32.         mangleName = mangleName(method);  
  33.       synchronized (_mangleMap) {  
  34.     _mangleMap.put(method, mangleName);  
  35.       }  
  36.     }  
  37.     ......  
  38. }  

 

这是invoke的开头,主要干的事情是把methodName缓存起来和过滤一些特殊调用,java反射是个比较耗性能的操作,把methodName缓存起来可以避免每次调用都要从method里得到methodName。另外,对equals、hashCode、getHessianType、getHessianURL等特殊方法的远程调用,HessianProxy不会走远程调用,而是直接返回。

接着往下看:

 

[java] view plaincopy
  1. public Object invoke(Object proxy, Method method, Object []args)  
  2.     throws Throwable  
  3.   {  
  4.     ......  
  5.     InputStream is = null;  
  6.     URLConnection conn = null;  
  7.     HttpURLConnection httpConn = null;  
  8.       
  9.     try {  
  10.              
  11.       conn = sendRequest(mangleName, args);  
  12.      ......  
  13.     } catch (HessianProtocolException e) {  
  14.       throw new HessianRuntimeException(e);  
  15.     } finally {  
  16.      ......  
  17.      }  
  18.   }  

 

这段主要是把方法名和参数序列化为网络输出流,并做网络请求,具体逻辑包装在sendRequest方法中:

 

[java] view plaincopy
  1. protected URLConnection sendRequest(String methodName, Object []args)  
  2.     throws IOException  
  3.   {  
  4.     URLConnection conn = null;  
  5.       
  6.     conn = _factory.openConnection(_url);  
  7.     boolean isValid = false;  
  8.     try {  
  9.       // Used chunked mode when available, i.e. JDK 1.5.  
  10.       if (_factory.isChunkedPost() && conn instanceof HttpURLConnection) {  
  11.     try {  
  12.       HttpURLConnection httpConn = (HttpURLConnection) conn;  
  13.       httpConn.setChunkedStreamingMode(8 * 1024);  
  14.     } catch (Throwable e) {  
  15.     }  
  16.       }  
  17.       
  18.       addRequestHeaders(conn);  
  19.       OutputStream os = null;  
  20.       try {  
  21.     os = conn.getOutputStream();  
  22.       } catch (Exception e) {  
  23.     throw new HessianRuntimeException(e);  
  24.       }  
  25.       if (log.isLoggable(Level.FINEST)) {  
  26.     PrintWriter dbg = new PrintWriter(new LogWriter(log));  
  27.     os = new HessianDebugOutputStream(os, dbg);  
  28.       }  
  29.         
  30.       AbstractHessianOutput out = _factory.getHessianOutput(os);  
  31.       out.call(methodName, args);  
  32.       out.flush();  
  33.       isValid = true;  
  34.       return conn;  
  35.     } finally {  
  36.       if (! isValid && conn instanceof HttpURLConnection)  
  37.     ((HttpURLConnection) conn).disconnect();  
  38.     }  
  39.   }  

 

sendRequest的主要流程是先初始化网络连接,然后用AbstractHessianOutput包装网络输出流,调用AbstractHessianOutput.call(methodName, args)完成网络输出,这个方法的细节会在hessian io的源码解析中详细分析。

下面接着看invoke方法:

 

[java] view plaincopy
  1. public Object invoke(Object proxy, Method method, Object []args)  
  2.     throws Throwable  
  3.   {  
  4.       ......  
  5.       is = conn.getInputStream();  
  6.       AbstractHessianInput in = _factory.getHessianInput(is);  
  7.       in.startReply();  
  8.       Object value = in.readObject(method.getReturnType());  
  9.       if (value instanceof InputStream) {  
  10.     value = new ResultInputStream(httpConn, is, in, (InputStream) value);  
  11.     is = null;  
  12.     httpConn = null;  
  13.       }  
  14.       else  
  15.     in.completeReply();  
  16.       ......  
  17.   }  

 

这一段主要是把输入流中取得返回值,具体是用AbstractHessianInput包装网络输入流,然后调用AbstractHessianInput.readObject从网络流中反序列化到实际返回值。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics