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