1 48 49 package com.caucho.burlap.client; 50 51 import com.caucho.burlap.io.AbstractBurlapInput; 52 import com.caucho.burlap.io.BurlapOutput; 53 54 import java.io.FileNotFoundException ; 55 import java.io.IOException ; 56 import java.io.InputStream ; 57 import java.io.OutputStream ; 58 import java.lang.reflect.InvocationHandler ; 59 import java.lang.reflect.Method ; 60 import java.lang.reflect.Proxy ; 61 import java.net.HttpURLConnection ; 62 import java.net.URL ; 63 import java.net.URLConnection ; 64 65 69 public class BurlapProxy implements InvocationHandler { 70 private BurlapProxyFactory _factory; 71 private URL _url; 72 73 BurlapProxy(BurlapProxyFactory factory, URL url) 74 { 75 _factory = factory; 76 _url = url; 77 } 78 79 82 public URL getURL() 83 { 84 return _url; 85 } 86 87 94 public Object invoke(Object proxy, Method method, Object []args) 95 throws Throwable 96 { 97 String methodName = method.getName(); 98 Class []params = method.getParameterTypes(); 99 100 if (methodName.equals("equals") && 102 params.length == 1 && params[0].equals(Object .class)) { 103 Object value = args[0]; 104 if (value == null || ! Proxy.isProxyClass(value.getClass())) 105 return new Boolean (false); 106 107 BurlapProxy handler = (BurlapProxy) Proxy.getInvocationHandler(value); 108 109 return new Boolean (_url.equals(handler.getURL())); 110 } 111 else if (methodName.equals("hashCode") && params.length == 0) 112 return new Integer (_url.hashCode()); 113 else if (methodName.equals("getBurlapType")) 114 return proxy.getClass().getInterfaces()[0].getName(); 115 else if (methodName.equals("getBurlapURL")) 116 return _url.toString(); 117 else if (methodName.equals("toString") && params.length == 0) 118 return "[BurlapProxy " + _url + "]"; 119 120 InputStream is = null; 121 122 URLConnection conn = null; 123 HttpURLConnection httpConn = null; 124 try { 125 conn = _factory.openConnection(_url); 126 conn.setRequestProperty("Content-Type", "text/xml"); 127 128 OutputStream os; 129 130 try { 131 os = conn.getOutputStream(); 132 } catch (Exception e) { 133 throw new BurlapRuntimeException(e); 134 } 135 136 BurlapOutput out = _factory.getBurlapOutput(os); 137 138 if (! _factory.isOverloadEnabled()) { 139 } 140 else if (args != null) 141 methodName = methodName + "__" + args.length; 142 else 143 methodName = methodName + "__0"; 144 145 out.call(methodName, args); 146 147 try { 148 os.flush(); 149 } catch (Exception e) { 150 throw new BurlapRuntimeException(e); 151 } 152 153 if (conn instanceof HttpURLConnection ) { 154 httpConn = (HttpURLConnection ) conn; 155 int code = 500; 156 157 try { 158 code = httpConn.getResponseCode(); 159 } catch (Exception e) { 160 } 161 162 if (code != 200) { 163 StringBuffer sb = new StringBuffer (); 164 int ch; 165 166 try { 167 is = httpConn.getInputStream(); 168 169 if (is != null) { 170 while ((ch = is.read()) >= 0) 171 sb.append((char) ch); 172 173 is.close(); 174 } 175 176 is = httpConn.getErrorStream(); 177 if (is != null) { 178 while ((ch = is.read()) >= 0) 179 sb.append((char) ch); 180 } 181 } catch (FileNotFoundException e) { 182 throw new BurlapRuntimeException(code + ": " + String.valueOf(e)); 183 } catch (IOException e) { 184 } 185 186 if (is != null) 187 is.close(); 188 189 throw new BurlapProtocolException(code + ": " + sb.toString()); 190 } 191 } 192 193 is = conn.getInputStream(); 194 195 AbstractBurlapInput in = _factory.getBurlapInput(is); 196 197 return in.readReply(method.getReturnType()); 198 } catch (BurlapProtocolException e) { 199 throw new BurlapRuntimeException(e); 200 } finally { 201 try { 202 if (is != null) 203 is.close(); 204 } catch (IOException e) { 205 } 206 207 if (httpConn != null) 208 httpConn.disconnect(); 209 } 210 } 211 } 212 | Popular Tags |