KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xmlrpc > XmlRpcClientWorker


1 /*
2  * Copyright 1999,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.xmlrpc;
19
20 import java.io.InputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22
23 /**
24  * Tie together the XmlRequestProcessor and XmlResponseProcessor to handle
25  * a request serially in a single thread.
26  *
27  * @author <a HREF="mailto:andrew@kungfoocoder.org">Andrew Evers</a>
28  * @since 2.0
29  */

30 public class XmlRpcClientWorker
31 {
32     protected XmlRpcClientRequestProcessor requestProcessor;
33     protected XmlRpcClientResponseProcessor responseProcessor;
34
35     /**
36      * Used as an internal marker value in {@link
37      * #execute(XmlRpcClientRequest, XmlRpcTransport)}.
38      */

39     private static final Object JavaDoc PROCESSING_ERROR_FLAG = new Object JavaDoc();
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 JavaDoc execute(XmlRpcClientRequest xmlRpcRequest,
56                           XmlRpcTransport transport)
57         throws XmlRpcException, XmlRpcClientException, IOException JavaDoc
58     {
59         long now = 0;
60         Object JavaDoc 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 JavaDoc is = transport.sendXmlRpc(request);
72             response = responseProcessor.decodeResponse(is);
73             return response;
74         }
75         catch (IOException JavaDoc ioe)
76         {
77             throw ioe;
78         }
79         catch (XmlRpcClientException xrce)
80         {
81             throw xrce;
82         }
83         catch (RuntimeException JavaDoc 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             // End the transport's session, handling any problem while
101
// avoiding hiding of any earlier exception.
102
try
103             {
104                 transport.endClientRequest();
105             }
106             catch (Throwable JavaDoc t)
107             {
108                 // Don't clobber an earlier exception.
109
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                     // else we've already thrown an exception
120
}
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     /**
138      * Called by the worker management framework to see if this worker can be
139      * re-used. Must attempt to clean up any state, and return true if it can
140      * be re-used.
141      *
142      * @return Whether this worker has been cleaned up and may be re-used.
143      */

144     // ### isReusable() would be a better name for this set of methods
145
protected boolean canReUse()
146     {
147         return responseProcessor.canReUse() && requestProcessor.canReUse();
148     }
149 }
150
Popular Tags