/
PropertyBasedPluginFeatureFactory.java
216 lines (190 loc) · 8.3 KB
/
PropertyBasedPluginFeatureFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package fitnesse.plugins;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import fitnesse.ConfigurationParameter;
import fitnesse.authentication.Authenticator;
import fitnesse.components.ComponentFactory;
import fitnesse.components.ComponentInstantiationException;
import fitnesse.reporting.Formatter;
import fitnesse.reporting.FormatterFactory;
import fitnesse.reporting.FormatterRegistry;
import fitnesse.responders.ResponderFactory;
import fitnesse.responders.editing.ContentFilter;
import fitnesse.testrunner.TestSystemFactoryRegistry;
import fitnesse.testsystems.TestSystemFactory;
import fitnesse.testsystems.slim.CustomComparator;
import fitnesse.testsystems.slim.CustomComparatorRegistry;
import fitnesse.testsystems.slim.tables.SlimTable;
import fitnesse.testsystems.slim.tables.SlimTableFactory;
import fitnesse.wiki.WikiPageFactory;
import fitnesse.wiki.WikiPageFactoryRegistry;
import fitnesse.wikitext.parser.SymbolProvider;
import fitnesse.wikitext.parser.SymbolType;
/**
* Determines which plugin features to load based on componentFactory's properties (e.g. plugins.properties).
*/
public class PropertyBasedPluginFeatureFactory extends PluginFeatureFactoryBase {
private final ComponentFactory componentFactory;
public static Collection<PluginFeatureFactory> loadFromProperties(ComponentFactory componentFactory) throws PluginException {
PropertyBasedPluginFeatureFactory propBased = new PropertyBasedPluginFeatureFactory(componentFactory);
Collection<PluginFeatureFactory> legacyWrappers = createWrappersForLegacyPlugins(componentFactory);
List<PluginFeatureFactory> all = new ArrayList<PluginFeatureFactory>(legacyWrappers.size() + 1);
all.add(propBased);
all.addAll(legacyWrappers);
return all;
}
private PropertyBasedPluginFeatureFactory(ComponentFactory componentFactory) {
this.componentFactory = componentFactory;
}
@Override
public void registerResponders(final ResponderFactory responderFactory) throws PluginException {
forEachNamedObject(ConfigurationParameter.RESPONDERS, new KeyRegistrar() {
@Override public void register(String key, Class clazz) {
responderFactory.addResponder(key, clazz);
LOG.info("Loaded responder " + key + ": " + clazz.getName());
}
});
}
private String[] getListFromProperties(ConfigurationParameter propertyName) {
return getListFromProperties(componentFactory, propertyName);
}
private static String[] getListFromProperties(ComponentFactory componentFactory, ConfigurationParameter propertyName) {
String value = componentFactory.getProperty(propertyName.getKey());
if (value == null)
return null;
else
return value.split(",");
}
@Override
public Authenticator getAuthenticator() {
return componentFactory.createComponent(ConfigurationParameter.AUTHENTICATOR);
}
@Override
public void registerSymbolTypes(final SymbolProvider symbolProvider) throws PluginException {
forEachObject(ConfigurationParameter.SYMBOL_TYPES, new Registrar<SymbolType>() {
@Override public void register(SymbolType instance) {
symbolProvider.add(instance);
LOG.info("Loaded SymbolType " + instance.getClass().getName());
}
});
}
@Override
public void registerWikiPageFactories(final WikiPageFactoryRegistry registrar) throws PluginException {
forEachObject(ConfigurationParameter.WIKI_PAGE_FACTORIES, new Registrar<WikiPageFactory>() {
@Override public void register(WikiPageFactory instance) {
registrar.registerWikiPageFactory(instance);
LOG.info("Loaded WikiPageFactory " + instance.getClass().getName());
}
});
}
@Override
public void registerFormatters(final FormatterRegistry registrar) throws PluginException {
forEachClass(ConfigurationParameter.FORMATTERS, new ClassRegistrar<Formatter>() {
@Override
public void register(Class<Formatter> clazz) {
registrar.registerFormatter(clazz);
LOG.info("Loaded formatter " + clazz.getName());
}
});
}
@Override
public ContentFilter getContentFilter() {
return componentFactory.createComponent(ConfigurationParameter.CONTENT_FILTER);
}
@Override
public void registerSlimTables(final SlimTableFactory slimTableFactory) throws PluginException {
forEachNamedObject(ConfigurationParameter.SLIM_TABLES, new KeyRegistrar<SlimTable>() {
@Override public void register(String key, Class<SlimTable> clazz) {
slimTableFactory.addTableType(key, clazz);
LOG.info("Loaded custom SLiM table type " + key + ":" + clazz.getName());
}
});
}
@Override
public void registerCustomComparators(final CustomComparatorRegistry customComparatorRegistry) throws PluginException {
forEachNamedObject(ConfigurationParameter.CUSTOM_COMPARATORS, new KeyRegistrar<CustomComparator>() {
@Override public void register(String key, Class<CustomComparator> clazz) {
customComparatorRegistry.addCustomComparator(key, componentFactory.createComponent(clazz));
LOG.info("Loaded custom comparator " + key + ": " + clazz.getName());
}
});
}
@Override
public void registerTestSystemFactories(final TestSystemFactoryRegistry registrar) throws PluginException {
forEachNamedObject(ConfigurationParameter.TEST_SYSTEMS, new KeyRegistrar<TestSystemFactory>() {
@Override public void register(String key, Class<TestSystemFactory> clazz) {
registrar.registerTestSystemFactory(key, componentFactory.createComponent(clazz));
LOG.info("Loaded test system " + key + ": " + clazz.getName());
}
});
}
private <T> void forEachClass(final ConfigurationParameter parameter, ClassRegistrar<T> registrar) throws PluginException {
String[] propList = getListFromProperties(parameter);
if (propList != null) {
for (String entry : propList) {
Class<T> clazz = forName(entry.trim());
registrar.register(clazz);
}
}
}
private <T> void forEachObject(final ConfigurationParameter parameter, final Registrar<T> registrar) throws PluginException {
forEachClass(parameter, new ClassRegistrar<T>() {
@Override public void register(Class<T> clazz) {
registrar.register(componentFactory.createComponent(clazz));
}
});
}
private <T> void forEachNamedObject(final ConfigurationParameter parameter, KeyRegistrar<T> registrar) throws PluginException {
String[] propList = getListFromProperties(parameter);
if (propList != null) {
for (String entry : propList) {
entry = entry.trim();
int colonIndex = entry.lastIndexOf(':');
String prefix = entry.substring(0, colonIndex);
String className = entry.substring(colonIndex + 1, entry.length());
register(registrar, prefix, className);
}
}
}
private <T> void register(KeyRegistrar<T> registrar, String prefix, String className) throws PluginException {
try {
Class<T> clazz = forName(className);
registrar.register(prefix, clazz);
} catch (ComponentInstantiationException e) {
throw new PluginException("Can not register plug in " + className, e);
}
}
private static Collection<PluginFeatureFactory> createWrappersForLegacyPlugins(ComponentFactory componentFactory) throws PluginException {
String[] pluginNames = getListFromProperties(componentFactory, ConfigurationParameter.PLUGINS);
if (pluginNames == null) {
return Collections.emptyList();
} else {
List<PluginFeatureFactory> providers = new ArrayList<PluginFeatureFactory>(pluginNames.length);
for (String pluginName : pluginNames) {
Class<?> pluginClass = forName(pluginName);
Object plugin = componentFactory.createComponent(pluginClass);
providers.add(new LegacyPluginFeatureFactory(plugin));
}
return providers;
}
}
@SuppressWarnings("unchecked")
private static <T> Class<T> forName(String className) throws PluginException {
try {
return (Class<T>) Class.forName(className);
} catch (ClassNotFoundException e) {
throw new PluginException("Unable to load class " + className, e);
}
}
private interface Registrar<T> {
void register(T instance);
}
private interface ClassRegistrar<T> {
void register(Class<T> clazz);
}
private interface KeyRegistrar<T> {
void register(String key, Class<T> clazz);
}
}