1 29 30 package com.caucho.server.admin; 31 32 import com.caucho.hessian.io.AbstractHessianInput; 33 import com.caucho.hessian.io.AbstractHessianOutput; 34 import com.caucho.hessian.io.Hessian2Input; 35 import com.caucho.hessian.io.HessianOutput; 36 import com.caucho.hessian.io.HessianProtocolException; 37 import com.caucho.util.CharBuffer; 38 import com.caucho.vfs.Path; 39 import com.caucho.vfs.ReadStream; 40 import com.caucho.vfs.ReadWritePair; 41 import com.caucho.vfs.WriteStream; 42 43 import java.io.IOException ; 44 import java.lang.reflect.InvocationHandler ; 45 import java.lang.reflect.Method ; 46 import java.lang.reflect.Proxy ; 47 48 52 public class HessianHmuxProxy implements InvocationHandler { 53 private Path _path; 54 55 private HessianHmuxProxy(Path url) 56 { 57 _path = url; 58 } 59 60 public static <X> X create(Path url, Class <X> api) 61 { 62 Thread thread = Thread.currentThread(); 63 64 return (X) Proxy.newProxyInstance(thread.getContextClassLoader(), 65 new Class [] { api }, 66 new HessianHmuxProxy(url)); 67 } 68 69 76 public Object invoke(Object proxy, Method method, Object []args) 77 throws Throwable 78 { 79 String methodName = method.getName(); 80 Class []params = method.getParameterTypes(); 81 82 if (methodName.equals("equals") && 84 params.length == 1 && params[0].equals(Object .class)) { 85 Object value = args[0]; 86 if (value == null || ! Proxy.isProxyClass(value.getClass())) 87 return new Boolean (false); 88 89 HessianHmuxProxy handler = (HessianHmuxProxy) Proxy.getInvocationHandler(value); 90 91 return new Boolean (_path.equals(handler._path)); 92 } 93 else if (methodName.equals("hashCode") && params.length == 0) 94 return new Integer (_path.hashCode()); 95 else if (methodName.equals("getHessianType")) 96 return proxy.getClass().getInterfaces()[0].getName(); 97 else if (methodName.equals("getHessianURL")) 98 return _path.toString(); 99 else if (methodName.equals("toString") && params.length == 0) 100 return "HessianHmuxProxy[" + _path + "]"; 101 102 ReadStream is = null; 103 104 try { 105 if (args != null) 106 methodName = methodName + "__" + args.length; 107 else 108 methodName = methodName + "__0"; 109 110 is = sendRequest(methodName, args); 111 112 String code = (String ) is.getAttribute("status"); 113 114 if (! "200".equals(code)) { 115 CharBuffer sb = new CharBuffer(); 116 117 while (is.readLine(sb)) { 118 } 119 120 throw new HessianProtocolException(code + ": " + sb); 121 } 122 123 AbstractHessianInput in = new Hessian2Input(is); 124 125 return in.readReply(method.getReturnType()); 126 } catch (HessianProtocolException e) { 127 throw new RuntimeException (e); 128 } finally { 129 try { 130 if (is != null) 131 is.close(); 132 } catch (Throwable e) { 133 } 134 } 135 } 136 137 private ReadStream sendRequest(String methodName, Object []args) 138 throws IOException 139 { 140 ReadWritePair pair = _path.openReadWrite(); 141 142 ReadStream is = pair.getReadStream(); 143 WriteStream os = pair.getWriteStream(); 144 145 try { 146 AbstractHessianOutput out = new HessianOutput(os); 147 148 out.call(methodName, args); 149 out.flush(); 150 151 return is; 152 } catch (IOException e) { 153 try { 154 os.close(); 155 } catch (Exception e1) { 156 } 157 158 try { 159 is.close(); 160 } catch (Exception e1) { 161 } 162 163 throw e; 164 } catch (RuntimeException e) { 165 try { 166 os.close(); 167 } catch (Exception e1) { 168 } 169 170 try { 171 is.close(); 172 } catch (Exception e1) { 173 } 174 175 throw e; 176 } 177 } 178 } 179 | Popular Tags |