View Javadoc

1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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       * @deprecated setting ServerRegistry here has no effect, the ServerRegistry extension on the CXF Bus is used instead
88       */
89      @Deprecated
90  	public void setCxfServerRegistry(ServerRegistry cxfServerRegistry) {
91          // no-op, see deprecation information
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 	 * 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 }