2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
2014-12-19 17:15:50 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
2014-12-19 17:15:50 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2014-12-19 17:15:50 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
2014-12-19 17:15:50 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
2014-12-19 17:15:50 +01:00
|
|
|
*/
|
2020-04-16 11:57:38 +02:00
|
|
|
|
2014-12-19 17:15:50 +01:00
|
|
|
package sonia.scm.plugin;
|
|
|
|
|
|
|
|
|
|
import com.google.common.collect.HashMultimap;
|
|
|
|
|
import com.google.common.collect.Iterables;
|
|
|
|
|
import com.google.common.collect.Maps;
|
|
|
|
|
import com.google.common.collect.Multimap;
|
|
|
|
|
import com.google.common.collect.Sets;
|
2020-01-08 14:27:11 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2014-12-19 17:15:50 +01:00
|
|
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.Collections;
|
2020-04-07 12:49:25 +02:00
|
|
|
import java.util.HashSet;
|
2014-12-19 17:15:50 +01:00
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
import java.util.Set;
|
2020-01-08 14:27:11 +01:00
|
|
|
import java.util.stream.Collectors;
|
2014-12-19 17:15:50 +01:00
|
|
|
|
2021-09-14 09:26:47 +02:00
|
|
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
|
|
|
|
public final class ExtensionCollector {
|
2014-12-19 17:15:50 +01:00
|
|
|
|
2020-01-08 14:27:11 +01:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(ExtensionCollector.class);
|
|
|
|
|
|
|
|
|
|
private final Set<String> pluginIndex;
|
|
|
|
|
|
2021-09-14 09:26:47 +02:00
|
|
|
private final Set<WebElementExtension> webElements = Sets.newHashSet();
|
|
|
|
|
private final Set<Class<?>> indexedTypes = Sets.newHashSet();
|
|
|
|
|
private final Set<Class> restResources = Sets.newHashSet();
|
|
|
|
|
private final Set<Class> restProviders = Sets.newHashSet();
|
|
|
|
|
private final Set<Class> mappers = Sets.newHashSet();
|
|
|
|
|
private final Set<Class> looseExtensions = Sets.newHashSet();
|
|
|
|
|
private final Multimap<ExtensionPointElement, Class> extensions = HashMultimap.create();
|
|
|
|
|
private final Map<Class, ExtensionPointElement> extensionPointIndex = Maps.newHashMap();
|
|
|
|
|
|
2020-01-08 14:27:11 +01:00
|
|
|
public ExtensionCollector(ClassLoader moduleClassLoader, Set<ScmModule> modules, Set<InstalledPlugin> installedPlugins) {
|
|
|
|
|
this.pluginIndex = createPluginIndex(installedPlugins);
|
|
|
|
|
|
|
|
|
|
for (ScmModule module : modules) {
|
2020-04-07 12:49:25 +02:00
|
|
|
collectRootElements(moduleClassLoader, module);
|
2014-12-19 17:15:50 +01:00
|
|
|
}
|
2020-04-16 11:57:38 +02:00
|
|
|
|
2020-04-07 12:49:25 +02:00
|
|
|
for (InstalledPlugin plugin : installedPlugins) {
|
|
|
|
|
collectRootElements(plugin.getClassLoader(), plugin.getDescriptor());
|
2020-01-08 14:27:11 +01:00
|
|
|
}
|
2014-12-19 17:15:50 +01:00
|
|
|
|
2020-01-08 14:27:11 +01:00
|
|
|
for (ScmModule module : modules) {
|
|
|
|
|
collectExtensions(moduleClassLoader, module);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (InstalledPlugin plugin : installedPlugins) {
|
|
|
|
|
collectExtensions(plugin.getClassLoader(), plugin.getDescriptor());
|
2014-12-19 17:15:50 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-08 14:27:11 +01:00
|
|
|
private Set<String> createPluginIndex(Set<InstalledPlugin> installedPlugins) {
|
|
|
|
|
return installedPlugins.stream()
|
|
|
|
|
.map(p -> p.getDescriptor().getInformation().getName())
|
|
|
|
|
.collect(Collectors.toSet());
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-14 09:26:47 +02:00
|
|
|
public Collection<Class> byExtensionPoint(ExtensionPointElement epe) {
|
2014-12-19 17:15:50 +01:00
|
|
|
Collection<Class> collection = extensions.get(epe);
|
2014-12-19 17:41:46 +01:00
|
|
|
|
2021-09-14 09:26:47 +02:00
|
|
|
if (collection == null) {
|
2020-04-16 12:03:12 +02:00
|
|
|
collection = Collections.emptySet();
|
2014-12-19 17:15:50 +01:00
|
|
|
}
|
2014-12-19 17:41:46 +01:00
|
|
|
|
2014-12-19 17:15:50 +01:00
|
|
|
return collection;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-14 09:26:47 +02:00
|
|
|
public Collection<Class> byExtensionPoint(Class clazz) {
|
2014-12-19 17:15:50 +01:00
|
|
|
Collection<Class> exts;
|
|
|
|
|
ExtensionPointElement epe = extensionPointIndex.get(clazz);
|
|
|
|
|
|
2021-09-14 09:26:47 +02:00
|
|
|
if (epe != null) {
|
2014-12-19 17:15:50 +01:00
|
|
|
exts = byExtensionPoint(epe);
|
2021-09-14 09:26:47 +02:00
|
|
|
} else {
|
2020-04-16 12:03:12 +02:00
|
|
|
exts = Collections.emptySet();
|
2014-12-19 17:15:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return exts;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-14 09:26:47 +02:00
|
|
|
public Class oneByExtensionPoint(ExtensionPointElement epe) {
|
2014-12-19 17:15:50 +01:00
|
|
|
return Iterables.getFirst(byExtensionPoint(epe), null);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-14 09:26:47 +02:00
|
|
|
public Class oneByExtensionPoint(Class clazz) {
|
2014-12-19 17:15:50 +01:00
|
|
|
return Iterables.getFirst(byExtensionPoint(clazz), null);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-14 09:26:47 +02:00
|
|
|
public Iterable<ExtensionPointElement> getExtensionPointElements() {
|
2014-12-19 17:15:50 +01:00
|
|
|
return extensionPointIndex.values();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-14 09:26:47 +02:00
|
|
|
public Multimap<ExtensionPointElement, Class> getExtensions() {
|
2014-12-19 17:15:50 +01:00
|
|
|
return extensions;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-14 09:26:47 +02:00
|
|
|
public Set<Class> getLooseExtensions() {
|
2014-12-19 17:15:50 +01:00
|
|
|
return looseExtensions;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-14 09:26:47 +02:00
|
|
|
public Set<Class> getRestProviders() {
|
2014-12-19 17:15:50 +01:00
|
|
|
return restProviders;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-14 09:26:47 +02:00
|
|
|
public Set<Class> getRestResources() {
|
2014-12-19 17:15:50 +01:00
|
|
|
return restResources;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-14 09:26:47 +02:00
|
|
|
public Set<Class> getMappers() {
|
|
|
|
|
return mappers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Set<WebElementExtension> getWebElements() {
|
2015-02-01 19:14:46 +01:00
|
|
|
return webElements;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-19 08:48:43 +02:00
|
|
|
public Set<Class<?>> getIndexedTypes() {
|
|
|
|
|
return indexedTypes;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-14 09:26:47 +02:00
|
|
|
private void appendExtension(Class extension) {
|
2014-12-19 17:15:50 +01:00
|
|
|
boolean found = false;
|
|
|
|
|
|
2021-09-14 09:26:47 +02:00
|
|
|
for (Entry<Class, ExtensionPointElement> e : extensionPointIndex.entrySet()) {
|
|
|
|
|
if (e.getKey().isAssignableFrom(extension)) {
|
2014-12-19 17:15:50 +01:00
|
|
|
extensions.put(e.getValue(), extension);
|
|
|
|
|
found = true;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-14 09:26:47 +02:00
|
|
|
if (!found) {
|
2014-12-19 17:15:50 +01:00
|
|
|
looseExtensions.add(extension);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-08 14:27:11 +01:00
|
|
|
private void collectExtensions(ClassLoader defaultClassLoader, ScmModule module) {
|
2020-04-07 12:49:25 +02:00
|
|
|
for (ClassElement extension : module.getExtensions()) {
|
2020-01-08 14:27:11 +01:00
|
|
|
if (isRequirementFulfilled(extension)) {
|
2021-07-19 08:48:43 +02:00
|
|
|
Class<?> extensionClass = load(defaultClassLoader, extension);
|
2020-01-08 14:27:11 +01:00
|
|
|
appendExtension(extensionClass);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-07 12:49:25 +02:00
|
|
|
private Set<Class<?>> collectClasses(ClassLoader defaultClassLoader, Iterable<ClassElement> classElements) {
|
|
|
|
|
Set<Class<?>> classes = new HashSet<>();
|
|
|
|
|
for (ClassElement element : classElements) {
|
|
|
|
|
if (isRequirementFulfilled(element)) {
|
2021-07-19 08:48:43 +02:00
|
|
|
Class<?> loadedClass = load(defaultClassLoader, element);
|
2020-04-07 12:49:25 +02:00
|
|
|
classes.add(loadedClass);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return classes;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-19 08:48:43 +02:00
|
|
|
private Collection<Class<?>> collectIndexedTypes(ClassLoader defaultClassLoader, Iterable<ClassElement> descriptors) {
|
|
|
|
|
Set<Class<?>> types = new HashSet<>();
|
|
|
|
|
for (ClassElement descriptor : descriptors) {
|
|
|
|
|
Class<?> loadedClass = load(defaultClassLoader, descriptor);
|
|
|
|
|
types.add(loadedClass);
|
|
|
|
|
}
|
|
|
|
|
return types;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-16 11:57:38 +02:00
|
|
|
private Set<WebElementExtension> collectWebElementExtensions(ClassLoader defaultClassLoader, Iterable<WebElementDescriptor> descriptors) {
|
|
|
|
|
Set<WebElementExtension> webElementExtensions = new HashSet<>();
|
|
|
|
|
for (WebElementDescriptor descriptor : descriptors) {
|
|
|
|
|
if (isRequirementFulfilled(descriptor)) {
|
2021-07-19 08:48:43 +02:00
|
|
|
Class<?> loadedClass = load(defaultClassLoader, descriptor);
|
2020-04-16 11:57:38 +02:00
|
|
|
webElementExtensions.add(new WebElementExtension(loadedClass, descriptor));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return webElementExtensions;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-19 08:48:43 +02:00
|
|
|
private Class<?> load(ClassLoader classLoader, ClassElement extension) {
|
2020-01-08 14:27:11 +01:00
|
|
|
try {
|
|
|
|
|
return classLoader.loadClass(extension.getClazz());
|
|
|
|
|
} catch (ClassNotFoundException ex) {
|
2020-01-09 08:20:47 +01:00
|
|
|
throw new PluginLoadException("failed to load clazz", ex);
|
2020-01-08 14:27:11 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-07 12:49:25 +02:00
|
|
|
private boolean isRequirementFulfilled(ClassElement extension) {
|
2020-01-08 14:27:11 +01:00
|
|
|
if (extension.getRequires() != null) {
|
|
|
|
|
for (String requiredPlugin : extension.getRequires()) {
|
|
|
|
|
if (!pluginIndex.contains(requiredPlugin)) {
|
|
|
|
|
LOG.debug("skip loading of extension {}, because the required plugin {} is not installed", extension.getClazz(), requiredPlugin);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-19 17:15:50 +01:00
|
|
|
}
|
2020-01-08 14:27:11 +01:00
|
|
|
return true;
|
2014-12-19 17:15:50 +01:00
|
|
|
}
|
|
|
|
|
|
2021-09-14 09:26:47 +02:00
|
|
|
private void collectRootElements(ClassLoader classLoader, ScmModule module) {
|
|
|
|
|
for (ExtensionPointElement epe : module.getExtensionPoints()) {
|
2014-12-19 17:15:50 +01:00
|
|
|
extensionPointIndex.put(epe.getClazz(), epe);
|
|
|
|
|
}
|
2014-12-19 17:41:46 +01:00
|
|
|
|
2020-04-07 12:49:25 +02:00
|
|
|
restProviders.addAll(collectClasses(classLoader, module.getRestProviders()));
|
|
|
|
|
restResources.addAll(collectClasses(classLoader, module.getRestResources()));
|
2021-09-14 09:26:47 +02:00
|
|
|
mappers.addAll(collectClasses(classLoader, module.getMappers()));
|
2020-04-16 11:57:38 +02:00
|
|
|
|
|
|
|
|
webElements.addAll(collectWebElementExtensions(classLoader, module.getWebElements()));
|
2021-07-19 08:48:43 +02:00
|
|
|
indexedTypes.addAll(collectIndexedTypes(classLoader, module.getIndexedTypes()));
|
2014-12-19 17:15:50 +01:00
|
|
|
}
|
|
|
|
|
}
|