GroovyAnnotationBuilder.java

  1. package org.jbehave.core.configuration.groovy;

  2. import org.jbehave.core.annotations.groovy.UsingGroovy;
  3. import org.jbehave.core.configuration.AnnotationBuilder;
  4. import org.jbehave.core.configuration.AnnotationMonitor;
  5. import org.jbehave.core.configuration.AnnotationRequired;
  6. import org.jbehave.core.configuration.Configuration;
  7. import org.jbehave.core.steps.CompositeStepsFactory;
  8. import org.jbehave.core.steps.InjectableStepsFactory;
  9. import org.jbehave.core.steps.groovy.GroovyStepsFactory;

  10. import groovy.lang.GroovyClassLoader;

  11. /**
  12.  * Extends {@link AnnotationBuilder} using Groovy-based resources if
  13.  * {@link UsingGroovy} annotation is present.
  14.  *
  15.  * @author Mauro Talevi
  16.  */
  17. public class GroovyAnnotationBuilder extends AnnotationBuilder {

  18.     private GroovyContext context;

  19.     public GroovyAnnotationBuilder(Class<?> annotatedClass) {
  20.         super(annotatedClass);
  21.     }

  22.     public GroovyAnnotationBuilder(Class<?> annotatedClass, AnnotationMonitor annotationMonitor) {
  23.         super(annotatedClass, annotationMonitor);
  24.     }

  25.     @SuppressWarnings("unchecked")
  26.     @Override
  27.     public Configuration buildConfiguration() throws AnnotationRequired {
  28.         if (annotationFinder().isAnnotationPresent(UsingGroovy.class)) {
  29.             Class<GroovyClassLoader> classLoaderClass = annotationFinder().getAnnotatedValue(UsingGroovy.class,
  30.                     Class.class, "classLoader");
  31.             Class<GroovyResourceFinder> resourceFinderClass = annotationFinder().getAnnotatedValue(UsingGroovy.class,
  32.                     Class.class, "resourceFinder");
  33.             try {
  34.                 GroovyClassLoader classLoader = super.instanceOf(classLoaderClass, classLoaderClass);
  35.                 GroovyResourceFinder resourceFinder = super.instanceOf(resourceFinderClass, resourceFinderClass);
  36.                 context = createGroovyContext(classLoader, resourceFinder);
  37.             } catch (Exception e) {
  38.                 annotationMonitor().elementCreationFailed(GroovyContext.class, e);
  39.             }
  40.         } else {
  41.             annotationMonitor().annotationNotFound(UsingGroovy.class, annotatedClass());
  42.         }
  43.         return super.buildConfiguration();
  44.     }

  45.     @Override
  46.     public InjectableStepsFactory buildStepsFactory(Configuration configuration) {
  47.         InjectableStepsFactory factoryUsingSteps = super.buildStepsFactory(configuration);
  48.         if (context != null) {
  49.             return new CompositeStepsFactory(new GroovyStepsFactory(configuration, context), factoryUsingSteps);
  50.         }
  51.         return factoryUsingSteps;
  52.     }
  53.    
  54.     @Override
  55.     protected <T, V extends T> T instanceOf(Class<T> type, Class<V> ofClass) {
  56.         if (context != null) {
  57.             try {
  58.                 return context.getInstanceOfType(type);
  59.             } catch (Exception e) {
  60.                 // default to super class
  61.             }
  62.         }
  63.         return super.instanceOf(type, ofClass);
  64.     }

  65.     protected GroovyContext createGroovyContext(GroovyClassLoader classLoader, GroovyResourceFinder resourceFinder) {
  66.         if (context != null) {
  67.             return context;
  68.         }
  69.         return new GroovyContext(classLoader, resourceFinder);
  70.     }

  71. }