AbstractStatelessSessionBean.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 4k
Category:

Java Develop

Development Platform:

Java

  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.  */
  44. public abstract class AbstractStatelessSessionBean extends AbstractSessionBean {
  45. protected final Log logger = LogFactory.getLog(getClass());
  46. /** 
  47.  * This implementation loads the BeanFactory. A BeansException thrown by
  48.  * loadBeanFactory will simply get propagated, as it is a runtime exception.
  49.  * <p>Don't override it (although it can't be made final): code your own
  50.  * initialization in onEjbCreate(), which is called when the BeanFactory
  51.  * is available.
  52.  * <p>Unfortunately we can't load the BeanFactory in setSessionContext(),
  53.  * as resource manager access isn't permitted there - but the BeanFactory
  54.  * may require it.
  55.  */
  56. public void ejbCreate() throws CreateException {
  57. loadBeanFactory();
  58. onEjbCreate();
  59. }
  60. /**
  61.  * Subclasses must implement this method to do any initialization
  62.  * they would otherwise have done in an ejbCreate() method. In contrast
  63.  * to ejbCreate, the BeanFactory will have been loaded here.
  64.  * <p>The same restrictions apply to the work of this method as
  65.  * to an ejbCreate() method.
  66.  * @throws CreateException
  67.  */
  68. protected abstract void onEjbCreate() throws CreateException;
  69. /**
  70.  * @see javax.ejb.SessionBean#ejbActivate(). This method always throws an exception, as
  71.  * it should not be invoked by the EJB container.
  72.  */
  73. public void ejbActivate() throws EJBException {
  74. throw new IllegalStateException("ejbActivate must not be invoked on a stateless session bean");
  75. }
  76. /**
  77.  * @see javax.ejb.SessionBean#ejbPassivate(). This method always throws an exception, as
  78.  * it should not be invoked by the EJB container.
  79.  */
  80. public void ejbPassivate() throws EJBException {
  81. throw new IllegalStateException("ejbPassivate must not be invoked on a stateless session bean");
  82. }
  83. }