1 16 17 18 package org.apache.xmlrpc; 19 20 import java.io.InputStream ; 21 import java.io.IOException ; 22 23 30 public class XmlRpcClientWorker 31 { 32 protected XmlRpcClientRequestProcessor requestProcessor; 33 protected XmlRpcClientResponseProcessor responseProcessor; 34 35 39 private static final Object PROCESSING_ERROR_FLAG = new Object (); 40 41 public XmlRpcClientWorker() 42 { 43 this(new XmlRpcClientRequestProcessor(), 44 new XmlRpcClientResponseProcessor() 45 ); 46 } 47 48 public XmlRpcClientWorker(XmlRpcClientRequestProcessor requestProcessor, 49 XmlRpcClientResponseProcessor responseProcessor) 50 { 51 this.requestProcessor = requestProcessor; 52 this.responseProcessor = responseProcessor; 53 } 54 55 public Object execute(XmlRpcClientRequest xmlRpcRequest, 56 XmlRpcTransport transport) 57 throws XmlRpcException, XmlRpcClientException, IOException 58 { 59 long now = 0; 60 Object response = PROCESSING_ERROR_FLAG; 61 62 if (XmlRpc.debug) 63 { 64 now = System.currentTimeMillis(); 65 } 66 67 try 68 { 69 byte[] request = requestProcessor.encodeRequestBytes 70 (xmlRpcRequest, responseProcessor.getEncoding()); 71 InputStream is = transport.sendXmlRpc(request); 72 response = responseProcessor.decodeResponse(is); 73 return response; 74 } 75 catch (IOException ioe) 76 { 77 throw ioe; 78 } 79 catch (XmlRpcClientException xrce) 80 { 81 throw xrce; 82 } 83 catch (RuntimeException x) 84 { 85 if (XmlRpc.debug) 86 { 87 x.printStackTrace(); 88 } 89 throw new XmlRpcClientException 90 ("Unexpected exception in client processing", x); 91 } 92 finally 93 { 94 if (XmlRpc.debug) 95 { 96 System.out.println("Spent " + (System.currentTimeMillis() - now) 97 + " millis in request/process/response"); 98 } 99 100 try 103 { 104 transport.endClientRequest(); 105 } 106 catch (Throwable t) 107 { 108 boolean haveFault = response instanceof XmlRpcException; 110 if (haveFault || response == PROCESSING_ERROR_FLAG) 111 { 112 System.err.println("Avoiding obscuring previous error " + 113 "by supressing error encountered " + 114 "while ending request: " + t); 115 if (haveFault) 116 { 117 throw (XmlRpcException) response; 118 } 119 } 121 else 122 { 123 if (t instanceof XmlRpcException) 124 { 125 throw (XmlRpcException) t; 126 } 127 else 128 { 129 throw new XmlRpcClientException 130 ("Unable to end request", t); 131 } 132 } 133 } 134 } 135 } 136 137 144 protected boolean canReUse() 146 { 147 return responseProcessor.canReUse() && requestProcessor.canReUse(); 148 } 149 } 150 | Popular Tags |