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