| 1 | |
package org.jbehave.core.configuration.groovy; |
| 2 | |
|
| 3 | |
import static java.text.MessageFormat.format; |
| 4 | |
import groovy.lang.GroovyClassLoader; |
| 5 | |
import groovy.lang.GroovyCodeSource; |
| 6 | |
|
| 7 | |
import java.io.File; |
| 8 | |
import java.util.ArrayList; |
| 9 | |
import java.util.List; |
| 10 | |
|
| 11 | |
public class GroovyContext { |
| 12 | |
|
| 13 | |
private final GroovyClassLoader classLoader; |
| 14 | |
private final List<String> resources; |
| 15 | |
private List<Object> instances; |
| 16 | |
|
| 17 | |
public GroovyContext() { |
| 18 | 0 | this(new GroovyResourceFinder()); |
| 19 | 0 | } |
| 20 | |
|
| 21 | |
public GroovyContext(GroovyResourceFinder resourceFinder) { |
| 22 | 1 | this(resourceFinder.findResources()); |
| 23 | 1 | } |
| 24 | |
|
| 25 | |
public GroovyContext(List<String> resources) { |
| 26 | 2 | this(new BytecodeGroovyClassLoader(), resources); |
| 27 | 1 | } |
| 28 | |
|
| 29 | |
public GroovyContext(GroovyClassLoader classLoader, GroovyResourceFinder resourceFinder) { |
| 30 | 2 | this(classLoader, resourceFinder.findResources()); |
| 31 | 2 | } |
| 32 | |
|
| 33 | 4 | public GroovyContext(GroovyClassLoader classLoader, List<String> resources) { |
| 34 | 4 | this.classLoader = classLoader; |
| 35 | 4 | this.resources = resources; |
| 36 | 4 | this.instances = createGroovyInstances(); |
| 37 | 3 | } |
| 38 | |
|
| 39 | |
public List<Object> getInstances() { |
| 40 | 2 | return instances; |
| 41 | |
} |
| 42 | |
|
| 43 | |
@SuppressWarnings("unchecked") |
| 44 | |
public <T> T getInstanceOfType(Class<T> type) { |
| 45 | 34 | for (Object instance : instances) { |
| 46 | 34 | if (type.isAssignableFrom(instance.getClass())) { |
| 47 | 0 | return (T) instance; |
| 48 | |
} |
| 49 | |
} |
| 50 | 34 | throw new GroovyInstanceNotFound(type); |
| 51 | |
} |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
public Object newInstance(String resource) { |
| 60 | |
try { |
| 61 | 5 | String name = resource.startsWith("/") ? resource : "/" + resource; |
| 62 | 5 | File file = new File(this.getClass().getResource(name).toURI()); |
| 63 | 5 | return newInstance(classLoader.parseClass(new GroovyCodeSource(file), true)); |
| 64 | 1 | } catch (Exception e) { |
| 65 | 1 | throw new GroovyClassInstantiationFailed(classLoader, resource, e); |
| 66 | |
} |
| 67 | |
} |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
public Object newInstance(Class<?> parsedClass) throws Exception { |
| 78 | 5 | return parsedClass.newInstance(); |
| 79 | |
} |
| 80 | |
|
| 81 | |
private List<Object> createGroovyInstances() { |
| 82 | 4 | List<Object> instances = new ArrayList<Object>(); |
| 83 | 4 | for (String resource : resources) { |
| 84 | 5 | instances.add(newInstance(resource)); |
| 85 | |
} |
| 86 | 3 | return instances; |
| 87 | |
} |
| 88 | |
|
| 89 | |
@SuppressWarnings("serial") |
| 90 | |
public static final class GroovyClassInstantiationFailed extends RuntimeException { |
| 91 | |
|
| 92 | |
public GroovyClassInstantiationFailed(GroovyClassLoader classLoader, String resource, Exception cause) { |
| 93 | 1 | super(format("Failed to create new instance of class from resource {0} using Groovy class loader {1}", |
| 94 | |
resource, classLoader), cause); |
| 95 | 1 | } |
| 96 | |
|
| 97 | |
} |
| 98 | |
|
| 99 | |
@SuppressWarnings("serial") |
| 100 | |
public static final class GroovyInstanceNotFound extends RuntimeException { |
| 101 | |
|
| 102 | |
public GroovyInstanceNotFound(Class<?> type) { |
| 103 | 34 | super(type.toString()); |
| 104 | 34 | } |
| 105 | |
|
| 106 | |
} |
| 107 | |
|
| 108 | |
} |