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.support;
  17. import javax.ejb.CreateException;
  18. import javax.ejb.EJBException;
  19. import org.apache.commons.logging.Log;
  20. import org.apache.commons.logging.LogFactory;
  21. /**
  22. * Convenient superclass for stateless session beans (SLSBs), minimizing
  23. * the work involved in implementing an SLSB and preventing common errors.
  24. * <b>Note that SLSBs are the most useful kind of EJB.</b>
  25. *
  26. * <p>As the ejbActivate() and ejbPassivate() methods cannot be invoked
  27. * on SLSBs, these methods are implemented to throw an exception and should
  28. * not be overriden by subclasses. (Unfortunately the EJB specification
  29. * forbids enforcing this by making EJB lifecycle methods final.)
  30. *
  31. * <p>There should be no need to override the setSessionContext() or
  32. * ejbCreate() lifecycle methods.
  33. *
  34. * <p>Subclasses are left to implement the onEjbCreate() method to do
  35. * whatever initialization they wish to do after their BeanFactory has
  36. * already been loaded, and is available from the getBeanFactory() method.
  37. *
  38. * <p>This class provides the no-argument ejbCreate() method required
  39. * by the EJB specification, but not the SessionBean interface,
  40. * eliminating a common cause of EJB deployment failure.
  41. *
  42. * @author Rod Johnson
  43. * @version $Id: AbstractStatelessSessionBean.java,v 1.7 2004/03/18 02:46:14 trisberg Exp $
  44. */
  45. public abstract class AbstractStatelessSessionBean extends AbstractSessionBean {
  46. protected final Log logger = LogFactory.getLog(getClass());
  47. /**
  48. * This implementation loads the BeanFactory. A BeansException thrown by
  49. * loadBeanFactory will simply get propagated, as it is a runtime exception.
  50. * <p>Don't override it (although it can't be made final): code your own
  51. * initialization in onEjbCreate(), which is called when the BeanFactory
  52. * is available.
  53. * <p>Unfortunately we can't load the BeanFactory in setSessionContext(),
  54. * as resource manager access isn't permitted there - but the BeanFactory
  55. * may require it.
  56. */
  57. public void ejbCreate() throws CreateException {
  58. loadBeanFactory();
  59. onEjbCreate();
  60. }
  61. /**
  62. * Subclasses must implement this method to do any initialization
  63. * they would otherwise have done in an ejbCreate() method. In contrast
  64. * to ejbCreate, the BeanFactory will have been loaded here.
  65. * <p>The same restrictions apply to the work of this method as
  66. * to an ejbCreate() method.
  67. * @throws CreateException
  68. */
  69. protected abstract void onEjbCreate() throws CreateException;
  70. /**
  71. * @see javax.ejb.SessionBean#ejbActivate(). This method always throws an exception, as
  72. * it should not be invoked by the EJB container.
  73. */
  74. public void ejbActivate() throws EJBException {
  75. throw new IllegalStateException("ejbActivate must not be invoked on a stateless session bean");
  76. }
  77. /**
  78. * @see javax.ejb.SessionBean#ejbPassivate(). This method always throws an exception, as
  79. * it should not be invoked by the EJB container.
  80. */
  81. public void ejbPassivate() throws EJBException {
  82. throw new IllegalStateException("ejbPassivate must not be invoked on a stateless session bean");
  83. }
  84. }