Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
XmlRpcClientWorker |
|
| 1.5;1,5 |
1 | 0 | /* |
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 | package org.apache.xmlrpc.client; |
|
17 | ||
18 | import org.apache.xmlrpc.XmlRpcException; |
|
19 | import org.apache.xmlrpc.XmlRpcRequest; |
|
20 | import org.apache.xmlrpc.common.XmlRpcController; |
|
21 | import org.apache.xmlrpc.common.XmlRpcWorker; |
|
22 | ||
23 | ||
24 | /** Object, which performs a request on the clients behalf. |
|
25 | * The client maintains a pool of workers. The main purpose of the |
|
26 | * pool is limitation of the maximum number of concurrent requests. |
|
27 | * @since 3.0 |
|
28 | */ |
|
29 | public class XmlRpcClientWorker implements XmlRpcWorker { |
|
30 | 0 | private final XmlRpcClientWorkerFactory factory; |
31 | ||
32 | /** Creates a new instance. |
|
33 | * @param pFactory The factory, which is being notified, if |
|
34 | * the worker's ready. |
|
35 | */ |
|
36 | 202 | public XmlRpcClientWorker(XmlRpcClientWorkerFactory pFactory) { |
37 | 202 | factory = pFactory; |
38 | 202 | } |
39 | ||
40 | public XmlRpcController getController() { |
|
41 | 432 | return factory.getController(); |
42 | } |
|
43 | ||
44 | /** Performs a synchronous request. |
|
45 | * @param pRequest The request being performed. |
|
46 | * @return The requests result. |
|
47 | * @throws XmlRpcException Performing the request failed. |
|
48 | */ |
|
49 | public Object execute(XmlRpcRequest pRequest) |
|
50 | throws XmlRpcException { |
|
51 | try { |
|
52 | 432 | XmlRpcClient client = (XmlRpcClient) getController(); |
53 | 747 | return client.getTransportFactory().getTransport().sendRequest(pRequest); |
54 | 117 | } finally { |
55 | 432 | factory.releaseWorker(this); |
56 | 117 | } |
57 | } |
|
58 | ||
59 | protected Thread newThread(Runnable pRunnable) { |
|
60 | 0 | Thread result = new Thread(pRunnable); |
61 | 0 | result.setDaemon(true); |
62 | 0 | return result; |
63 | } |
|
64 | ||
65 | /** Performs an synchronous request. |
|
66 | * @param pRequest The request being performed. |
|
67 | * @param pCallback The callback being invoked, when the request is finished. |
|
68 | * @throws XmlRpcException Performing the request failed. |
|
69 | */ |
|
70 | public void execute(final XmlRpcRequest pRequest, |
|
71 | final AsyncCallback pCallback) |
|
72 | throws XmlRpcException { |
|
73 | 0 | Runnable runnable = new Runnable(){ |
74 | public void run(){ |
|
75 | 0 | Object result = null; |
76 | 0 | Throwable th = null; |
77 | try { |
|
78 | 0 | XmlRpcClient client = (XmlRpcClient) getController(); |
79 | 0 | result = client.getTransportFactory().getTransport().sendRequest(pRequest); |
80 | 0 | } catch (Throwable t) { |
81 | 0 | th = t; |
82 | } |
|
83 | 0 | factory.releaseWorker(XmlRpcClientWorker.this); |
84 | 0 | if (th == null) { |
85 | 0 | pCallback.handleResult(pRequest, result); |
86 | } else { |
|
87 | 0 | pCallback.handleError(pRequest, th); |
88 | } |
|
89 | 0 | } |
90 | }; |
|
91 | 0 | newThread(runnable).start(); |
92 | 0 | } |
93 | } |