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