001/*
002 * Copyright 2002-2018 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.ejb.access;
018
019import javax.naming.NamingException;
020
021import org.springframework.aop.framework.ProxyFactory;
022import org.springframework.beans.factory.BeanClassLoaderAware;
023import org.springframework.beans.factory.FactoryBean;
024import org.springframework.lang.Nullable;
025import org.springframework.util.ClassUtils;
026
027/**
028 * Convenient {@link FactoryBean} for local Stateless Session Bean (SLSB) proxies.
029 * Designed for EJB 2.x, but works for EJB 3 Session Beans as well.
030 *
031 * <p>See {@link org.springframework.jndi.JndiObjectLocator} for info on
032 * how to specify the JNDI location of the target EJB.
033 *
034 * <p>If you want control over interceptor chaining, use an AOP ProxyFactoryBean
035 * with LocalSlsbInvokerInterceptor rather than rely on this class.
036 *
037 * <p>In a bean container, this class is normally best used as a singleton. However,
038 * if that bean container pre-instantiates singletons (as do the XML ApplicationContext
039 * variants) you may have a problem if the bean container is loaded before the EJB
040 * container loads the target EJB. That is because by default the JNDI lookup will be
041 * performed in the init method of this class and cached, but the EJB will not have been
042 * bound at the target location yet. The best solution is to set the "lookupHomeOnStartup"
043 * property to "false", in which case the home will be fetched on first access to the EJB.
044 * (This flag is only true by default for backwards compatibility reasons).
045 *
046 * @author Rod Johnson
047 * @author Colin Sampaleanu
048 * @since 09.05.2003
049 * @see AbstractSlsbInvokerInterceptor#setLookupHomeOnStartup
050 * @see AbstractSlsbInvokerInterceptor#setCacheHome
051 */
052public class LocalStatelessSessionProxyFactoryBean extends LocalSlsbInvokerInterceptor
053                implements FactoryBean<Object>, BeanClassLoaderAware {
054
055        /** The business interface of the EJB we're proxying. */
056        @Nullable
057        private Class<?> businessInterface;
058
059        @Nullable
060        private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();
061
062        /** EJBLocalObject. */
063        @Nullable
064        private Object proxy;
065
066
067        /**
068         * Set the business interface of the EJB we're proxying.
069         * This will normally be a super-interface of the EJB local component interface.
070         * Using a business methods interface is a best practice when implementing EJBs.
071         * @param businessInterface set the business interface of the EJB
072         */
073        public void setBusinessInterface(@Nullable Class<?> businessInterface) {
074                this.businessInterface = businessInterface;
075        }
076
077        /**
078         * Return the business interface of the EJB we're proxying.
079         */
080        @Nullable
081        public Class<?> getBusinessInterface() {
082                return this.businessInterface;
083        }
084
085        @Override
086        public void setBeanClassLoader(ClassLoader classLoader) {
087                this.beanClassLoader = classLoader;
088        }
089
090        @Override
091        public void afterPropertiesSet() throws NamingException {
092                super.afterPropertiesSet();
093                if (this.businessInterface == null) {
094                        throw new IllegalArgumentException("businessInterface is required");
095                }
096                this.proxy = new ProxyFactory(this.businessInterface, this).getProxy(this.beanClassLoader);
097        }
098
099
100        @Override
101        @Nullable
102        public Object getObject() {
103                return this.proxy;
104        }
105
106        @Override
107        public Class<?> getObjectType() {
108                return this.businessInterface;
109        }
110
111        @Override
112        public boolean isSingleton() {
113                return true;
114        }
115
116}