1 48 49 package com.caucho.hessian.client; 50 51 import com.caucho.hessian.io.AbstractHessianInput; 52 import com.caucho.hessian.io.AbstractHessianOutput; 53 import com.caucho.hessian.io.HessianProtocolException; 54 55 import java.io.FileNotFoundException ; 56 import java.io.IOException ; 57 import java.io.InputStream ; 58 import java.io.OutputStream ; 59 import java.lang.reflect.InvocationHandler ; 60 import java.lang.reflect.Method ; 61 import java.lang.reflect.Proxy ; 62 import java.net.HttpURLConnection ; 63 import java.net.URL ; 64 import java.net.URLConnection ; 65 66 70 public class HessianProxy implements InvocationHandler { 71 private HessianProxyFactory _factory; 72 private URL _url; 73 74 HessianProxy(HessianProxyFactory factory, URL url) 75 { 76 _factory = factory; 77 _url = url; 78 } 79 80 83 public URL getURL() 84 { 85 return _url; 86 } 87 88 95 public Object invoke(Object proxy, Method method, Object []args) 96 throws Throwable 97 { 98 String methodName = method.getName(); 99 Class []params = method.getParameterTypes(); 100 101 if (methodName.equals("equals") && 103 params.length == 1 && params[0].equals(Object .class)) { 104 Object value = args[0]; 105 if (value == null || ! Proxy.isProxyClass(value.getClass())) 106 return new Boolean (false); 107 108 HessianProxy handler = (HessianProxy) Proxy.getInvocationHandler(value); 109 110 return new Boolean (_url.equals(handler.getURL())); 111 } 112 else if (methodName.equals("hashCode") && params.length == 0) 113 return new Integer (_url.hashCode()); 114 else if (methodName.equals("getHessianType")) 115 return proxy.getClass().getInterfaces()[0].getName(); 116 else if (methodName.equals("getHessianURL")) 117 return _url.toString(); 118 else if (methodName.equals("toString") && params.length == 0) 119 return "[HessianProxy " + _url + "]"; 120 121 InputStream is = null; 122 URLConnection conn = null; 123 HttpURLConnection httpConn = null; 124 125 try { 126 if (! _factory.isOverloadEnabled()) { 127 } 128 else if (args != null) 129 methodName = methodName + "__" + args.length; 130 else 131 methodName = methodName + "__0"; 132 133 conn = sendRequest(methodName, args); 134 135 if (conn instanceof HttpURLConnection ) { 136 httpConn = (HttpURLConnection ) conn; 137 int code = 500; 138 139 try { 140 code = httpConn.getResponseCode(); 141 } catch (Exception e) { 142 } 143 144 if (code != 200) { 145 StringBuffer sb = new StringBuffer (); 146 int ch; 147 148 try { 149 is = httpConn.getInputStream(); 150 151 if (is != null) { 152 while ((ch = is.read()) >= 0) 153 sb.append((char) ch); 154 155 is.close(); 156 } 157 158 is = httpConn.getErrorStream(); 159 if (is != null) { 160 while ((ch = is.read()) >= 0) 161 sb.append((char) ch); 162 } 163 } catch (FileNotFoundException e) { 164 throw new HessianRuntimeException(String.valueOf(e)); 165 } catch (IOException e) { 166 if (is == null) 167 throw new HessianProtocolException(code + ": " + e, e); 168 } 169 170 if (is != null) 171 is.close(); 172 173 throw new HessianProtocolException(code + ": " + sb.toString()); 174 } 175 } 176 177 is = conn.getInputStream(); 178 179 AbstractHessianInput in = _factory.getHessianInput(is); 180 181 return in.readReply(method.getReturnType()); 182 } catch (HessianProtocolException e) { 183 throw new HessianRuntimeException(e); 184 } finally { 185 try { 186 if (is != null) 187 is.close(); 188 } catch (Throwable e) { 189 } 190 191 try { 192 if (httpConn != null) 193 httpConn.disconnect(); 194 } catch (Throwable e) { 195 } 196 } 197 } 198 199 private URLConnection sendRequest(String methodName, Object []args) 200 throws IOException 201 { 202 URLConnection conn = null; 203 204 conn = _factory.openConnection(_url); 205 206 if (_factory.isChunkedPost() && conn instanceof HttpURLConnection ) { 208 try { 209 HttpURLConnection httpConn = (HttpURLConnection ) conn; 210 211 httpConn.setChunkedStreamingMode(8 * 1024); 212 } catch (Throwable e) { 213 } 214 } 215 216 OutputStream os = null; 217 218 try { 219 os = conn.getOutputStream(); 220 } catch (Exception e) { 221 throw new HessianRuntimeException(e); 222 } 223 224 try { 225 AbstractHessianOutput out = _factory.getHessianOutput(os); 226 227 out.call(methodName, args); 228 out.flush(); 229 230 return conn; 231 } catch (IOException e) { 232 if (conn instanceof HttpURLConnection ) 233 ((HttpURLConnection ) conn).disconnect(); 234 235 throw e; 236 } catch (RuntimeException e) { 237 if (conn instanceof HttpURLConnection ) 238 ((HttpURLConnection ) conn).disconnect(); 239 240 throw e; 241 } 242 } 243 } 244 | Popular Tags |