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