01: /*
02: * $Id: CxfMessageRequester.java 10961 2008-02-22 19:01:02Z dfeist $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
10:
11: package org.mule.transport.cxf;
12:
13: import org.mule.DefaultMuleMessage;
14: import org.mule.api.MuleMessage;
15: import org.mule.api.config.MuleProperties;
16: import org.mule.api.endpoint.InboundEndpoint;
17: import org.mule.transport.AbstractMessageRequester;
18:
19: import java.util.Iterator;
20: import java.util.Properties;
21:
22: import org.apache.cxf.endpoint.ClientImpl;
23:
24: /**
25: * <code>AxisMessageDispatcher</code> is used to make soap requests via the Axis
26: * soap client.
27: */
28: public class CxfMessageRequester extends AbstractMessageRequester {
29:
30: protected CxfConnector connector;
31: private ClientWrapper wrapper;
32:
33: public CxfMessageRequester(InboundEndpoint endpoint) {
34: super (endpoint);
35: this .connector = (CxfConnector) endpoint.getConnector();
36: }
37:
38: protected void doConnect() throws Exception {
39: wrapper = new ClientWrapper();
40: wrapper.setBus(connector.getCxfBus());
41: wrapper.setEndpoint(endpoint);
42: wrapper.initialize();
43: }
44:
45: protected void doDisconnect() throws Exception {
46: }
47:
48: protected void doDispose() {
49: // template method
50: }
51:
52: /**
53: * Make a specific request to the underlying transport
54: *
55: * @param timeout the maximum time the operation should block before returning.
56: * The call should return immediately if there is data available. If
57: * no data becomes available before the timeout elapses, null will be
58: * returned
59: * @return the result of the request wrapped in a MuleMessage object. Null will be
60: * returned if no data was avaialable
61: * @throws Exception if the call to the underlying protocal cuases an exception
62: */
63: protected MuleMessage doRequest(long timeout) throws Exception {
64: ((ClientImpl) wrapper.getClient())
65: .setSynchronousTimeout((int) timeout);
66:
67: String method = (String) endpoint
68: .getProperty(MuleProperties.MULE_METHOD_PROPERTY);
69:
70: if (method == null) {
71: method = (String) endpoint
72: .getProperty(CxfConstants.OPERATION);
73: }
74:
75: Properties params = endpoint.getEndpointURI().getUserParams();
76: Object args[] = new Object[params.size()];
77: int i = 0;
78: for (Iterator<Object> iterator = params.values().iterator(); iterator
79: .hasNext(); i++) {
80: args[i] = iterator.next().toString();
81: }
82:
83: Object[] response = wrapper.getClient().invoke(method, args);
84:
85: if (response != null && response.length == 1) {
86: return new DefaultMuleMessage(response[0]);
87: } else {
88: return new DefaultMuleMessage(response);
89: }
90: }
91:
92: }
|