1 | |
package org.jbehave.core.configuration.groovy; |
2 | |
|
3 | |
import static java.text.MessageFormat.format; |
4 | |
import groovy.lang.GroovyClassLoader; |
5 | |
|
6 | |
import java.io.File; |
7 | |
import java.util.ArrayList; |
8 | |
import java.util.List; |
9 | |
|
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 GroovyClassLoader(), 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 | 30 | for (Object instance : instances ) { |
46 | 30 | if (type.isAssignableFrom(instance.getClass()) ) { |
47 | 0 | return (T) instance; |
48 | |
} |
49 | |
} |
50 | 30 | throw new GroovyInstanceNotFound(type); |
51 | |
} |
52 | |
|
53 | |
public Object newInstance(String resource) { |
54 | |
try { |
55 | 5 | String name = resource.startsWith("/") ? resource : "/" + resource; |
56 | 5 | File file = new File(this.getClass().getResource(name).toURI()); |
57 | 5 | return classLoader.parseClass(file).newInstance(); |
58 | 1 | } catch (Exception e) { |
59 | 1 | throw new GroovyClassInstantiationFailed(classLoader, resource, e); |
60 | |
} |
61 | |
} |
62 | |
|
63 | |
private List<Object> createGroovyInstances() { |
64 | 4 | List<Object> instances = new ArrayList<Object>(); |
65 | 4 | for (String resource : resources) { |
66 | 5 | instances.add(newInstance(resource)); |
67 | |
} |
68 | 3 | return instances; |
69 | |
} |
70 | |
|
71 | |
@SuppressWarnings("serial") |
72 | |
public static final class GroovyClassInstantiationFailed extends RuntimeException { |
73 | |
|
74 | |
public GroovyClassInstantiationFailed(GroovyClassLoader classLoader, String resource, Exception cause) { |
75 | 1 | super(format("Failed to create new instance of class from resource {0} using Groovy class loader {1}", |
76 | |
resource, classLoader), cause); |
77 | 1 | } |
78 | |
|
79 | |
} |
80 | |
|
81 | |
@SuppressWarnings("serial") |
82 | |
public static final class GroovyInstanceNotFound extends RuntimeException { |
83 | |
|
84 | |
public GroovyInstanceNotFound(Class<?> type) { |
85 | 30 | super(type.toString()); |
86 | 30 | } |
87 | |
|
88 | |
} |
89 | |
|
90 | |
} |