1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.ksb.messaging.serviceexporters;
17
18 import java.util.List;
19
20 import org.apache.cxf.Bus;
21 import org.apache.cxf.endpoint.Server;
22 import org.apache.cxf.endpoint.ServerRegistry;
23 import org.apache.log4j.Logger;
24 import org.kuali.rice.ksb.api.bus.ServiceDefinition;
25 import org.kuali.rice.ksb.messaging.bam.BAMServerProxy;
26 import org.kuali.rice.ksb.messaging.servlet.CXFServletControllerAdapter;
27
28
29
30
31
32
33
34 public abstract class AbstractWebServiceExporter implements ServiceExporter {
35
36 static final Logger LOG = Logger.getLogger(AbstractWebServiceExporter.class);
37
38 private final ServiceDefinition serviceDefinition;
39 private final Bus cxfBus;
40
41 protected abstract void publishService(ServiceDefinition serviceDefinition, Object serviceImpl, String address) throws Exception;
42
43 public AbstractWebServiceExporter(ServiceDefinition serviceDefinition, Bus cxfBus) {
44 this.serviceDefinition = serviceDefinition;
45 this.cxfBus = cxfBus;
46 }
47
48 @Override
49 public Object exportService(ServiceDefinition serviceDefinition) {
50 try {
51 String serviceAddress = getServiceAddress(serviceDefinition);
52
53
54 if (!(isServicePublished(serviceAddress))){
55 publishService(serviceDefinition, serviceDefinition.getService(), serviceAddress);
56 }
57
58
59 CXFServletControllerAdapter cxfController = new CXFServletControllerAdapter();
60
61 return BAMServerProxy.wrap(cxfController, serviceDefinition);
62 } catch (Exception e) {
63 throw new RuntimeException(e);
64 }
65
66 }
67
68
69
70
71 protected String getServiceAddress(ServiceDefinition serviceDefinition) {
72
73 String serviceAddress = serviceDefinition.getServicePath();
74 if (("/").equals(serviceAddress)){
75 serviceAddress = serviceAddress + serviceDefinition.getServiceName().getLocalPart();
76 } else {
77 serviceAddress = serviceAddress + "/" + serviceDefinition.getServiceName().getLocalPart();
78 }
79 return serviceAddress;
80 }
81
82
83
84
85
86
87 protected boolean isServicePublished(String serviceAddress) {
88
89 ServerRegistry serverRegistry = getCXFServerRegistry();
90 List<Server> servers = serverRegistry.getServers();
91
92 for (Server server:servers){
93 String endpointAddress = server.getEndpoint().getEndpointInfo().getAddress();
94 if (endpointAddress.contains(serviceAddress)){
95 LOG.info("Service already published on CXF, not republishing: " + serviceAddress);
96 return true;
97 }
98 }
99
100 return false;
101 }
102
103 protected ServiceDefinition getServiceDefinition() {
104 return this.serviceDefinition;
105 }
106
107 protected Bus getCXFBus() {
108 return this.cxfBus;
109 }
110
111 protected ServerRegistry getCXFServerRegistry() {
112 return getCXFBus().getExtension(ServerRegistry.class);
113 }
114
115 }