001/*
002 * Copyright 2002-2017 the original author or authors.
003 *
004 * Licensed under the Apache 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 *      https://www.apache.org/licenses/LICENSE-2.0
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 */
016
017package org.springframework.jms.remoting;
018
019import org.springframework.aop.framework.ProxyFactory;
020import org.springframework.beans.factory.BeanClassLoaderAware;
021import org.springframework.beans.factory.FactoryBean;
022import org.springframework.lang.Nullable;
023import org.springframework.util.Assert;
024import org.springframework.util.ClassUtils;
025
026/**
027 * FactoryBean for JMS invoker proxies. Exposes the proxied service for use
028 * as a bean reference, using the specified service interface.
029 *
030 * <p>Serializes remote invocation objects and deserializes remote invocation
031 * result objects. Uses Java serialization just like RMI, but with the JMS
032 * provider as communication infrastructure.
033 *
034 * <p>To be configured with a {@link javax.jms.QueueConnectionFactory} and a
035 * target queue (either as {@link javax.jms.Queue} reference or as queue name).
036 *
037 * @author Juergen Hoeller
038 * @since 2.0
039 * @see #setConnectionFactory
040 * @see #setQueueName
041 * @see #setServiceInterface
042 * @see org.springframework.jms.remoting.JmsInvokerClientInterceptor
043 * @see org.springframework.jms.remoting.JmsInvokerServiceExporter
044 */
045public class JmsInvokerProxyFactoryBean extends JmsInvokerClientInterceptor
046                implements FactoryBean<Object>, BeanClassLoaderAware {
047
048        @Nullable
049        private Class<?> serviceInterface;
050
051        @Nullable
052        private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();
053
054        @Nullable
055        private Object serviceProxy;
056
057
058        /**
059         * Set the interface that the proxy must implement.
060         * @param serviceInterface the interface that the proxy must implement
061         * @throws IllegalArgumentException if the supplied {@code serviceInterface}
062         * is not an interface type
063         */
064        public void setServiceInterface(Class<?> serviceInterface) {
065                Assert.notNull(serviceInterface, "'serviceInterface' must not be null");
066                Assert.isTrue(serviceInterface.isInterface(), "'serviceInterface' must be an interface");
067                this.serviceInterface = serviceInterface;
068        }
069
070        @Override
071        public void setBeanClassLoader(ClassLoader classLoader) {
072                this.beanClassLoader = classLoader;
073        }
074
075        @Override
076        public void afterPropertiesSet() {
077                super.afterPropertiesSet();
078                Assert.notNull(this.serviceInterface, "Property 'serviceInterface' is required");
079                this.serviceProxy = new ProxyFactory(this.serviceInterface, this).getProxy(this.beanClassLoader);
080        }
081
082
083        @Override
084        @Nullable
085        public Object getObject() {
086                return this.serviceProxy;
087        }
088
089        @Override
090        public Class<?> getObjectType() {
091                return this.serviceInterface;
092        }
093
094        @Override
095        public boolean isSingleton() {
096                return true;
097        }
098
099}