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 remote 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 SimpleRemoteSlsbInvokerInterceptor 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 * <p>This proxy factory is typically used with an RMI business interface, which serves
047 * as super-interface of the EJB component interface. Alternatively, this factory
048 * can also proxy a remote SLSB with a matching non-RMI business interface, i.e. an
049 * interface that mirrors the EJB business methods but does not declare RemoteExceptions.
050 * In the latter case, RemoteExceptions thrown by the EJB stub will automatically get
051 * converted to Spring's unchecked RemoteAccessException.
052 *
053 * @author Rod Johnson
054 * @author Colin Sampaleanu
055 * @author Juergen Hoeller
056 * @since 09.05.2003
057 * @see org.springframework.remoting.RemoteAccessException
058 * @see AbstractSlsbInvokerInterceptor#setLookupHomeOnStartup
059 * @see AbstractSlsbInvokerInterceptor#setCacheHome
060 * @see AbstractRemoteSlsbInvokerInterceptor#setRefreshHomeOnConnectFailure
061 */
062public class SimpleRemoteStatelessSessionProxyFactoryBean extends SimpleRemoteSlsbInvokerInterceptor
063        implements FactoryBean<Object>, BeanClassLoaderAware {
064
065        /** The business interface of the EJB we're proxying. */
066        @Nullable
067        private Class<?> businessInterface;
068
069        @Nullable
070        private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();
071
072        /** EJBObject. */
073        @Nullable
074        private Object proxy;
075
076
077        /**
078         * Set the business interface of the EJB we're proxying.
079         * This will normally be a super-interface of the EJB remote component interface.
080         * Using a business methods interface is a best practice when implementing EJBs.
081         * <p>You can also specify a matching non-RMI business interface, i.e. an interface
082         * that mirrors the EJB business methods but does not declare RemoteExceptions.
083         * In this case, RemoteExceptions thrown by the EJB stub will automatically get
084         * converted to Spring's generic RemoteAccessException.
085         * @param businessInterface the business interface of the EJB
086         */
087        public void setBusinessInterface(@Nullable Class<?> businessInterface) {
088                this.businessInterface = businessInterface;
089        }
090
091        /**
092         * Return the business interface of the EJB we're proxying.
093         */
094        @Nullable
095        public Class<?> getBusinessInterface() {
096                return this.businessInterface;
097        }
098
099        @Override
100        public void setBeanClassLoader(ClassLoader classLoader) {
101                this.beanClassLoader = classLoader;
102        }
103
104        @Override
105        public void afterPropertiesSet() throws NamingException {
106                super.afterPropertiesSet();
107                if (this.businessInterface == null) {
108                        throw new IllegalArgumentException("businessInterface is required");
109                }
110                this.proxy = new ProxyFactory(this.businessInterface, this).getProxy(this.beanClassLoader);
111        }
112
113
114        @Override
115        @Nullable
116        public Object getObject() {
117                return this.proxy;
118        }
119
120        @Override
121        public Class<?> getObjectType() {
122                return this.businessInterface;
123        }
124
125        @Override
126        public boolean isSingleton() {
127                return true;
128        }
129
130}