1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.ejb.access;
  17. import org.springframework.aop.framework.ProxyFactory;
  18. import org.springframework.beans.factory.FactoryBean;
  19. /**
  20. * <p>Convenient factory for local Stateless Session Bean (SLSB) proxies.
  21. * If you want control over interceptor chaining, use an AOP ProxyFactoryBean
  22. * with LocalSlsbInvokerInterceptor rather than rely on this class.
  23. *
  24. * <p>See {@link org.springframework.jndi.AbstractJndiLocator} for info on
  25. * how to specify the JNDI location of the target EJB.
  26. *
  27. * <p>In a bean container, this class is normally best used as a singleton. However,
  28. * if that bean container pre-instantiates singletons (as do the XML ApplicationContext
  29. * variants) you may have a problem if the bean container is loaded before the EJB
  30. * container loads the target EJB. That is because the JNDI lookup will be performed in
  31. * the init method of this class and cached, but the EJB will not have been bound at the
  32. * target location yet. The solution is to not pre-instantiate this factory object, but
  33. * allow it to be created on first use. In the XML containers, this is controlled via
  34. * the "lazy-init" attribute.
  35. *
  36. * @author Rod Johnson
  37. * @author Colin Sampaleanu
  38. * @since 09-May-2003
  39. * @version $Id: LocalStatelessSessionProxyFactoryBean.java,v 1.10 2004/03/18 02:46:14 trisberg Exp $
  40. */
  41. public class LocalStatelessSessionProxyFactoryBean extends LocalSlsbInvokerInterceptor
  42. implements FactoryBean {
  43. /*
  44. * Instead of a separate subclass for each type of SlsbInvoker, we could have added
  45. * this functionality to AbstractSlsbInvokerInterceptor. However, the avoiding of
  46. * code duplication would be outweighed by the confusion this would produce over the
  47. * purpose of AbstractSlsbInvokerInterceptor.
  48. */
  49. /** EJBLocalObject */
  50. private Object proxy;
  51. /**
  52. * The business interface of the EJB we're proxying.
  53. */
  54. private Class businessInterface;
  55. /**
  56. * Set the business interface of the EJB we're proxying.
  57. * This will normally be a super-interface of the EJB local component interface.
  58. * Using a business methods interface is a best practice when implementing EJBs.
  59. * @param businessInterface set the business interface of the EJB
  60. */
  61. public void setBusinessInterface(Class businessInterface) {
  62. this.businessInterface = businessInterface;
  63. }
  64. /**
  65. * Return the business interface of the EJB we're proxying.
  66. */
  67. public Class getBusinessInterface() {
  68. return businessInterface;
  69. }
  70. public void afterLocated() {
  71. if (this.businessInterface == null) {
  72. throw new IllegalArgumentException("businessInterface is required");
  73. }
  74. this.proxy = ProxyFactory.getProxy(this.businessInterface, this);
  75. }
  76. public Object getObject() {
  77. return this.proxy;
  78. }
  79. public Class getObjectType() {
  80. return (this.proxy != null) ? this.proxy.getClass() : this.businessInterface;
  81. }
  82. public boolean isSingleton() {
  83. return true;
  84. }
  85. }