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