Search in sources :

Example 41 with PluginDependencyResult

use of com.qcadoo.plugin.api.PluginDependencyResult in project qcadoo by qcadoo.

the class PluginDependencyManagerTest method shouldCheckDependenciesDependenciesAndReturnMultipleDependencies.

@Test
public void shouldCheckDependenciesDependenciesAndReturnMultipleDependencies() throws Exception {
    // given
    Set<PluginDependencyInformation> disabledRequiredPlugins = new HashSet<PluginDependencyInformation>();
    disabledRequiredPlugins.add(dependencyInfo2);
    given(plugin1.getRequiredPlugins()).willReturn(disabledRequiredPlugins);
    given(plugin2.getState()).willReturn(PluginState.DISABLED);
    Set<PluginDependencyInformation> disabledRequiredPlugins2 = new HashSet<PluginDependencyInformation>();
    disabledRequiredPlugins2.add(dependencyInfo3);
    given(plugin2.getRequiredPlugins()).willReturn(disabledRequiredPlugins2);
    given(plugin3.getState()).willReturn(PluginState.TEMPORARY);
    given(pluginAccessor.getPlugin("testPlugin2")).willReturn(plugin2);
    given(pluginAccessor.getPlugin("testPlugin3")).willReturn(plugin3);
    // when
    PluginDependencyResult result = manager.getDependenciesToEnable(singletonList((Plugin) plugin1), pluginStatusResolver);
    // then
    assertFalse(result.isCyclic());
    assertEquals(2, result.getDependenciesToEnable().size());
    assertTrue(result.getDependenciesToEnable().contains(new PluginDependencyInformation("testPlugin2")));
    assertTrue(result.getDependenciesToEnable().contains(new PluginDependencyInformation("testPlugin3")));
    assertEquals(0, result.getUnsatisfiedDependencies().size());
    assertEquals(0, result.getDependenciesToDisable().size());
}
Also used : PluginDependencyResult(com.qcadoo.plugin.api.PluginDependencyResult) PluginDependencyInformation(com.qcadoo.plugin.api.PluginDependencyInformation) HashSet(java.util.HashSet) Plugin(com.qcadoo.plugin.api.Plugin) InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin) Test(org.junit.Test)

Example 42 with PluginDependencyResult

use of com.qcadoo.plugin.api.PluginDependencyResult in project qcadoo by qcadoo.

the class PluginDependencyManagerTest method shouldReturnValidResultForCyclicDependencies.

@Test
public void shouldReturnValidResultForCyclicDependencies() throws Exception {
    // given
    Set<PluginDependencyInformation> disabledRequiredPlugins1 = new HashSet<PluginDependencyInformation>();
    disabledRequiredPlugins1.add(dependencyInfo2);
    given(plugin1.getRequiredPlugins()).willReturn(disabledRequiredPlugins1);
    Set<PluginDependencyInformation> disabledRequiredPlugins2 = new HashSet<PluginDependencyInformation>();
    disabledRequiredPlugins2.add(dependencyInfo3);
    disabledRequiredPlugins2.add(dependencyInfo4);
    given(plugin2.getRequiredPlugins()).willReturn(disabledRequiredPlugins2);
    Set<PluginDependencyInformation> disabledRequiredPlugins3 = new HashSet<PluginDependencyInformation>();
    disabledRequiredPlugins3.add(dependencyInfo1);
    given(plugin3.getRequiredPlugins()).willReturn(disabledRequiredPlugins3);
    given(pluginAccessor.getPlugin("testPlugin1")).willReturn(plugin1);
    given(pluginAccessor.getPlugin("testPlugin2")).willReturn(plugin2);
    given(pluginAccessor.getPlugin("testPlugin3")).willReturn(plugin3);
    given(pluginAccessor.getPlugin("testPlugin4")).willReturn(plugin4);
    List<Plugin> plugins = new ArrayList<Plugin>();
    plugins.add(plugin1);
    plugins.add(plugin2);
    plugins.add(plugin3);
    // when
    PluginDependencyResult result = manager.getDependenciesToEnable(plugins, pluginStatusResolver);
    // then
    assertTrue(result.isCyclic());
    assertEquals(0, result.getDependenciesToEnable().size());
    assertEquals(0, result.getUnsatisfiedDependencies().size());
    assertEquals(0, result.getDependenciesToDisable().size());
}
Also used : PluginDependencyResult(com.qcadoo.plugin.api.PluginDependencyResult) ArrayList(java.util.ArrayList) PluginDependencyInformation(com.qcadoo.plugin.api.PluginDependencyInformation) HashSet(java.util.HashSet) Plugin(com.qcadoo.plugin.api.Plugin) InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin) Test(org.junit.Test)

