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 local Stateless Session Bean (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 LocalSlsbInvokerInterceptor 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 * @author Rod Johnson 046 * @author Colin Sampaleanu 047 * @since 09.05.2003 048 * @see AbstractSlsbInvokerInterceptor#setLookupHomeOnStartup 049 * @see AbstractSlsbInvokerInterceptor#setCacheHome 050 */ 051public class LocalStatelessSessionProxyFactoryBean extends LocalSlsbInvokerInterceptor 052 implements FactoryBean<Object>, BeanClassLoaderAware { 053 054 /** The business interface of the EJB we're proxying */ 055 private Class<?> businessInterface; 056 057 private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); 058 059 /** EJBLocalObject */ 060 private Object proxy; 061 062 063 /** 064 * Set the business interface of the EJB we're proxying. 065 * This will normally be a super-interface of the EJB local component interface. 066 * Using a business methods interface is a best practice when implementing EJBs. 067 * @param businessInterface set the business interface of the EJB 068 */ 069 public void setBusinessInterface(Class<?> businessInterface) { 070 this.businessInterface = businessInterface; 071 } 072 073 /** 074 * Return the business interface of the EJB we're proxying. 075 */ 076 public Class<?> getBusinessInterface() { 077 return this.businessInterface; 078 } 079 080 @Override 081 public void setBeanClassLoader(ClassLoader classLoader) { 082 this.beanClassLoader = classLoader; 083 } 084 085 @Override 086 public void afterPropertiesSet() throws NamingException { 087 super.afterPropertiesSet(); 088 if (this.businessInterface == null) { 089 throw new IllegalArgumentException("businessInterface is required"); 090 } 091 this.proxy = new ProxyFactory(this.businessInterface, this).getProxy(this.beanClassLoader); 092 } 093 094 095 @Override 096 public Object getObject() { 097 return this.proxy; 098 } 099 100 @Override 101 public Class<?> getObjectType() { 102 return this.businessInterface; 103 } 104 105 @Override 106 public boolean isSingleton() { 107 return true; 108 } 109 110}