1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.test.remote;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.apache.cxf.endpoint.Client;
21 import org.apache.cxf.frontend.ClientProxy;
22 import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
23 import org.kuali.rice.ksb.impl.cxf.interceptors.ImmutableCollectionsInInterceptor;
24
25 import javax.jws.WebService;
26 import javax.xml.ws.Endpoint;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 public class RemoteTestHarness {
42
43 private static final Log LOG = LogFactory.getLog(RemoteTestHarness.class);
44
45 private static String ENDPOINT_ROOT = "http://localhost"; //Default URL
46 private static String ENDPOINT_PATH = "/service";
47
48 private Endpoint endpoint;
49
50 @SuppressWarnings("unchecked")
51
52
53
54
55 public <T> T publishEndpointAndReturnProxy(Class<T> jaxWsAnnotatedInterface, T serviceImplementation) {
56 if (jaxWsAnnotatedInterface.isInterface() &&
57 jaxWsAnnotatedInterface.getAnnotation(WebService.class) != null &&
58 jaxWsAnnotatedInterface.isInstance(serviceImplementation)) {
59
60 String endpointUrl = getAvailableEndpointUrl();
61 LOG.info("Publishing service to: " + endpointUrl);
62 endpoint = Endpoint.publish(endpointUrl, serviceImplementation);
63
64 JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
65 factory.setServiceClass(jaxWsAnnotatedInterface);
66 factory.setAddress(endpointUrl);
67
68 T serviceProxy = (T) factory.create();
69
70
71 Client cxfClient = ClientProxy.getClient(serviceProxy);
72 cxfClient.getInInterceptors().add(new ImmutableCollectionsInInterceptor());
73
74 return serviceProxy;
75 } else {
76 throw new IllegalArgumentException("Passed in interface class type must be annotated with @WebService " +
77 "and object reference must be an implementing class of that interface.");
78
79 }
80 }
81
82
83
84
85
86 public void stopEndpoint() {
87 if (endpoint != null) {
88 endpoint.stop();
89 }
90 }
91
92 private String getAvailableEndpointUrl() {
93 String port = Integer.toString(AvailablePortFinder.getNextAvailable());
94 return ENDPOINT_ROOT + ":" + port + ENDPOINT_PATH;
95 }
96 }