Example 43 with PluginDependencyResult

use of com.qcadoo.plugin.api.PluginDependencyResult in project qcadoo by qcadoo.

the class DefaultPluginManager method uninstallPlugin.

@Override
public PluginOperationResult uninstallPlugin(final String... keys) {
    List<Plugin> plugins = new ArrayList<Plugin>();
    for (String key : keys) {
        Plugin plugin = pluginAccessor.getPlugin(key);
        if (plugin.isSystemPlugin()) {
            return PluginOperationResultImpl.systemPluginUninstalling();
        }
        plugins.add(plugin);
    }
    PluginDependencyResult pluginDependencyResult = pluginDependencyManager.getDependenciesToUninstall(plugins, pluginStatusResolver);
    if (!pluginDependencyResult.isDependenciesSatisfied() && !pluginDependencyResult.getDependenciesToUninstall().isEmpty()) {
        return PluginOperationResultImpl.dependenciesToUninstall(pluginDependencyResult);
    }
    boolean shouldRestart = false;
    List<String> fileNames = new ArrayList<String>();
    for (Plugin plugin : plugins) {
        if (!plugin.hasState(PluginState.TEMPORARY)) {
            shouldRestart = true;
        }
        fileNames.add(plugin.getFilename());
    }
    pluginFileManager.uninstallPlugin(fileNames.toArray(new String[fileNames.size()]));
    plugins = pluginDependencyManager.sortPluginsInDependencyOrder(plugins);
    Collections.reverse(plugins);
    for (Plugin plugin : plugins) {
        if (plugin.hasState(PluginState.ENABLED)) {
            ((InternalPlugin) plugin).changeStateTo(PluginState.DISABLED);
        }
        pluginDao.delete(plugin);
        pluginAccessor.removePlugin(plugin);
    }
    if (shouldRestart) {
        return PluginOperationResultImpl.successWithRestart();
    } else {
        return PluginOperationResultImpl.success();
    }
}
Also used : InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin) PluginDependencyResult(com.qcadoo.plugin.api.PluginDependencyResult) ArrayList(java.util.ArrayList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) QcadooPluginPlugin(com.qcadoo.model.beans.qcadooPlugin.QcadooPluginPlugin) Plugin(com.qcadoo.plugin.api.Plugin) InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin)

Example 44 with PluginDependencyResult

use of com.qcadoo.plugin.api.PluginDependencyResult in project qcadoo by qcadoo.

the class DefaultPluginManager method disablePlugin.

@Override
public PluginOperationResult disablePlugin(final String... keys) {
    List<Plugin> plugins = new ArrayList<>();
    for (String key : keys) {
        Plugin plugin = pluginAccessor.getPlugin(key);
        if (plugin.isSystemPlugin()) {
            return PluginOperationResultImpl.systemPluginDisabling();
        }
        if (plugin.hasState(PluginState.ENABLED)) {
            plugins.add(plugin);
        }
    }
    if (plugins.isEmpty()) {
        return PluginOperationResultImpl.success();
    }
    PluginDependencyResult pluginDependencyResult = pluginDependencyManager.getDependenciesToDisable(plugins, pluginStatusResolver);
    if (!pluginDependencyResult.isDependenciesSatisfied() && !pluginDependencyResult.getDependenciesToDisable().isEmpty()) {
        return PluginOperationResultImpl.dependenciesToDisable(pluginDependencyResult);
    }
    plugins = pluginDependencyManager.sortPluginsInDependencyOrder(plugins);
    Collections.reverse(plugins);
    for (Plugin plugin : plugins) {
        ((InternalPlugin) plugin).changeStateTo(PluginState.DISABLED);
        pluginDao.save(plugin);
        pluginAccessor.savePlugin(plugin);
    }
    return PluginOperationResultImpl.success();
}
Also used : InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin) PluginDependencyResult(com.qcadoo.plugin.api.PluginDependencyResult) ArrayList(java.util.ArrayList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) QcadooPluginPlugin(com.qcadoo.model.beans.qcadooPlugin.QcadooPluginPlugin) Plugin(com.qcadoo.plugin.api.Plugin) InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin)

