WeldStepsFactory.java

  1. package org.jbehave.core.steps.weld;

  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import javax.enterprise.inject.Any;
  5. import javax.enterprise.inject.Instance;
  6. import javax.inject.Inject;
  7. import javax.inject.Singleton;

  8. import org.jbehave.core.annotations.weld.WeldConfiguration;
  9. import org.jbehave.core.annotations.weld.WeldStep;
  10. import org.jbehave.core.configuration.Configuration;
  11. import org.jbehave.core.steps.AbstractStepsFactory;
  12. import org.jbehave.core.steps.InjectableStepsFactory;

  13. /**
  14.  * An {@link InjectableStepsFactory} that uses a Weld {@link Inject} for the
  15.  * composition and instantiation of all components that contain JBehave
  16.  * annotated methods.
  17.  *
  18.  * @author Aaron Walker
  19.  */
  20. @Singleton
  21. public class WeldStepsFactory extends AbstractStepsFactory {

  22.     @Inject
  23.     @Any
  24.     @WeldStep
  25.     private Instance<Object> instances;

  26.     @Inject
  27.     public WeldStepsFactory(@WeldConfiguration Configuration configuration) {
  28.         super(configuration);
  29.     }

  30.     @Override
  31.     protected List<Class<?>> stepsTypes() {
  32.         List<Class<?>> types = new ArrayList<>();
  33.         for (Object o : instances) {
  34.             types.add(o.getClass());
  35.         }
  36.         return types;
  37.     }

  38.     @Override
  39.     public Object createInstanceOfType(Class<?> type) {
  40.         for (Object o : instances) {
  41.             if (type.equals(o.getClass())) {
  42.                 return o;
  43.             }
  44.         }
  45.         throw new StepsInstanceNotFound(type, this);
  46.     }

  47. }