1 8 9 package mx4j.tools.remote.caucho; 10 11 import java.io.BufferedInputStream ; 12 import java.io.BufferedOutputStream ; 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.io.OutputStream ; 16 import java.lang.reflect.InvocationHandler ; 17 import java.lang.reflect.Method ; 18 import java.lang.reflect.Proxy ; 19 import java.net.URL ; 20 import java.net.URLConnection ; 21 22 import mx4j.log.Log; 23 import mx4j.log.Logger; 24 import mx4j.tools.remote.http.HTTPClientInvoker; 25 import mx4j.tools.remote.http.HTTPConnection; 26 27 30 public abstract class CauchoClientInvoker extends HTTPClientInvoker 31 { 32 private final String endpoint; 33 private final HTTPConnection service; 34 35 public CauchoClientInvoker(String endpoint) 36 { 37 this.endpoint = endpoint; 38 CauchoServiceProxy proxy = new CauchoServiceProxy(); 39 service = (HTTPConnection)Proxy.newProxyInstance(proxy.getClass().getClassLoader(), new Class []{HTTPConnection.class}, proxy); 40 } 41 42 protected HTTPConnection getService() 43 { 44 return service; 45 } 46 47 protected abstract CauchoInput createCauchoInput(InputStream stream); 48 49 protected abstract CauchoOutput createCauchoOutput(OutputStream stream); 50 51 private class CauchoServiceProxy implements InvocationHandler 52 { 53 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable 54 { 55 URLConnection connection = new URL (endpoint).openConnection(); 56 connection.setDoInput(true); 57 connection.setDoOutput(true); 58 connection.setUseCaches(false); 59 OutputStream os = new BufferedOutputStream (connection.getOutputStream()); 60 try 61 { 62 CauchoOutput output = createCauchoOutput(os); 63 startCall(output); 64 writeHeaders(output); 65 writeMethod(output, method); 66 writeArguments(output, args); 67 completeCall(output); 68 os.flush(); 69 70 InputStream is = new BufferedInputStream (connection.getInputStream()); 71 try 72 { 73 CauchoInput input = createCauchoInput(is); 74 input.startReply(); 75 Object result = input.readObject(null); 76 input.completeReply(); 77 return result; 78 } 79 catch (Throwable x) 80 { 81 Logger logger = Log.getLogger(CauchoClientInvoker.class.getName()); 82 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("", x); 83 throw x; 84 } 85 finally 86 { 87 is.close(); 88 } 89 } 90 finally 91 { 92 os.close(); 93 } 94 } 95 96 private void startCall(CauchoOutput output) throws IOException 97 { 98 output.startCall(); 99 } 100 101 private void writeHeaders(CauchoOutput output) throws IOException 102 { 103 output.writeHeader(CauchoService.CONNECTION_ID_HEADER_NAME); 104 output.writeObject(getConnectionId()); 105 } 106 107 private void writeMethod(CauchoOutput output, Method method) throws IOException 108 { 109 String methodName = mangleMethodName(method); 110 output.writeMethod(methodName); 111 } 112 113 private String mangleMethodName(Method method) 114 { 115 return CauchoService.mangleMethodName(method); 116 } 117 118 private void writeArguments(CauchoOutput output, Object [] args) throws IOException 119 { 120 if (args != null) for (int i = 0; i < args.length; ++i) output.writeObject(args[i]); 121 } 122 123 private void completeCall(CauchoOutput output) throws IOException 124 { 125 output.completeCall(); 126 } 127 } 128 } 129 | Popular Tags |