private BindingsConfigurationLoader getPluginsConfigurationLoader(Collection<ServiceBinding> bindings) {
ServiceBinding pluginsFinderBinding = null;
for (ServiceBinding binding : bindings) {
if (PluginsFinder.class.getCanonicalName().equals(binding.getInterfaceName())) {
if (pluginsFinderBinding != null)
throw new ServiceStartupException("multiple bindings found for " + PluginsFinder.class);
pluginsFinderBinding = binding;
}
}
if (pluginsFinderBinding == null)
return emptyConfigurationLoader;
String pluginsFinderClassName = pluginsFinderBinding.getImplementingClassName();
Class<?> pluginsFinderClass;
try {
pluginsFinderClass = Class.forName(pluginsFinderClassName);
}
catch (ClassNotFoundException e) {
throw new ServiceStartupException("couldn't get Class object for " + pluginsFinderClassName);
}
PluginsFinder pluginsFinder;
try {
pluginsFinder = (PluginsFinder) pluginsFinderClass.newInstance();
}
catch (Exception e) {
logger.error("while instantiating plugins finder", e);
logger.error("plugins finder must have a no-arg constructor, though there may be something else wrong");
throw new ServiceStartupException("error while instantiating plugins finder. please check logs");
}
CompositeConfigurationLoader compositeLoader = new CompositeConfigurationLoader();
Collection<? extends Plugin> plugins = pluginsFinder.get();
List<URL> pluginUrls = new ArrayList<>(plugins.size());
for (Plugin plugin : plugins) {
URL url = plugin.getClassLoaderURL();
if (url != null) {
pluginUrls.add(url);
}
}
ClassLoader pluginsClassloader = null;
if (!pluginUrls.isEmpty()) {
pluginsClassloader = new URLClassLoader(pluginUrls.toArray(new URL[pluginUrls.size()]));
}
for (Plugin plugin : plugins) {
try {
YamlConfiguration pluginConfig = new YamlConfiguration(
plugin.toString(),
plugin.getServiceConfigsReader(),
pluginsClassloader);
compositeLoader.add(pluginConfig);
}
catch (IOException e) {
logger.error("while reading services config for " + plugin, e);
throw new ServiceStartupException("error while reading services config for " + plugin);
}
}
return compositeLoader;
}