|
|
|
@ -48,67 +48,131 @@ public abstract class AbstractExtensionFinder implements ExtensionFinder, Plugin
|
|
|
|
|
public <T> List<ExtensionWrapper<T>> find(Class<T> type) { |
|
|
|
|
log.debug("Finding extensions of extension point '{}'", type.getName()); |
|
|
|
|
Map<String, Set<String>> entries = getEntries(); |
|
|
|
|
List<ExtensionWrapper<T>> result = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
// add extensions found in classpath
|
|
|
|
|
List<ExtensionWrapper<T>> classpathExtensions = find(type, null); |
|
|
|
|
result.addAll(classpathExtensions); |
|
|
|
|
|
|
|
|
|
// add extensions found in each plugin
|
|
|
|
|
for (String pluginId : entries.keySet()) { |
|
|
|
|
List<ExtensionWrapper<T>> pluginExtensions = find(type, pluginId); |
|
|
|
|
result.addAll(pluginExtensions); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (entries.isEmpty()) { |
|
|
|
|
log.debug("No extensions found for extension point '{}'", type.getName()); |
|
|
|
|
} else { |
|
|
|
|
log.debug("Found {} extensions for extension point '{}'", result.size(), type.getName()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// sort by "ordinal" property
|
|
|
|
|
Collections.sort(result); |
|
|
|
|
|
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
public <T> List<ExtensionWrapper<T>> find(Class<T> type, String pluginId) { |
|
|
|
|
log.debug("Finding extensions of extension point '{}' for plugin '{}'", type.getName(), pluginId); |
|
|
|
|
List<ExtensionWrapper<T>> result = new ArrayList<>(); |
|
|
|
|
for (Map.Entry<String, Set<String>> entry : entries.entrySet()) { |
|
|
|
|
if (entry.getValue().isEmpty()) { |
|
|
|
|
continue; |
|
|
|
|
|
|
|
|
|
Set<String> classNames = findClassNames(pluginId); |
|
|
|
|
if (classNames.isEmpty()) { |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (pluginId != null) { |
|
|
|
|
PluginWrapper pluginWrapper = pluginManager.getPlugin(pluginId); |
|
|
|
|
if (PluginState.STARTED != pluginWrapper.getPluginState()) { |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
String pluginId = entry.getKey(); |
|
|
|
|
log.trace("Checking extensions from plugin '{}'", pluginId); |
|
|
|
|
} else { |
|
|
|
|
log.trace("Checking extensions from classpath"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ClassLoader classLoader = (pluginId != null) ? pluginManager.getPluginClassLoader(pluginId) : getClass().getClassLoader(); |
|
|
|
|
|
|
|
|
|
for (String className : classNames) { |
|
|
|
|
try { |
|
|
|
|
log.debug("Loading class '{}' using class loader '{}'", className, classLoader); |
|
|
|
|
Class<?> extensionClass = classLoader.loadClass(className); |
|
|
|
|
|
|
|
|
|
if (pluginId != null) { |
|
|
|
|
PluginWrapper pluginWrapper = pluginManager.getPlugin(pluginId); |
|
|
|
|
if (PluginState.STARTED != pluginWrapper.getPluginState()) { |
|
|
|
|
continue; |
|
|
|
|
log.debug("Checking extension type '{}'", className); |
|
|
|
|
if (type.isAssignableFrom(extensionClass)) { |
|
|
|
|
ExtensionWrapper extensionWrapper = createExtensionWrapper(extensionClass); |
|
|
|
|
result.add(extensionWrapper); |
|
|
|
|
log.debug("Added extension '{}' with ordinal {}", className, extensionWrapper.getOrdinal()); |
|
|
|
|
} else { |
|
|
|
|
log.trace("'{}' is not an extension for extension point '{}'", className, type.getName()); |
|
|
|
|
} |
|
|
|
|
} catch (ClassNotFoundException e) { |
|
|
|
|
log.error(e.getMessage(), e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
log.trace("Checking extensions from plugin '{}'", pluginId); |
|
|
|
|
} else { |
|
|
|
|
log.trace("Checking extensions from classpath"); |
|
|
|
|
if (result.isEmpty()) { |
|
|
|
|
log.debug("No extensions found for extension point '{}'", type.getName()); |
|
|
|
|
} else { |
|
|
|
|
log.debug("Found {} extensions for extension point '{}'", result.size(), type.getName()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// sort by "ordinal" property
|
|
|
|
|
Collections.sort(result); |
|
|
|
|
|
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public List<ExtensionWrapper> find(String pluginId) { |
|
|
|
|
log.debug("Finding extensions from plugin '{}'", pluginId); |
|
|
|
|
List<ExtensionWrapper> result = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
Set<String> classNames = findClassNames(pluginId); |
|
|
|
|
if (classNames.isEmpty()) { |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (pluginId != null) { |
|
|
|
|
PluginWrapper pluginWrapper = pluginManager.getPlugin(pluginId); |
|
|
|
|
if (PluginState.STARTED != pluginWrapper.getPluginState()) { |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ClassLoader classLoader = (pluginId != null) ? pluginManager.getPluginClassLoader(pluginId) : getClass().getClassLoader(); |
|
|
|
|
|
|
|
|
|
for (String className : entry.getValue()) { |
|
|
|
|
try { |
|
|
|
|
log.debug("Loading class '{}' using class loader '{}'", className, classLoader); |
|
|
|
|
Class<?> extensionClass = classLoader.loadClass(className); |
|
|
|
|
|
|
|
|
|
log.debug("Checking extension type '{}'", className); |
|
|
|
|
if (type.isAssignableFrom(extensionClass)) { |
|
|
|
|
ExtensionDescriptor descriptor = new ExtensionDescriptor(); |
|
|
|
|
int ordinal = 0; |
|
|
|
|
if (extensionClass.isAnnotationPresent(Extension.class)) { |
|
|
|
|
ordinal = extensionClass.getAnnotation(Extension.class).ordinal(); |
|
|
|
|
} |
|
|
|
|
descriptor.setOrdinal(ordinal); |
|
|
|
|
descriptor.setExtensionClass(extensionClass); |
|
|
|
|
|
|
|
|
|
ExtensionWrapper extensionWrapper = new ExtensionWrapper<>(descriptor); |
|
|
|
|
extensionWrapper.setExtensionFactory(pluginManager.getExtensionFactory()); |
|
|
|
|
result.add(extensionWrapper); |
|
|
|
|
log.debug("Added extension '{}' with ordinal {}", className, ordinal); |
|
|
|
|
} else { |
|
|
|
|
log.trace("'{}' is not an extension for extension point '{}'", className, type.getName()); |
|
|
|
|
} |
|
|
|
|
} catch (ClassNotFoundException e) { |
|
|
|
|
log.error(e.getMessage(), e); |
|
|
|
|
} |
|
|
|
|
log.trace("Checking extensions from plugin '{}'", pluginId); |
|
|
|
|
} else { |
|
|
|
|
log.trace("Checking extensions from classpath"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ClassLoader classLoader = (pluginId != null) ? pluginManager.getPluginClassLoader(pluginId) : getClass().getClassLoader(); |
|
|
|
|
|
|
|
|
|
for (String className : classNames) { |
|
|
|
|
try { |
|
|
|
|
log.debug("Loading class '{}' using class loader '{}'", className, classLoader); |
|
|
|
|
Class<?> extensionClass = classLoader.loadClass(className); |
|
|
|
|
|
|
|
|
|
ExtensionWrapper extensionWrapper = createExtensionWrapper(extensionClass); |
|
|
|
|
result.add(extensionWrapper); |
|
|
|
|
log.debug("Added extension '{}' with ordinal {}", className, extensionWrapper.getOrdinal()); |
|
|
|
|
} catch (ClassNotFoundException e) { |
|
|
|
|
log.error(e.getMessage(), e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (entries.isEmpty()) { |
|
|
|
|
log.debug("No extensions found for extension point '{}'", type.getName()); |
|
|
|
|
if (result.isEmpty()) { |
|
|
|
|
log.debug("No extensions found for plugin '{}'", pluginId); |
|
|
|
|
} else { |
|
|
|
|
log.debug("Found {} extensions for extension point '{}'", result.size(), type.getName()); |
|
|
|
|
log.debug("Found {} extensions for plugin '{}'", result.size(), pluginId); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// sort by "ordinal" property
|
|
|
|
|
Collections.sort(result); |
|
|
|
|
|
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Set<String> findClassNames(String pluginId) { |
|
|
|
@ -122,6 +186,19 @@ public abstract class AbstractExtensionFinder implements ExtensionFinder, Plugin
|
|
|
|
|
entries = null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected void debugExtensions(Set<String> extensions) { |
|
|
|
|
if (log.isDebugEnabled()) { |
|
|
|
|
if (extensions.isEmpty()) { |
|
|
|
|
log.debug("No extensions found"); |
|
|
|
|
} else { |
|
|
|
|
log.debug("Found possible {} extensions:", extensions.size()); |
|
|
|
|
for (String extension : extensions) { |
|
|
|
|
log.debug(" " + extension); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Map<String, Set<String>> readStorages() { |
|
|
|
|
Map<String, Set<String>> result = new LinkedHashMap<>(); |
|
|
|
|
|
|
|
|
@ -139,4 +216,19 @@ public abstract class AbstractExtensionFinder implements ExtensionFinder, Plugin
|
|
|
|
|
return entries; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private ExtensionWrapper createExtensionWrapper(Class<?> extensionClass) { |
|
|
|
|
ExtensionDescriptor descriptor = new ExtensionDescriptor(); |
|
|
|
|
int ordinal = 0; |
|
|
|
|
if (extensionClass.isAnnotationPresent(Extension.class)) { |
|
|
|
|
ordinal = extensionClass.getAnnotation(Extension.class).ordinal(); |
|
|
|
|
} |
|
|
|
|
descriptor.setOrdinal(ordinal); |
|
|
|
|
descriptor.setExtensionClass(extensionClass); |
|
|
|
|
|
|
|
|
|
ExtensionWrapper extensionWrapper = new ExtensionWrapper<>(descriptor); |
|
|
|
|
extensionWrapper.setExtensionFactory(pluginManager.getExtensionFactory()); |
|
|
|
|
|
|
|
|
|
return extensionWrapper; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|