001 package org.kuali.rice.ksb.messaging.serviceexporters;
002
003 import java.net.URI;
004 import java.net.URL;
005 import java.util.concurrent.ConcurrentHashMap;
006 import java.util.concurrent.ConcurrentMap;
007
008 import javax.xml.namespace.QName;
009
010 import org.apache.commons.lang.StringUtils;
011 import org.apache.cxf.Bus;
012 import org.apache.cxf.endpoint.ServerRegistry;
013 import org.kuali.rice.core.api.config.property.Config;
014 import org.kuali.rice.core.api.config.property.ConfigContext;
015 import org.kuali.rice.ksb.api.bus.ServiceDefinition;
016
017 public class ServiceExportManagerImpl implements ServiceExportManager {
018
019 private final ConcurrentMap<QName, ExportedServiceHolder> exportedServices;
020 private final ServiceNameFinder serviceNameFinder;
021
022 private Bus cxfBus;
023 private ServerRegistry cxfServerRegistry;
024
025 public ServiceExportManagerImpl() {
026 this.exportedServices = new ConcurrentHashMap<QName, ExportedServiceHolder>();
027 this.serviceNameFinder = new ServiceNameFinder();
028 }
029
030 @Override
031 public QName getServiceName(String endpointUrl) {
032 return getServiceNameFinder().lookup(endpointUrl);
033 }
034
035 protected ServiceNameFinder getServiceNameFinder() {
036 return serviceNameFinder;
037 }
038
039 @Override
040 public Object getService(QName serviceName) {
041 ExportedServiceHolder holder = exportedServices.get(serviceName);
042 if (holder == null) {
043 return null;
044 }
045 return holder.getExportedService();
046 }
047
048 @Override
049 public void exportService(ServiceDefinition serviceDefinition) {
050 if (serviceDefinition == null) {
051 throw new IllegalArgumentException("serviceDefinition was null");
052 }
053 ServiceExporter serviceExporter = ServiceExporterFactory.getServiceExporter(serviceDefinition, cxfBus, cxfServerRegistry);
054 Object exportedService = serviceExporter.exportService(serviceDefinition);
055 exportedServices.put(serviceDefinition.getServiceName(), new ExportedServiceHolder(exportedService, serviceDefinition));
056 getServiceNameFinder().register(serviceDefinition);
057 }
058
059 @Override
060 public void removeService(QName serviceName) {
061 ExportedServiceHolder exportedServiceHolder = exportedServices.remove(serviceName);
062 getServiceNameFinder().remove(exportedServiceHolder.getServiceDefinition().getEndpointUrl());
063 }
064
065 protected ConcurrentMap<QName, ExportedServiceHolder> getExportedServices() {
066 return exportedServices;
067 }
068
069 public void setCxfBus(Bus cxfBus) {
070 this.cxfBus = cxfBus;
071 }
072
073 public void setCxfServerRegistry(ServerRegistry cxfServerRegistry) {
074 this.cxfServerRegistry = cxfServerRegistry;
075 }
076
077 protected static class ExportedServiceHolder {
078
079 private final Object exportedService;
080 private final ServiceDefinition serviceDefinition;
081
082 ExportedServiceHolder(Object exportedService, ServiceDefinition serviceDefinition) {
083 this.exportedService = exportedService;
084 this.serviceDefinition = serviceDefinition;
085 }
086
087 public Object getExportedService() {
088 return exportedService;
089 }
090
091 public ServiceDefinition getServiceDefinition() {
092 return serviceDefinition;
093 }
094
095 }
096
097 /**
098 * Looks up service QNameS based on URL StringS. API is Map-like, but non-service specific portions of the
099 * URL are trimmed prior to accessing its internal Map.
100 *
101 * @author Kuali Rice Team (rice.collab@kuali.org)
102 *
103 */
104 protected static class ServiceNameFinder {
105
106 /**
107 * A service path to service QName map
108 */
109 private ConcurrentMap<String, QName> servicePathToQName = new ConcurrentHashMap<String, QName>();
110
111
112 /**
113 * This method trims the endpoint url base ({@link Config#getEndPointUrl()}) base off of the full service URL, e.g.
114 * "http://kuali.edu/kr-dev/remoting/SomeService" -> "SomeService". It makes an effort to do so even if the host
115 * and ip don't match what is in {@link Config#getEndPointUrl()} by stripping host/port info.
116 *
117 * If the service URL contains the configured subpath for RESTful service, additional trimming is done to
118 * isolate the service name from the rest of the url.
119 *
120 * @param url
121 * @return the service specific suffix. If fullServiceUrl doesn't contain the endpoint url base,
122 * fullServiceUrl is returned unmodified.
123 */
124 private String trimServiceUrlBase(String url) {
125 String trimmedUrl = StringUtils.removeStart(url, ConfigContext.getCurrentContextConfig().getEndPointUrl());
126
127 if (trimmedUrl.length() == url.length()) { // it didn't contain the endpoint url base.
128 // Perhaps the incoming url has a different host (or the ip) or a different port.
129 // Trim off the host & port, then trim off the common base.
130 URI serviceUri = URI.create(url);
131 URI endpointUrlBase = URI.create(ConfigContext.getCurrentContextConfig().getEndPointUrl());
132
133 String reqPath = serviceUri.getPath();
134 String basePath = endpointUrlBase.getPath();
135
136 trimmedUrl = StringUtils.removeStart(reqPath, basePath);
137 }
138
139 return trimmedUrl;
140 }
141
142 /**
143 * adds a mapping from the service specific portion of the service URL to the service name.
144 *
145 * @param serviceUrl
146 * @param serviceName
147 */
148 public void register(ServiceDefinition serviceDefinition) {
149 String serviceUrlBase = trimServiceUrlBase(serviceDefinition.getEndpointUrl().toExternalForm());
150 if (serviceUrlBase.endsWith("/")) {
151 serviceUrlBase = StringUtils.chop(serviceUrlBase);
152 }
153 servicePathToQName.put(serviceUrlBase, serviceDefinition.getServiceName());
154 }
155
156 /**
157 * removes the mapping (if one exists) for the service specific portion of this url.
158 *
159 * @param serviceUrl
160 */
161 public void remove(URL endpointUrl) {
162 servicePathToQName.remove(trimServiceUrlBase(endpointUrl.toExternalForm()));
163 }
164
165 /**
166 * gets the QName for the service
167 *
168 * @param serviceUrl
169 * @return
170 */
171 public QName lookup(String serviceUrl) {
172 String serviceUrlBase = trimServiceUrlBase(serviceUrl);
173
174 // First, make sure we don't have any query params
175 if (serviceUrlBase.length() > 0 && serviceUrlBase.lastIndexOf('?') != -1) {
176 serviceUrlBase = serviceUrlBase.substring(0, serviceUrlBase.lastIndexOf('?'));
177 }
178
179 QName qname = null;
180 // Now, iterate backwards through the url, stripping off pieces until you match -- this should work for rest too
181 while (qname == null) {
182 qname = servicePathToQName.get(serviceUrlBase);
183
184 int lastSeparatorIndex = serviceUrlBase.lastIndexOf('/');
185 if (lastSeparatorIndex == -1)
186 break;
187 serviceUrlBase = serviceUrlBase.substring(0, lastSeparatorIndex);
188 }
189
190 return qname;
191 }
192
193 }
194
195 }