Example 45 with PluginDependencyResult

use of com.qcadoo.plugin.api.PluginDependencyResult in project qcadoo by qcadoo.

the class DefaultPluginManager method installPlugin.

@Override
public PluginOperationResult installPlugin(final PluginArtifact pluginArtifact) {
    File pluginFile = null;
    try {
        pluginFile = pluginFileManager.uploadPlugin(pluginArtifact);
    } catch (PluginException e) {
        return PluginOperationResultImpl.cannotUploadPlugin();
    }
    Plugin plugin = null;
    try {
        plugin = pluginDescriptorParser.parse(pluginFile);
    } catch (PluginException e) {
        LOG.error(e.getMessage());
        pluginFileManager.uninstallPlugin(pluginFile.getName());
        return PluginOperationResultImpl.corruptedPlugin();
    }
    if (plugin.isSystemPlugin()) {
        pluginFileManager.uninstallPlugin(plugin.getFilename());
        return PluginOperationResultImpl.systemPluginUpdating();
    }
    boolean shouldRestart = false;
    PluginDependencyResult pluginDependencyResult = pluginDependencyManager.getDependenciesToEnable(newArrayList(plugin), pluginStatusResolver);
    if (pluginDependencyResult.isCyclic()) {
        pluginFileManager.uninstallPlugin(plugin.getFilename());
        return PluginOperationResultImpl.dependenciesCyclesExists();
    }
    Plugin existingPlugin = pluginAccessor.getPlugin(plugin.getIdentifier());
    if (existingPlugin == null) {
        ((InternalPlugin) plugin).changeStateTo(PluginState.TEMPORARY);
        pluginDao.save(plugin);
        pluginAccessor.savePlugin(plugin);
        if (!pluginDependencyResult.isDependenciesSatisfied() && !pluginDependencyResult.getUnsatisfiedDependencies().isEmpty()) {
            return PluginOperationResultImpl.successWithMissingDependencies(pluginDependencyResult);
        }
        return PluginOperationResultImpl.success();
    } else {
        if (existingPlugin.getVersion().compareTo(plugin.getVersion()) >= 0) {
            pluginFileManager.uninstallPlugin(plugin.getFilename());
            return PluginOperationResultImpl.cannotDowngradePlugin();
        }
        if (existingPlugin.hasState(PluginState.TEMPORARY)) {
            if (!pluginDependencyResult.isDependenciesSatisfied() && !pluginDependencyResult.getUnsatisfiedDependencies().isEmpty()) {
                pluginFileManager.uninstallPlugin(existingPlugin.getFilename());
                ((InternalPlugin) plugin).changeStateTo(existingPlugin.getState());
                pluginDao.save(plugin);
                pluginAccessor.savePlugin(plugin);
                return PluginOperationResultImpl.successWithMissingDependencies(pluginDependencyResult);
            }
            ((InternalPlugin) plugin).changeStateTo(existingPlugin.getState());
        } else if (existingPlugin.hasState(PluginState.DISABLED)) {
            if (!pluginDependencyResult.isDependenciesSatisfied() && !pluginDependencyResult.getUnsatisfiedDependencies().isEmpty()) {
                pluginFileManager.uninstallPlugin(plugin.getFilename());
                return PluginOperationResultImpl.unsatisfiedDependencies(pluginDependencyResult);
            }
            if (!pluginFileManager.installPlugin(plugin.getFilename())) {
                pluginFileManager.uninstallPlugin(plugin.getFilename());
                return PluginOperationResultImpl.cannotInstallPlugin();
            }
            shouldRestart = true;
            ((InternalPlugin) plugin).changeStateTo(existingPlugin.getState());
        } else if (existingPlugin.hasState(PluginState.ENABLED)) {
            if (!pluginDependencyResult.isDependenciesSatisfied()) {
                if (!pluginDependencyResult.getUnsatisfiedDependencies().isEmpty()) {
                    pluginFileManager.uninstallPlugin(plugin.getFilename());
                    return PluginOperationResultImpl.unsatisfiedDependencies(pluginDependencyResult);
                }
                if (!pluginDependencyResult.getDependenciesToEnable().isEmpty()) {
                    pluginFileManager.uninstallPlugin(plugin.getFilename());
                    return PluginOperationResultImpl.dependenciesToEnable(pluginDependencyResult);
                }
            }
            if (!pluginFileManager.installPlugin(plugin.getFilename())) {
                pluginFileManager.uninstallPlugin(plugin.getFilename());
                return PluginOperationResultImpl.cannotInstallPlugin();
            }
            shouldRestart = true;
            PluginDependencyResult installPluginDependencyResult = pluginDependencyManager.getDependenciesToUpdate(existingPlugin, plugin, pluginStatusResolver);
            if (!installPluginDependencyResult.getDependenciesToDisableUnsatisfiedAfterUpdate().isEmpty()) {
                pluginFileManager.uninstallPlugin(plugin.getFilename());
                return PluginOperationResultImpl.unsatisfiedDependenciesAfterUpdate(installPluginDependencyResult);
            }
            List<Plugin> dependencyPlugins = new ArrayList<Plugin>();
            for (PluginDependencyInformation pluginDependencyInformation : installPluginDependencyResult.getDependenciesToDisable()) {
                dependencyPlugins.add(pluginAccessor.getPlugin(pluginDependencyInformation.getIdentifier()));
            }
            dependencyPlugins = pluginDependencyManager.sortPluginsInDependencyOrder(dependencyPlugins);
            Collections.reverse(dependencyPlugins);
            for (Plugin dependencyPlugin : dependencyPlugins) {
                ((InternalPlugin) dependencyPlugin).changeStateTo(PluginState.DISABLED);
            }
            ((InternalPlugin) existingPlugin).changeStateTo(PluginState.DISABLED);
            ((InternalPlugin) plugin).changeStateTo(PluginState.ENABLING);
            Collections.reverse(dependencyPlugins);
            for (Plugin dependencyPlugin : dependencyPlugins) {
                ((InternalPlugin) dependencyPlugin).changeStateTo(PluginState.ENABLING);
                pluginDao.save(dependencyPlugin);
            }
        }
        pluginFileManager.uninstallPlugin(existingPlugin.getFilename());
        pluginDao.save(plugin);
        pluginAccessor.savePlugin(plugin);
        if (shouldRestart) {
            return PluginOperationResultImpl.successWithRestart();
        } else {
            return PluginOperationResultImpl.success();
        }
    }
}
Also used : InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin) PluginDependencyResult(com.qcadoo.plugin.api.PluginDependencyResult) PluginException(com.qcadoo.plugin.internal.PluginException) ArrayList(java.util.ArrayList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) PluginDependencyInformation(com.qcadoo.plugin.api.PluginDependencyInformation) File(java.io.File) QcadooPluginPlugin(com.qcadoo.model.beans.qcadooPlugin.QcadooPluginPlugin) Plugin(com.qcadoo.plugin.api.Plugin) InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin)

