/*
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.ejb3.injection;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.jboss.ejb3.BeanContext;
import org.jboss.ejb3.Container;
import org.jboss.logging.Logger;

/**
 * Comment
 *
 * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
 * @version $Revision: 1.2.2.4 $
 */
public class EntityManagerFactoryMethodInjector implements Injector
{
   private static final Logger log = Logger.getLogger(EntityManagerFactoryMethodInjector.class);
   private Method setMethod;
   private Container container;
   private Object factory;

   public EntityManagerFactoryMethodInjector(Method setMethod, Container container, Object factory)
   {
      this.setMethod = setMethod;
      this.container = container;
      this.factory = factory;
   }

   public void inject(BeanContext ctx)
   {
      try
      {
         Object[] args = {factory};
         setMethod.invoke(ctx.getInstance(), args);
      }
      catch (IllegalAccessException e)
      {
         throw new RuntimeException(e);  //To change body of catch statement use Options | File Templates.
      }
      catch (IllegalArgumentException e)
      {
         throw new RuntimeException("Failed in setting EntityManager on setter method: " + setMethod.toString());
      }
      catch (InvocationTargetException e)
      {
         throw new RuntimeException(e.getCause());  //To change body of catch statement use Options | File Templates.
      }
   }
}
