1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.esigate.extension;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
21 import java.util.Properties;
22
23 import org.esigate.ConfigurationException;
24 import org.esigate.Driver;
25 import org.esigate.util.Parameter;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29
30
31
32
33
34
35
36 public final class ExtensionFactory {
37 private static final Logger LOG = LoggerFactory.getLogger(ExtensionFactory.class);
38
39 private ExtensionFactory() {
40
41 }
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public static <T extends Extension> T getExtension(Properties properties, Parameter<String> parameter, Driver d) {
58 T result;
59 String className = parameter.getValue(properties);
60 if (className == null) {
61 return null;
62 }
63 try {
64 if (LOG.isDebugEnabled()) {
65 LOG.debug("Creating extension " + className);
66 }
67 result = (T) Class.forName(className).newInstance();
68 result.init(d, properties);
69 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
70 throw new ConfigurationException(e);
71 }
72 return result;
73 }
74
75
76
77
78
79
80
81
82
83 public static <T extends Extension> List<T> getExtensions(Properties properties,
84 Parameter<Collection<String>> parameter, Driver d) {
85 Collection<String> className = parameter.getValue(properties);
86 List<T> finalResult = new ArrayList<>();
87 for (String cName : className) {
88 try {
89 T result;
90
91 if (LOG.isDebugEnabled()) {
92 LOG.debug("Creating extension " + className);
93 }
94 result = (T) Class.forName(cName).newInstance();
95 result.init(d, properties);
96 finalResult.add(result);
97 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
98 throw new ConfigurationException(e);
99 }
100 }
101 return finalResult;
102 }
103
104 }