1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.cxf.builder; |
8 | |
|
9 | |
import org.mule.api.lifecycle.CreateException; |
10 | |
import org.mule.module.cxf.CxfOutboundMessageProcessor; |
11 | |
import org.mule.module.cxf.i18n.CxfMessages; |
12 | |
import org.mule.module.cxf.support.CxfUtils; |
13 | |
|
14 | |
import java.io.IOException; |
15 | |
import java.lang.reflect.Constructor; |
16 | |
import java.lang.reflect.InvocationTargetException; |
17 | |
import java.lang.reflect.Method; |
18 | |
import java.net.URL; |
19 | |
|
20 | |
import javax.xml.namespace.QName; |
21 | |
import javax.xml.ws.BindingProvider; |
22 | |
import javax.xml.ws.Service; |
23 | |
import javax.xml.ws.WebEndpoint; |
24 | |
import javax.xml.ws.WebServiceClient; |
25 | |
|
26 | |
import org.apache.cxf.common.classloader.ClassLoaderUtils; |
27 | |
import org.apache.cxf.endpoint.Client; |
28 | |
import org.apache.cxf.frontend.ClientProxy; |
29 | |
import org.apache.cxf.jaxws.JaxWsClientFactoryBean; |
30 | |
import org.apache.cxf.resource.ResourceManager; |
31 | |
import org.apache.cxf.resource.URIResolver; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 0 | public class JaxWsClientMessageProcessorBuilder extends AbstractClientMessageProcessorBuilder |
49 | |
{ |
50 | |
|
51 | |
|
52 | |
|
53 | |
protected BindingProvider clientProxy; |
54 | |
|
55 | |
protected String clientClass; |
56 | |
|
57 | |
protected String port; |
58 | |
|
59 | |
@Override |
60 | |
protected Client createClient() throws CreateException, Exception |
61 | |
{ |
62 | 0 | if (clientClass != null && serviceClass != null) |
63 | |
{ |
64 | 0 | throw new CreateException(CxfMessages.onlyServiceOrClientClassIsValid(), this); |
65 | |
} |
66 | |
|
67 | 0 | if (clientClass != null) |
68 | |
{ |
69 | 0 | return createClientFromJaxWsProxy(); |
70 | |
} |
71 | |
else |
72 | |
{ |
73 | 0 | return createClientFromFactoryBean(); |
74 | |
} |
75 | |
} |
76 | |
|
77 | |
private Client createClientFromFactoryBean() |
78 | |
{ |
79 | 0 | JaxWsClientFactoryBean cpf = new JaxWsClientFactoryBean(); |
80 | 0 | cpf.setServiceClass(serviceClass); |
81 | 0 | if (databinding == null) |
82 | |
{ |
83 | 0 | cpf.setDataBinding(databinding); |
84 | |
} |
85 | 0 | cpf.setAddress(getAddress()); |
86 | 0 | cpf.setBus(getBus()); |
87 | 0 | cpf.setProperties(properties); |
88 | |
|
89 | |
|
90 | 0 | if(soapVersion != null) |
91 | |
{ |
92 | 0 | cpf.setBindingId(CxfUtils.getBindingIdForSoapVersion(soapVersion)); |
93 | |
} |
94 | |
|
95 | 0 | if (wsdlLocation != null) |
96 | |
{ |
97 | 0 | cpf.setWsdlURL(wsdlLocation); |
98 | |
} |
99 | |
|
100 | 0 | return cpf.create(); |
101 | |
} |
102 | |
|
103 | |
private Client createClientFromJaxWsProxy() |
104 | |
throws ClassNotFoundException, NoSuchMethodException, IOException, CreateException, |
105 | |
InstantiationException, IllegalAccessException, InvocationTargetException |
106 | |
{ |
107 | 0 | Class<?> clientCls = ClassLoaderUtils.loadClass(clientClass, getClass()); |
108 | |
|
109 | 0 | Service s = null; |
110 | 0 | if (wsdlLocation != null) |
111 | |
{ |
112 | 0 | Constructor<?> cons = clientCls.getConstructor(URL.class, QName.class); |
113 | 0 | ResourceManager rr = getBus().getExtension(ResourceManager.class); |
114 | 0 | URL url = rr.resolveResource(wsdlLocation, URL.class); |
115 | |
|
116 | 0 | if (url == null) |
117 | |
{ |
118 | 0 | URIResolver res = new URIResolver(wsdlLocation); |
119 | |
|
120 | 0 | if (!res.isResolved()) |
121 | |
{ |
122 | 0 | throw new CreateException(CxfMessages.wsdlNotFound(wsdlLocation), this); |
123 | |
} |
124 | 0 | url = res.getURL(); |
125 | |
} |
126 | |
|
127 | 0 | WebServiceClient clientAnn = clientCls.getAnnotation(WebServiceClient.class); |
128 | 0 | QName svcName = new QName(clientAnn.targetNamespace(), clientAnn.name()); |
129 | |
|
130 | 0 | s = (Service) cons.newInstance(url, svcName); |
131 | 0 | } |
132 | |
else |
133 | |
{ |
134 | 0 | s = (Service) clientCls.newInstance(); |
135 | |
} |
136 | |
|
137 | 0 | if (port == null) |
138 | |
{ |
139 | 0 | throw new CreateException(CxfMessages.mustSpecifyPort(), this); |
140 | |
} |
141 | |
|
142 | 0 | clientProxy = null; |
143 | 0 | if (port != null) |
144 | |
{ |
145 | 0 | for (Method m : clientCls.getMethods()) |
146 | |
{ |
147 | 0 | WebEndpoint we = m.getAnnotation(WebEndpoint.class); |
148 | |
|
149 | 0 | if (we != null && we.name().equals(port) && m.getParameterTypes().length == 0) |
150 | |
{ |
151 | 0 | clientProxy = (BindingProvider) m.invoke(s, new Object[0]); |
152 | 0 | break; |
153 | |
} |
154 | |
} |
155 | |
} |
156 | |
|
157 | 0 | if (clientProxy == null) |
158 | |
{ |
159 | 0 | throw new CreateException(CxfMessages.portNotFound(port), this); |
160 | |
} |
161 | |
|
162 | 0 | return ClientProxy.getClient(clientProxy); |
163 | |
} |
164 | |
|
165 | |
@Override |
166 | |
protected void configureMessageProcessor(CxfOutboundMessageProcessor processor) |
167 | |
{ |
168 | 0 | super.configureMessageProcessor(processor); |
169 | 0 | processor.setClientProxy(clientProxy); |
170 | 0 | } |
171 | |
|
172 | |
public String getClientClass() |
173 | |
{ |
174 | 0 | return clientClass; |
175 | |
} |
176 | |
|
177 | |
public void setClientClass(String clientClass) |
178 | |
{ |
179 | 0 | this.clientClass = clientClass; |
180 | 0 | } |
181 | |
|
182 | |
public String getPort() |
183 | |
{ |
184 | 0 | return port; |
185 | |
} |
186 | |
|
187 | |
public void setPort(String port) |
188 | |
{ |
189 | 0 | this.port = port; |
190 | 0 | } |
191 | |
} |