001/**
002 * Copyright 2005-2017 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;
017
018import org.kuali.rice.core.api.util.ClassLoaderUtils;
019import org.kuali.rice.core.api.util.ContextClassLoaderProxy;
020import org.kuali.rice.ksb.api.bus.ServiceDefinition;
021import org.kuali.rice.ksb.messaging.bam.BAMServerProxy;
022import org.springframework.aop.framework.ProxyFactory;
023import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
024import org.springframework.remoting.support.RemoteInvocationTraceInterceptor;
025
026import java.util.Arrays;
027import java.util.List;
028
029
030public class KSBHttpInvokerServiceExporter extends HttpInvokerServiceExporter {
031        
032        private List<Class<?>> serviceInterfaces;
033        private ServiceDefinition serviceDefinition;
034        private boolean registerTraceInterceptor = true;
035        
036        public ServiceDefinition getServiceDefinition() {
037                return this.serviceDefinition;
038        }
039
040        public void setServiceDefinition(ServiceDefinition serviceDefinition) {
041                this.serviceDefinition = serviceDefinition;
042        }
043        
044        /**
045         * @see org.springframework.remoting.support.RemoteExporter#setRegisterTraceInterceptor(boolean)
046         */
047        @Override
048        public void setRegisterTraceInterceptor(boolean registerTraceInterceptor) {
049            // HttpInvokerServiceExporter.registerTraceInterceptor is no longer exposed, capture its value if set
050            this.registerTraceInterceptor = registerTraceInterceptor;
051            super.setRegisterTraceInterceptor(registerTraceInterceptor);
052        }
053
054        protected Object getProxyForService() {
055                checkService();
056                checkServiceInterface();
057                ProxyFactory proxyFactory = new ProxyFactory();
058                for (Class<?> serviceInterface : getServiceInterfaces()) {
059                        proxyFactory.addInterface(serviceInterface);
060                }
061                if (registerTraceInterceptor == true) {
062                        proxyFactory.addAdvice(new RemoteInvocationTraceInterceptor(getExporterName()));
063                }
064                ClassLoader classLoader = serviceDefinition.getServiceClassLoader();
065                if (classLoader == null) {
066                    classLoader = ClassLoaderUtils.getDefaultClassLoader();
067                }
068                Object service = ContextClassLoaderProxy.wrap(getService(), classLoader);
069                service = BAMServerProxy.wrap(service, getServiceDefinition());
070                proxyFactory.setTarget(service);
071                return proxyFactory.getProxy(classLoader);
072        }
073
074        @Override
075        protected void checkServiceInterface() throws IllegalArgumentException {
076                if (this.serviceInterfaces == null) {
077                    this.serviceInterfaces = Arrays.asList(ContextClassLoaderProxy.getInterfacesToProxy(getService()));
078                }
079                if (getServiceInterfaces().isEmpty()) {
080                        throw new IllegalArgumentException("At least one service interface should be defined.");
081                }
082        }
083        
084        public List<Class<?>> getServiceInterfaces() {
085                return this.serviceInterfaces;
086        }
087
088        public void setServiceInterfaces(List<Class<?>> serviceInterfaces) {
089                this.serviceInterfaces = serviceInterfaces;
090        }
091        
092        public Object getService() {
093                return super.getService();
094        }
095        
096}