001/**
002 * Copyright 2005-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.ksb.messaging.serviceexporters;
017
018import org.apache.commons.lang.StringUtils;
019import org.apache.cxf.Bus;
020import org.apache.cxf.endpoint.ServerRegistry;
021import org.kuali.rice.core.api.config.property.Config;
022import org.kuali.rice.core.api.config.property.ConfigContext;
023import org.kuali.rice.ksb.api.bus.ServiceDefinition;
024
025import javax.xml.namespace.QName;
026import java.net.URI;
027import java.net.URL;
028import java.util.concurrent.ConcurrentHashMap;
029import java.util.concurrent.ConcurrentMap;
030
031public class ServiceExportManagerImpl implements ServiceExportManager {
032
033        private final ConcurrentMap<QName, ExportedServiceHolder> exportedServices;
034        private final ServiceNameFinder serviceNameFinder;
035        
036        private Bus cxfBus;
037
038        public ServiceExportManagerImpl() {
039                this.exportedServices = new ConcurrentHashMap<QName, ExportedServiceHolder>();
040                this.serviceNameFinder = new ServiceNameFinder();
041        }
042        
043        @Override
044        public QName getServiceName(String endpointUrl) {
045                return getServiceNameFinder().lookup(endpointUrl);
046        }
047        
048        protected ServiceNameFinder getServiceNameFinder() {
049                return serviceNameFinder;
050        }
051        
052        @Override
053        public Object getService(QName serviceName) {
054                ExportedServiceHolder holder = exportedServices.get(serviceName);
055                if (holder == null) {
056                        return null;
057                }
058                return holder.getExportedService();
059        }
060        
061        @Override
062        public void exportService(ServiceDefinition serviceDefinition) {
063                if (serviceDefinition == null) {
064                        throw new IllegalArgumentException("serviceDefinition was null");
065                }
066                ServiceExporter serviceExporter = ServiceExporterFactory.getServiceExporter(serviceDefinition, cxfBus);
067                Object exportedService = serviceExporter.exportService(serviceDefinition);
068                exportedServices.put(serviceDefinition.getServiceName(), new ExportedServiceHolder(exportedService, serviceDefinition));
069                getServiceNameFinder().register(serviceDefinition);
070        }
071
072        @Override
073        public void removeService(QName serviceName) {
074                ExportedServiceHolder exportedServiceHolder = exportedServices.remove(serviceName);
075                getServiceNameFinder().remove(exportedServiceHolder.getServiceDefinition().getEndpointUrl());
076        }
077                
078        protected ConcurrentMap<QName, ExportedServiceHolder> getExportedServices() {
079                return exportedServices;
080        }
081        
082        public void setCxfBus(Bus cxfBus) {
083                this.cxfBus = cxfBus;
084        }
085
086    /**
087     * @deprecated setting ServerRegistry here has no effect, the ServerRegistry extension on the CXF Bus is used instead
088     */
089    @Deprecated
090        public void setCxfServerRegistry(ServerRegistry cxfServerRegistry) {
091        // no-op, see deprecation information
092        }
093        
094        protected static class ExportedServiceHolder {
095                
096                private final Object exportedService;
097                private final ServiceDefinition serviceDefinition;
098                
099                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         * Looks up service QNameS based on URL StringS.  API is Map-like, but non-service specific portions of the
116         * URL are trimmed prior to accessing its internal Map.
117         * 
118         * @author Kuali Rice Team (rice.collab@kuali.org)
119         *
120         */
121        protected static class ServiceNameFinder {
122            
123                /**
124                 * A service path to service QName map
125                 */
126                private ConcurrentMap<String, QName> servicePathToQName = new ConcurrentHashMap<String, QName>();
127                
128
129                /**
130                 * This method trims the endpoint url base ({@link Config#getEndPointUrl()}) base off of the full service URL, e.g.
131                 * "http://kuali.edu/kr-dev/remoting/SomeService" -> "SomeService".  It makes an effort to do so even if the host
132                 * and ip don't match what is in {@link Config#getEndPointUrl()} by stripping host/port info.
133                 * 
134                 * If the service URL contains the configured subpath for RESTful service, additional trimming is done to
135                 * isolate the service name from the rest of the url.
136                 * 
137                 * @param url
138                 * @return the service specific suffix.  If fullServiceUrl doesn't contain the endpoint url base,
139                 * fullServiceUrl is returned unmodified.  
140                 */
141                private String trimServiceUrlBase(String url) {
142                        String trimmedUrl = StringUtils.removeStart(url, ConfigContext.getCurrentContextConfig().getEndPointUrl());
143                        
144                        if (trimmedUrl.length() == url.length()) { // it didn't contain the endpoint url base.
145                                // Perhaps the incoming url has a different host (or the ip) or a different port.
146                                // Trim off the host & port, then trim off the common base.
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                 * adds a mapping from the service specific portion of the service URL to the service name.
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                 * removes the mapping (if one exists) for the service specific portion of this url.
172                 */
173                public void remove(URL endpointUrl) {
174                        servicePathToQName.remove(trimServiceUrlBase(endpointUrl.toExternalForm()));
175                }
176                
177                /**
178                 * gets the QName for the service
179                 * 
180                 * @param serviceUrl
181                 * @return
182                 */
183                public QName lookup(String serviceUrl) {
184                        String serviceUrlBase = trimServiceUrlBase(serviceUrl);
185
186                        // First, make sure we don't have any query params
187                        if (serviceUrlBase.length() > 0 && serviceUrlBase.lastIndexOf('?') != -1) {
188                                serviceUrlBase = serviceUrlBase.substring(0, serviceUrlBase.lastIndexOf('?'));
189                        }
190
191                        QName qname = null;
192                        // Now, iterate backwards through the url, stripping off pieces until you match -- this should work for rest too
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}