[jboss-user] [EJB 3.0 Development] New message: "Re: Deferring instance creation/injection to CDI"

Andrew Rubinger do-not-reply at jboss.com
Mon Mar 15 18:07:39 EDT 2010


User development,

A new message was posted in the thread "Deferring instance creation/injection to CDI":

http://community.jboss.org/message/532123#532123

Author  : Andrew Rubinger
Profile : http://community.jboss.org/people/ALRubinger

Message:
--------------------------------------------------------------
https://jira.jboss.org/jira/browse/EJBTHREE-2046
 
Looks like we can extract the instantiation logic, and let EJB3 provide it's own implementation.  CDI, if it likes, can swap out for their own during deployment.  I've committed r102436 to see if this can be a workable solution for both teams.
 
/**
 * Contract of a component responsible for creation of EJB bean 
 * and instances.  Though the EJB specification dictates 
 * the use of a no-arg constructor, contextual EJBs defined by JSR-299 
 * provides for additional features such as injected constructor parameters, 
 * so alternate implementations of this type may be used by the 
 * EJB container.
 *
 * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
 * @version $Revision: $
 */
public interface BeanInstantiator
{
   //-------------------------------------------------------------------------------------||
   // Contracts --------------------------------------------------------------------------||
   //-------------------------------------------------------------------------------------||
 
   /**
    * Creates a new instance of the specified implementation {@link Class}
    * using the supplied parameters.  While the implementation class 
    * must be specified, <code>null</code> is a valid value for the parameters,
    * and will be treated as a 0-length array.
    * 
    * @throws IllegalArgumentException If the implementation class was not specified
    * or the implementation does not support parameters (while they've been supplied).
    * @throws InvalidConstructionParamsException If the implementation of this type does 
    * not support the parameters supplied.
    * @throws BeanInstantiationException If an unexpected error occurred in constructing the bean
    * instance
    */
   <T> T create(Class<T> implClass, Object[] parameters) throws IllegalArgumentException,
         InvalidConstructionParamsException, BeanInstantiationException;
}

 
The EJB3 impl is simple:
 
/**
 * Implementation of {@link BeanInstantiator} strictly conforming to the
 * EJB 3.1 Specification.  Any non-0-length or non-null parameter arguments to 
 * {@link Ejb31SpecBeanInstantiator#create(Class, Object[])}
 * will result in {@link InvalidConstructionParamsException}
 *
 * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
 * @version $Revision: $
 */
public class Ejb31SpecBeanInstantiator implements BeanInstantiator
{
 
   //-------------------------------------------------------------------------------------||
   // Class Members ----------------------------------------------------------------------||
   //-------------------------------------------------------------------------------------||
 
   /**
    * Logger
    */
   private static final Logger log = Logger.getLogger(Ejb31SpecBeanInstantiator.class.getName());
 
   /**
    * Message used in reporting exceptions during instantiation
    */
   private static final String MSG_PREFIX_INSTANTIATION_EXCEPTION = "Could not create new instance with no arguments of: ";
 
   //-------------------------------------------------------------------------------------||
   // Required Implementations -----------------------------------------------------------||
   //-------------------------------------------------------------------------------------||
 
   /**
    * {@inheritDoc}
    * @see org.jboss.ejb3.instantiator.spi.BeanInstantiator#create(java.lang.Class, java.lang.Object[])
    */
   public <T> T create(final Class<T> implClass, final Object[] parameters) throws IllegalArgumentException,
         InvalidConstructionParamsException, BeanInstantiationException
   {
      // Precondition checks
      if (parameters != null && parameters.length > 0)
      {
         throw InvalidConstructionParamsException
               .newInstance("EJB Specification requires a no-argument constructor be invoked for bean instances on "
                     + implClass.getName());
      }
 
      // Instantiate
      final T obj;
      try
      {
         obj = implClass.newInstance();
      }
      catch (final InstantiationException e)
      {
         throw BeanInstantiationException.newInstance(MSG_PREFIX_INSTANTIATION_EXCEPTION + implClass, e);
      }
      catch (final IllegalAccessException e)
      {
         throw BeanInstantiationException.newInstance(MSG_PREFIX_INSTANTIATION_EXCEPTION + implClass, e);
      }
 
      // Log
      if (log.isLoggable(Level.FINER))
      {
         log.finer("Created: " + obj + "; instance of " + implClass.getName());
      }
 
      // Return
      return obj;
   }
 
}

 
S,
ALR

--------------------------------------------------------------

To reply to this message visit the message page: http://community.jboss.org/message/532123#532123




More information about the jboss-user mailing list