1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package tools.gsf.config;
17
18 import org.apache.commons.lang3.StringUtils;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import java.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.lang.reflect.InvocationTargetException;
27 import java.lang.reflect.Method;
28 import java.net.URL;
29 import java.util.Enumeration;
30 import java.util.HashSet;
31 import java.util.Set;
32
33
34
35
36
37
38
39 final class ReflectionUtils {
40
41 private static final Logger LOG = LoggerFactory.getLogger(ReflectionUtils.class);
42
43 private ReflectionUtils() {
44 }
45
46
47
48
49
50
51
52
53
54 static URL getSingleResource(ClassLoader classLoader, String resourceName) {
55 try {
56 Enumeration<URL> resources = classLoader.getResources(resourceName);
57 URL result = null;
58 boolean bFound = false;
59 while (resources.hasMoreElements()) {
60 if (bFound) {
61 throw new IllegalStateException("Too many resources found matching name: " + resourceName);
62 }
63 result = resources.nextElement();
64 bFound = true;
65 }
66 return result;
67 } catch (IOException e) {
68 throw new IllegalStateException("Failed to locate resource: " + resourceName, e);
69 }
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92 static Set<String> readConfigurationResource(ClassLoader classLoader, String resourceName) {
93 Set<String> lines = new HashSet<>();
94 try {
95 Enumeration<URL> resources = classLoader.getResources(resourceName);
96 while (resources.hasMoreElements()) {
97 URL url = resources.nextElement();
98 try (InputStream in = url.openStream(); BufferedReader r = new BufferedReader(new InputStreamReader(in, "utf-8"))) {
99 String line;
100 while ((line = r.readLine()) != null) {
101 line = StringUtils.normalizeSpace(line);
102 if (StringUtils.isNotBlank(line) && !StringUtils.startsWith(line, "#")) {
103 if (lines.contains(line)) {
104 throw new IllegalStateException("Duplicate configuration information found in resource named " + resourceName + ". The following information was found more than once: " + line);
105 }
106 lines.add(line);
107 }
108 }
109 } catch (IOException e) {
110 throw new RuntimeException("Error reading configuration resource: " + resourceName, e);
111 }
112 }
113 } catch (IOException e) {
114 throw new RuntimeException("Error reading configuration resource: " + resourceName, e);
115 }
116 return lines;
117 }
118
119
120
121
122
123
124
125
126
127
128 @SuppressWarnings("unchecked")
129 static <T> T createFromMethod(String name, Class<T> typeTocreate, Object factory, Method factoryMethod, Object... params) throws InvocationTargetException {
130 Object o = null;
131 LOG.trace("Trying to create a {} object with name {} from method {} from factory {}", typeTocreate.getName(), name, factoryMethod.toGenericString(), factory.getClass().getName());
132
133 if (typeTocreate.isAssignableFrom(factoryMethod.getReturnType())) {
134 try {
135 o = factoryMethod.invoke(factory, params);
136 } catch (IllegalAccessException e) {
137 throw new IllegalStateException("Access exception creating object " + typeTocreate.getName() + ": " + e, e);
138 }
139 }
140 return (T) o;
141 }
142 }