make scm-webapp depend optional of scm-landingpage-plugin // add some events for landingpage

This commit is contained in:
Eduard Heimbuch
2020-04-01 16:01:26 +02:00
parent 1c21562c96
commit 548bf97c57
14 changed files with 741 additions and 12 deletions

View File

@@ -31,7 +31,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.NotFoundException;
import sonia.scm.event.ScmEventBus;
import sonia.scm.lifecycle.RestartEvent;
import sonia.scm.lifecycle.Restarter;
import sonia.scm.version.Version;
@@ -64,17 +63,19 @@ public class DefaultPluginManager implements PluginManager {
private final PluginCenter center;
private final PluginInstaller installer;
private final Restarter restarter;
private final ScmEventBus eventBus;
private final Collection<PendingPluginInstallation> pendingInstallQueue = new ArrayList<>();
private final Collection<PendingPluginUninstallation> pendingUninstallQueue = new ArrayList<>();
private final PluginDependencyTracker dependencyTracker = new PluginDependencyTracker();
@Inject
public DefaultPluginManager(PluginLoader loader, PluginCenter center, PluginInstaller installer, Restarter restarter) {
public DefaultPluginManager(PluginLoader loader, PluginCenter center, PluginInstaller installer, Restarter restarter, ScmEventBus eventBus) {
this.loader = loader;
this.center = center;
this.installer = installer;
this.restarter = restarter;
this.eventBus = eventBus;
this.computeInstallationDependencies();
}
@@ -172,8 +173,10 @@ public class DefaultPluginManager implements PluginManager {
PendingPluginInstallation pending = installer.install(plugin);
dependencyTracker.addInstalled(plugin.getDescriptor());
pendingInstallations.add(pending);
eventBus.post(new PluginEvent(PluginEventType.INSTALLED, plugin));
} catch (PluginInstallException ex) {
cancelPending(pendingInstallations);
eventBus.post(new PluginEvent(PluginEventType.INSTALLATION_FAILED, plugin));
throw ex;
}
}

View File

@@ -27,10 +27,11 @@ package sonia.scm.plugin;
import com.google.common.annotations.VisibleForTesting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.event.Event;
import sonia.scm.event.ScmEventBus;
import sonia.scm.net.ahc.AdvancedHttpClient;
import javax.inject.Inject;
import java.io.IOException;
import java.util.Collections;
import java.util.Set;
@@ -40,16 +41,18 @@ class PluginCenterLoader {
private final AdvancedHttpClient client;
private final PluginCenterDtoMapper mapper;
private final ScmEventBus eventBus;
@Inject
public PluginCenterLoader(AdvancedHttpClient client) {
this(client, PluginCenterDtoMapper.INSTANCE);
public PluginCenterLoader(AdvancedHttpClient client, ScmEventBus eventBus) {
this(client, PluginCenterDtoMapper.INSTANCE, eventBus);
}
@VisibleForTesting
PluginCenterLoader(AdvancedHttpClient client, PluginCenterDtoMapper mapper) {
PluginCenterLoader(AdvancedHttpClient client, PluginCenterDtoMapper mapper, ScmEventBus eventBus) {
this.client = client;
this.mapper = mapper;
this.eventBus = eventBus;
}
Set<AvailablePlugin> load(String url) {
@@ -59,8 +62,12 @@ class PluginCenterLoader {
return mapper.map(pluginCenterDto);
} catch (Exception ex) {
LOG.error("failed to load plugins from plugin center, returning empty list", ex);
eventBus.post(new PluginCenterNotAvailableEvent());
return Collections.emptySet();
}
}
@Event
class PluginCenterNotAvailableEvent {}
}

View File

@@ -0,0 +1,39 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* 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:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* 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.
*/
package sonia.scm.plugin;
import lombok.Getter;
import sonia.scm.event.Event;
@Getter
@Event
public class PluginEvent {
private final PluginEventType eventType;
private final AvailablePlugin plugin;
public PluginEvent(PluginEventType eventType, AvailablePlugin plugin) {
this.eventType = eventType;
this.plugin = plugin;
}
}

View File

@@ -0,0 +1,29 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* 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:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* 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.
*/
package sonia.scm.plugin;
enum PluginEventType {
INSTALLED, INSTALLATION_FAILED
}

View File

@@ -0,0 +1,86 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* 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:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* 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.
*/
package sonia.scm.plugin;
import com.cloudogu.scm.myevents.MyEvent;
import com.github.legman.Subscribe;
import lombok.Getter;
import lombok.NoArgsConstructor;
import sonia.scm.EagerSingleton;
import sonia.scm.event.ScmEventBus;
import sonia.scm.xml.XmlInstantAdapter;
import javax.inject.Inject;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.time.Instant;
@Extension(requires = "scm-landingpage-plugin")
@EagerSingleton
public class PluginInstallationFailedEventSubscriber {
private final ScmEventBus eventBus;
@Inject
public PluginInstallationFailedEventSubscriber(ScmEventBus eventBus) {
this.eventBus = eventBus;
}
@Subscribe
public void handleEvent(PluginEvent pluginEvent) {
if (pluginEvent.getEventType() == PluginEventType.INSTALLATION_FAILED) {
AvailablePlugin newPlugin = pluginEvent.getPlugin();
String permission = PluginPermissions.manage().asShiroString();
String pluginName = newPlugin.getDescriptor().getInformation().getDisplayName();
String pluginVersion = newPlugin.getDescriptor().getInformation().getVersion();
eventBus.post(new PluginInstallationFailedEvent(permission, pluginName, pluginVersion, Instant.now()));
}
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@Getter
@NoArgsConstructor
static class PluginInstallationFailedEvent extends MyEvent {
private String pluginName;
private String pluginVersion;
@XmlJavaTypeAdapter(XmlInstantAdapter.class)
private Instant date;
PluginInstallationFailedEvent(String permission, String pluginName, String pluginVersion, Instant date) {
super(PluginInstalledEventSubscriber.PluginInstalledEvent.class.getSimpleName(), permission);
this.pluginName = pluginName;
this.pluginVersion = pluginVersion;
this.date = date;
}
}
}

View File

@@ -0,0 +1,98 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* 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:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* 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.
*/
package sonia.scm.plugin;
import com.cloudogu.scm.myevents.MyEvent;
import com.github.legman.Subscribe;
import lombok.Getter;
import lombok.NoArgsConstructor;
import sonia.scm.EagerSingleton;
import sonia.scm.event.ScmEventBus;
import sonia.scm.xml.XmlInstantAdapter;
import javax.inject.Inject;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.time.Instant;
import java.util.Optional;
@Extension(requires = "scm-landingpage-plugin")
@EagerSingleton
public class PluginInstalledEventSubscriber {
private final ScmEventBus eventBus;
private final PluginManager pluginManager;
@Inject
public PluginInstalledEventSubscriber(ScmEventBus eventBus, PluginManager pluginManager) {
this.eventBus = eventBus;
this.pluginManager = pluginManager;
}
@Subscribe
public void handleEvent(PluginEvent pluginEvent) {
if (pluginEvent.getEventType() == PluginEventType.INSTALLED) {
AvailablePlugin newPlugin = pluginEvent.getPlugin();
Optional<InstalledPlugin> installedPlugin = pluginManager.getInstalled(newPlugin.getDescriptor().getInformation().getDisplayName());
String permission = PluginPermissions.manage().asShiroString();
String pluginName = newPlugin.getDescriptor().getInformation().getName();
String previousPluginVersion = null;
if (installedPlugin.isPresent()) {
previousPluginVersion = installedPlugin.get().getDescriptor().getInformation().getVersion();
}
String newPluginVersion = newPlugin.getDescriptor().getInformation().getVersion();
eventBus.post(new PluginInstalledEvent(permission, pluginName, previousPluginVersion, newPluginVersion, Instant.now()));
}
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@Getter
@NoArgsConstructor
static class PluginInstalledEvent extends MyEvent {
private String pluginName;
private String previousPluginVersion;
private String newPluginVersion;
@XmlJavaTypeAdapter(XmlInstantAdapter.class)
private Instant date;
PluginInstalledEvent(String permission, String pluginName, String previousPluginVersion, String newPluginVersion, Instant date) {
super(PluginInstalledEventSubscriber.PluginInstalledEvent.class.getSimpleName(), permission);
this.pluginName = pluginName;
this.previousPluginVersion = previousPluginVersion;
this.newPluginVersion = newPluginVersion;
this.date = date;
}
}
}

View File

@@ -0,0 +1,88 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* 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:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* 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.
*/
package sonia.scm.repository;
import com.cloudogu.scm.myevents.MyEvent;
import com.github.legman.Subscribe;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.apache.shiro.SecurityUtils;
import sonia.scm.EagerSingleton;
import sonia.scm.HandlerEventType;
import sonia.scm.event.ScmEventBus;
import sonia.scm.plugin.Extension;
import sonia.scm.user.User;
import sonia.scm.xml.XmlInstantAdapter;
import javax.inject.Inject;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.time.Instant;
@Extension(requires = "scm-landingpage-plugin")
@EagerSingleton
public class RepositoryCreatedEventSubscriber {
private final ScmEventBus eventBus;
@Inject
public RepositoryCreatedEventSubscriber(ScmEventBus eventBus) {
this.eventBus = eventBus;
}
@Subscribe
public void handleEvent(RepositoryEvent event) {
if (event.getEventType() == HandlerEventType.CREATE) {
Repository eventRepo = event.getItem();
String permission = RepositoryPermissions.read(event.getItem()).asShiroString();
String repository = eventRepo.getNamespace() + "/" + eventRepo.getName();
String creator = SecurityUtils.getSubject().getPrincipals().oneByType(User.class).getDisplayName();
Instant date = Instant.now();
eventBus.post(new RepositoryCreatedEvent(permission, repository, creator, date));
}
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@Getter
@NoArgsConstructor
static class RepositoryCreatedEvent extends MyEvent {
private String repository;
private String creator;
@XmlJavaTypeAdapter(XmlInstantAdapter.class)
private Instant date;
RepositoryCreatedEvent(String permission, String repository, String creator, Instant date) {
super(RepositoryCreatedEvent.class.getSimpleName(), permission);
this.repository = repository;
this.creator = creator;
this.date = date;
}
}
}