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