Aggregations

PluginDependencyResult (com.qcadoo.plugin.api.PluginDependencyResult)46 Test (org.junit.Test)39 Plugin (com.qcadoo.plugin.api.Plugin)38 InternalPlugin (com.qcadoo.plugin.internal.api.InternalPlugin)36 PluginDependencyInformation (com.qcadoo.plugin.api.PluginDependencyInformation)28 HashSet (java.util.HashSet)23 PluginOperationResult (com.qcadoo.plugin.api.PluginOperationResult)15 SimplePluginStatusResolver (com.qcadoo.plugin.internal.dependencymanager.SimplePluginStatusResolver)15 ArrayList (java.util.ArrayList)11 VersionOfDependency (com.qcadoo.plugin.api.VersionOfDependency)7 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)4 QcadooPluginPlugin (com.qcadoo.model.beans.qcadooPlugin.QcadooPluginPlugin)4 InOrder (org.mockito.InOrder)3 Version (com.qcadoo.plugin.api.Version)2 PluginException (com.qcadoo.plugin.internal.PluginException)2 File (java.io.File)1 LinkedList (java.util.LinkedList)1 CycleDetector (org.jgrapht.alg.CycleDetector)1 DefaultDirectedGraph (org.jgrapht.graph.DefaultDirectedGraph)1 DefaultEdge (org.jgrapht.graph.DefaultEdge)1