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.MessageDrivenBean;
  18. import javax.ejb.MessageDrivenContext;
  19. import org.apache.commons.logging.Log;
  20. import org.apache.commons.logging.LogFactory;
  21. /**
  22. * Convenient superclass for MDBs.
  23. * Doesn't require JMS, as EJB 2.1 MDBs are no longer
  24. * JMS-specific: see the AbstractJmsMessageDrivenBean subclass.
  25. *
  26. * <p>This class ensures that subclasses have access to the
  27. * MessageDrivenContext provided by the EJB container, and implement
  28. * a no argument ejbCreate() method as required by the EJB specification.
  29. * This ejbCreate() method loads a BeanFactory, before invoking the
  30. * onEjbCreate() method, which should contain subclass-specific
  31. * initialization.
  32. *
  33. * <p>NB: We cannot use final methods to implement EJB API methods,
  34. * as this violates the EJB specification. However, there should
  35. * be no need to override the setMessageDrivenContext() or
  36. * ejbCreate() methods.
  37. *
  38. * @author Rod Johnson
  39. * @version $Id: AbstractMessageDrivenBean.java,v 1.5 2004/03/18 02:46:14 trisberg Exp $
  40. */
  41. public abstract class AbstractMessageDrivenBean extends AbstractEnterpriseBean
  42. implements MessageDrivenBean {
  43. protected final Log logger = LogFactory.getLog(getClass());
  44. private MessageDrivenContext messageDrivenContext;
  45. /**
  46. * Required lifecycle method. Sets the MessageDriven context.
  47. * @param messageDrivenContext MessageDrivenContext
  48. */
  49. public void setMessageDrivenContext(MessageDrivenContext messageDrivenContext) {
  50. this.messageDrivenContext = messageDrivenContext;
  51. }
  52. /**
  53. * Convenience method for subclasses to use.
  54. * @return the MessageDrivenContext passed to this EJB by the EJB container
  55. */
  56. protected final MessageDrivenContext getMessageDrivenContext() {
  57. return messageDrivenContext;
  58. }
  59. /**
  60. * Lifecycle method required by the EJB specification but not the
  61. * MessageDrivenBean interface. This implementation loads the BeanFactory.
  62. * <p>Don't override it (although it can't be made final): code initialization
  63. * in onEjbCreate(), which is called when the BeanFactory is available.
  64. * <p>Unfortunately we can't load the BeanFactory in setSessionContext(),
  65. * as resource manager access isn't permitted and the BeanFactory may require it.
  66. */
  67. public void ejbCreate() {
  68. loadBeanFactory();
  69. onEjbCreate();
  70. }
  71. /**
  72. * Subclasses must implement this method to do any initialization
  73. * they would otherwise have done in an ejbCreate() method. In contrast
  74. * to ejbCreate, the BeanFactory will have been loaded here.
  75. * <p>The same restrictions apply to the work of this method as
  76. * to an ejbCreate() method.
  77. */
  78. protected abstract void onEjbCreate();
  79. }