mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-07 22:15:45 +01:00
implement simplified PluginManager API
This commit is contained in:
@@ -35,11 +35,15 @@ package sonia.scm.plugin;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
import javax.inject.Inject;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -48,24 +52,48 @@ import java.util.Optional;
|
||||
@Singleton
|
||||
public class DefaultPluginManager implements PluginManager {
|
||||
|
||||
private final PluginLoader loader;
|
||||
private final PluginCenter center;
|
||||
|
||||
@Inject
|
||||
public DefaultPluginManager(PluginLoader loader, PluginCenter center) {
|
||||
this.loader = loader;
|
||||
this.center = center;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<AvailablePlugin> getAvailable(String name) {
|
||||
return Optional.empty();
|
||||
return center.getAvailable()
|
||||
.stream()
|
||||
.filter(filterByName(name))
|
||||
.filter(this::isNotInstalled)
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<InstalledPlugin> getInstalled(String name) {
|
||||
return Optional.empty();
|
||||
return loader.getInstalledPlugins()
|
||||
.stream()
|
||||
.filter(filterByName(name))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<InstalledPlugin> getInstalled() {
|
||||
return null;
|
||||
return ImmutableList.copyOf(loader.getInstalledPlugins());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AvailablePlugin> getAvailable() {
|
||||
return null;
|
||||
return center.getAvailable().stream().filter(this::isNotInstalled).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private <T extends Plugin> Predicate<T> filterByName(String name) {
|
||||
return (plugin) -> name.equals(plugin.getDescriptor().getInformation().getName());
|
||||
}
|
||||
|
||||
private boolean isNotInstalled(AvailablePlugin availablePlugin) {
|
||||
return !getInstalled(availablePlugin.getDescriptor().getInformation().getName()).isPresent();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
55
scm-webapp/src/main/java/sonia/scm/plugin/PluginCenter.java
Normal file
55
scm-webapp/src/main/java/sonia/scm/plugin/PluginCenter.java
Normal file
@@ -0,0 +1,55 @@
|
||||
package sonia.scm.plugin;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.SCMContextProvider;
|
||||
import sonia.scm.cache.Cache;
|
||||
import sonia.scm.cache.CacheManager;
|
||||
import sonia.scm.config.ScmConfiguration;
|
||||
import sonia.scm.util.HttpUtil;
|
||||
import sonia.scm.util.SystemUtil;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.Set;
|
||||
|
||||
public class PluginCenter {
|
||||
|
||||
private static final String CACHE_NAME = "sonia.cache.plugins";
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(PluginCenter.class);
|
||||
|
||||
private final SCMContextProvider context;
|
||||
private final ScmConfiguration configuration;
|
||||
private final PluginCenterLoader loader;
|
||||
private final Cache<String, Set<AvailablePlugin>> cache;
|
||||
|
||||
@Inject
|
||||
public PluginCenter(SCMContextProvider context, CacheManager cacheManager, ScmConfiguration configuration, PluginCenterLoader loader) {
|
||||
this.context = context;
|
||||
this.configuration = configuration;
|
||||
this.loader = loader;
|
||||
this.cache = cacheManager.getCache(CACHE_NAME);
|
||||
}
|
||||
|
||||
synchronized Set<AvailablePlugin> getAvailable() {
|
||||
String url = buildPluginUrl(configuration.getPluginUrl());
|
||||
Set<AvailablePlugin> plugins = cache.get(url);
|
||||
if (plugins == null) {
|
||||
LOG.debug("no cached available plugins found, start fetching");
|
||||
plugins = loader.load(url);
|
||||
cache.put(url, plugins);
|
||||
} else {
|
||||
LOG.debug("return available plugins from cache");
|
||||
}
|
||||
return plugins;
|
||||
}
|
||||
|
||||
private String buildPluginUrl(String url) {
|
||||
String os = HttpUtil.encode(SystemUtil.getOS());
|
||||
String arch = SystemUtil.getArch();
|
||||
return url.replace("{version}", context.getVersion())
|
||||
.replace("{os}", os)
|
||||
.replace("{arch}", arch);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,26 +1,26 @@
|
||||
package sonia.scm.plugin;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@Mapper
|
||||
public interface PluginCenterDtoMapper {
|
||||
public abstract class PluginCenterDtoMapper {
|
||||
|
||||
@Mapping(source = "conditions", target = "condition")
|
||||
PluginInformation map(PluginCenterDto.Plugin plugin);
|
||||
static final PluginCenterDtoMapper INSTANCE = Mappers.getMapper(PluginCenterDtoMapper.class);
|
||||
|
||||
PluginCondition map(PluginCenterDto.Condition condition);
|
||||
abstract PluginInformation map(PluginCenterDto.Plugin plugin);
|
||||
abstract PluginCondition map(PluginCenterDto.Condition condition);
|
||||
|
||||
static Set<PluginInformation> map(List<PluginCenterDto.Plugin> dtos) {
|
||||
PluginCenterDtoMapper mapper = Mappers.getMapper(PluginCenterDtoMapper.class);
|
||||
Set<PluginInformation> plugins = new HashSet<>();
|
||||
for (PluginCenterDto.Plugin plugin : dtos) {
|
||||
plugins.add(mapper.map(plugin));
|
||||
Set<AvailablePlugin> map(PluginCenterDto pluginCenterDto) {
|
||||
Set<AvailablePlugin> plugins = new HashSet<>();
|
||||
for (PluginCenterDto.Plugin plugin : pluginCenterDto.getEmbedded().getPlugins()) {
|
||||
AvailablePluginDescriptor descriptor = new AvailablePluginDescriptor(
|
||||
map(plugin), map(plugin.getConditions()), plugin.getDependencies()
|
||||
);
|
||||
plugins.add(new AvailablePlugin(descriptor));
|
||||
}
|
||||
return plugins;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package sonia.scm.plugin;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.net.ahc.AdvancedHttpClient;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
class PluginCenterLoader {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(PluginCenterLoader.class);
|
||||
|
||||
private final AdvancedHttpClient client;
|
||||
private final PluginCenterDtoMapper mapper;
|
||||
|
||||
@Inject
|
||||
public PluginCenterLoader(AdvancedHttpClient client) {
|
||||
this(client, PluginCenterDtoMapper.INSTANCE);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
PluginCenterLoader(AdvancedHttpClient client, PluginCenterDtoMapper mapper) {
|
||||
this.client = client;
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
Set<AvailablePlugin> load(String url) {
|
||||
try {
|
||||
LOG.info("fetch plugins from {}", url);
|
||||
PluginCenterDto pluginCenterDto = client.get(url).request().contentFromJson(PluginCenterDto.class);
|
||||
return mapper.map(pluginCenterDto);
|
||||
} catch (IOException ex) {
|
||||
LOG.error("failed to load plugins from plugin center, returning empty list");
|
||||
return Collections.emptySet();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user