View Javadoc

1   /**
2    * Copyright 2005-2012 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;
17  
18  import org.kuali.rice.core.api.util.ClassLoaderUtils;
19  import org.kuali.rice.core.api.util.ContextClassLoaderProxy;
20  import org.kuali.rice.ksb.api.bus.ServiceDefinition;
21  import org.kuali.rice.ksb.messaging.bam.BAMServerProxy;
22  import org.springframework.aop.framework.ProxyFactory;
23  import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
24  import org.springframework.remoting.support.RemoteInvocationTraceInterceptor;
25  
26  import java.util.Arrays;
27  import java.util.List;
28  
29  
30  public class KSBHttpInvokerServiceExporter extends HttpInvokerServiceExporter {
31  	
32  	private List<Class<?>> serviceInterfaces;
33  	private ServiceDefinition serviceDefinition;
34  	private boolean registerTraceInterceptor = true;
35  	
36  	public ServiceDefinition getServiceDefinition() {
37  		return this.serviceDefinition;
38  	}
39  
40  	public void setServiceDefinition(ServiceDefinition serviceDefinition) {
41  		this.serviceDefinition = serviceDefinition;
42  	}
43  	
44  	/**
45  	 * @see org.springframework.remoting.support.RemoteExporter#setRegisterTraceInterceptor(boolean)
46  	 */
47  	@Override
48  	public void setRegisterTraceInterceptor(boolean registerTraceInterceptor) {
49  	    // HttpInvokerServiceExporter.registerTraceInterceptor is no longer exposed, capture its value if set
50  	    this.registerTraceInterceptor = registerTraceInterceptor;
51  	    super.setRegisterTraceInterceptor(registerTraceInterceptor);
52  	}
53  
54  	protected Object getProxyForService() {
55  		checkService();
56  		checkServiceInterface();
57  		ProxyFactory proxyFactory = new ProxyFactory();
58  		for (Class<?> serviceInterface : getServiceInterfaces()) {
59  			proxyFactory.addInterface(serviceInterface);
60  		}
61  		if (registerTraceInterceptor == true) {
62  			proxyFactory.addAdvice(new RemoteInvocationTraceInterceptor(getExporterName()));
63  		}
64  		ClassLoader classLoader = serviceDefinition.getServiceClassLoader();
65  		if (classLoader == null) {
66  		    classLoader = ClassLoaderUtils.getDefaultClassLoader();
67  		}
68  		Object service = ContextClassLoaderProxy.wrap(getService(), classLoader);
69  		service = BAMServerProxy.wrap(service, getServiceDefinition());
70  		proxyFactory.setTarget(service);
71  		return proxyFactory.getProxy(classLoader);
72  	}
73  
74  	@Override
75  	protected void checkServiceInterface() throws IllegalArgumentException {
76  		if (this.serviceInterfaces == null) {
77  		    this.serviceInterfaces = Arrays.asList(ContextClassLoaderProxy.getInterfacesToProxy(getService()));
78  		}
79  		if (getServiceInterfaces().isEmpty()) {
80  			throw new IllegalArgumentException("At least one service interface should be defined.");
81  		}
82  	}
83  	
84  	public List<Class<?>> getServiceInterfaces() {
85  		return this.serviceInterfaces;
86  	}
87  
88  	public void setServiceInterfaces(List<Class<?>> serviceInterfaces) {
89  		this.serviceInterfaces = serviceInterfaces;
90  	}
91  	
92  	public Object getService() {
93  		return super.getService();
94  	}
95  	
96  }