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 org.apache.commons.lang.StringUtils;
19 import org.apache.cxf.Bus;
20 import org.apache.cxf.endpoint.ServerRegistry;
21 import org.kuali.rice.core.api.config.property.Config;
22 import org.kuali.rice.core.api.config.property.ConfigContext;
23 import org.kuali.rice.ksb.api.bus.ServiceDefinition;
24
25 import javax.xml.namespace.QName;
26 import java.net.URI;
27 import java.net.URL;
28 import java.util.concurrent.ConcurrentHashMap;
29 import java.util.concurrent.ConcurrentMap;
30
31 public class ServiceExportManagerImpl implements ServiceExportManager {
32
33 private final ConcurrentMap<QName, ExportedServiceHolder> exportedServices;
34 private final ServiceNameFinder serviceNameFinder;
35
36 private Bus cxfBus;
37
38 public ServiceExportManagerImpl() {
39 this.exportedServices = new ConcurrentHashMap<QName, ExportedServiceHolder>();
40 this.serviceNameFinder = new ServiceNameFinder();
41 }
42
43 @Override
44 public QName getServiceName(String endpointUrl) {
45 return getServiceNameFinder().lookup(endpointUrl);
46 }
47
48 protected ServiceNameFinder getServiceNameFinder() {
49 return serviceNameFinder;
50 }
51
52 @Override
53 public Object getService(QName serviceName) {
54 ExportedServiceHolder holder = exportedServices.get(serviceName);
55 if (holder == null) {
56 return null;
57 }
58 return holder.getExportedService();
59 }
60
61 @Override
62 public void exportService(ServiceDefinition serviceDefinition) {
63 if (serviceDefinition == null) {
64 throw new IllegalArgumentException("serviceDefinition was null");
65 }
66 ServiceExporter serviceExporter = ServiceExporterFactory.getServiceExporter(serviceDefinition, cxfBus);
67 Object exportedService = serviceExporter.exportService(serviceDefinition);
68 exportedServices.put(serviceDefinition.getServiceName(), new ExportedServiceHolder(exportedService, serviceDefinition));
69 getServiceNameFinder().register(serviceDefinition);
70 }
71
72 @Override
73 public void removeService(QName serviceName) {
74 ExportedServiceHolder exportedServiceHolder = exportedServices.remove(serviceName);
75 getServiceNameFinder().remove(exportedServiceHolder.getServiceDefinition().getEndpointUrl());
76 }
77
78 protected ConcurrentMap<QName, ExportedServiceHolder> getExportedServices() {
79 return exportedServices;
80 }
81
82 public void setCxfBus(Bus cxfBus) {
83 this.cxfBus = cxfBus;
84 }
85
86
87
88
89 @Deprecated
90 public void setCxfServerRegistry(ServerRegistry cxfServerRegistry) {
91
92 }
93
94 protected static class ExportedServiceHolder {
95
96 private final Object exportedService;
97 private final ServiceDefinition serviceDefinition;
98
99 ExportedServiceHolder(Object exportedService, ServiceDefinition serviceDefinition) {
100 this.exportedService = exportedService;
101 this.serviceDefinition = serviceDefinition;
102 }
103
104 public Object getExportedService() {
105 return exportedService;
106 }
107
108 public ServiceDefinition getServiceDefinition() {
109 return serviceDefinition;
110 }
111
112 }
113
114
115
116
117
118
119
120
121 protected static class ServiceNameFinder {
122
123
124
125
126 private ConcurrentMap<String, QName> servicePathToQName = new ConcurrentHashMap<String, QName>();
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141 private String trimServiceUrlBase(String url) {
142 String trimmedUrl = StringUtils.removeStart(url, ConfigContext.getCurrentContextConfig().getEndPointUrl());
143
144 if (trimmedUrl.length() == url.length()) {
145
146
147 URI serviceUri = URI.create(url);
148 URI endpointUrlBase = URI.create(ConfigContext.getCurrentContextConfig().getEndPointUrl());
149
150 String reqPath = serviceUri.getPath();
151 String basePath = endpointUrlBase.getPath();
152
153 trimmedUrl = StringUtils.removeStart(reqPath, basePath);
154 }
155
156 return trimmedUrl;
157 }
158
159
160
161
162 public void register(ServiceDefinition serviceDefinition) {
163 String serviceUrlBase = trimServiceUrlBase(serviceDefinition.getEndpointUrl().toExternalForm());
164 if (serviceUrlBase.endsWith("/")) {
165 serviceUrlBase = StringUtils.chop(serviceUrlBase);
166 }
167 servicePathToQName.put(serviceUrlBase, serviceDefinition.getServiceName());
168 }
169
170
171
172
173 public void remove(URL endpointUrl) {
174 servicePathToQName.remove(trimServiceUrlBase(endpointUrl.toExternalForm()));
175 }
176
177
178
179
180
181
182
183 public QName lookup(String serviceUrl) {
184 String serviceUrlBase = trimServiceUrlBase(serviceUrl);
185
186
187 if (serviceUrlBase.length() > 0 && serviceUrlBase.lastIndexOf('?') != -1) {
188 serviceUrlBase = serviceUrlBase.substring(0, serviceUrlBase.lastIndexOf('?'));
189 }
190
191 QName qname = null;
192
193 while (qname == null) {
194 qname = servicePathToQName.get(serviceUrlBase);
195
196 int lastSeparatorIndex = serviceUrlBase.lastIndexOf('/');
197 if (lastSeparatorIndex == -1)
198 break;
199 serviceUrlBase = serviceUrlBase.substring(0, lastSeparatorIndex);
200 }
201
202 return qname;
203 }
204
205 }
206
207 }