Search in sources :

Example 16 with InternalPlugin

use of com.qcadoo.plugin.internal.api.InternalPlugin in project qcadoo by qcadoo.

the class DefaultPluginManager method enablePlugin.

@Override
public PluginOperationResult enablePlugin(final String... keys) {
    List<Plugin> plugins = new ArrayList<Plugin>();
    for (String key : keys) {
        Plugin plugin = pluginAccessor.getPlugin(key);
        if (!plugin.hasState(PluginState.ENABLED)) {
            plugins.add(plugin);
        }
    }
    if (plugins.isEmpty()) {
        return PluginOperationResultImpl.success();
    }
    PluginDependencyResult pluginDependencyResult = pluginDependencyManager.getDependenciesToEnable(plugins, pluginStatusResolver);
    if (pluginDependencyResult.isCyclic()) {
        return PluginOperationResultImpl.dependenciesCyclesExists();
    }
    if (!pluginDependencyResult.isDependenciesSatisfied()) {
        if (!pluginDependencyResult.getUnsatisfiedDependencies().isEmpty()) {
            return PluginOperationResultImpl.unsatisfiedDependencies(pluginDependencyResult);
        }
        if (!pluginDependencyResult.getDependenciesToEnable().isEmpty()) {
            return PluginOperationResultImpl.dependenciesToEnable(pluginDependencyResult);
        }
    }
    boolean shouldRestart = false;
    List<String> fileNames = new ArrayList<String>();
    for (Plugin plugin : plugins) {
        if (plugin.hasState(PluginState.TEMPORARY)) {
            fileNames.add(plugin.getFilename());
        }
    }
    if (!fileNames.isEmpty()) {
        if (!pluginFileManager.installPlugin(fileNames.toArray(new String[fileNames.size()]))) {
            return PluginOperationResultImpl.cannotInstallPlugin();
        }
        shouldRestart = true;
    }
    plugins = pluginDependencyManager.sortPluginsInDependencyOrder(plugins);
    for (Plugin plugin : plugins) {
        if (plugin.hasState(PluginState.TEMPORARY)) {
            ((InternalPlugin) plugin).changeStateTo(PluginState.ENABLING);
        } else {
            try {
                ((InternalPlugin) plugin).changeStateTo(PluginState.ENABLED);
            } catch (Exception e) {
                LOG.error(e.getMessage());
                ((InternalPlugin) plugin).changeStateTo(PluginState.DISABLED);
                return PluginOperationResultImpl.pluginEnablingEncounteredErrors();
            }
        }
        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) ArrayList(java.util.ArrayList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) PluginException(com.qcadoo.plugin.internal.PluginException) QcadooPluginPlugin(com.qcadoo.model.beans.qcadooPlugin.QcadooPluginPlugin) Plugin(com.qcadoo.plugin.api.Plugin) InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin)

Example 17 with InternalPlugin

use of com.qcadoo.plugin.internal.api.InternalPlugin in project qcadoo by qcadoo.

the class DefaultPluginDescriptorParser method loadPlugins.

@Override
public Set<InternalPlugin> loadPlugins() {
    Map<String, InternalPlugin> loadedplugins = new HashMap<String, InternalPlugin>();
    Resource[] resources = pluginDescriptorResolver.getDescriptors();
    for (Resource resource : resources) {
        InternalPlugin plugin = parse(resource);
        if (loadedplugins.containsKey(plugin.getIdentifier())) {
            throw new PluginException("Duplicated plugin identifier: " + plugin.getIdentifier());
        }
        loadedplugins.put(plugin.getIdentifier(), plugin);
    }
    return new HashSet<InternalPlugin>(loadedplugins.values());
}
Also used : InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin) PluginException(com.qcadoo.plugin.internal.PluginException) Resource(org.springframework.core.io.Resource)

Example 18 with InternalPlugin

use of com.qcadoo.plugin.internal.api.InternalPlugin in project qcadoo by qcadoo.

the class PluginDependencyManagerTest method shouldReturnUnsatisfiedDependenciesForOnePluginWhenNoDependencyPluginFoundOrVersionIsNotMet.

@Test
public void shouldReturnUnsatisfiedDependenciesForOnePluginWhenNoDependencyPluginFoundOrVersionIsNotMet() throws Exception {
    // given
    given(plugin1.getState()).willReturn(PluginState.DISABLED);
    given(plugin1.getIdentifier()).willReturn("testPlugin1");
    given(plugin2.getState()).willReturn(PluginState.TEMPORARY);
    given(plugin2.getIdentifier()).willReturn("testPlugin2");
    given(plugin3.getState()).willReturn(PluginState.TEMPORARY);
    given(plugin3.getIdentifier()).willReturn("testPlugin3");
    given(pluginAccessor.getPlugin("testPlugin1")).willReturn(plugin1);
    given(pluginAccessor.getPlugin("testPlugin2")).willReturn(plugin2);
    given(pluginAccessor.getPlugin("testPlugin3")).willReturn(plugin3);
    given(pluginAccessor.getPlugin("testPlugin4")).willReturn(null);
    dependencyInfo3 = new PluginDependencyInformation("testPlugin3", new VersionOfDependency("[2"));
    InternalPlugin pluginToEnable = mock(InternalPlugin.class);
    Set<PluginDependencyInformation> disabledRequiredPlugins = new HashSet<PluginDependencyInformation>();
    disabledRequiredPlugins.add(dependencyInfo1);
    disabledRequiredPlugins.add(dependencyInfo2);
    disabledRequiredPlugins.add(dependencyInfo3);
    disabledRequiredPlugins.add(dependencyInfo4);
    given(pluginToEnable.getRequiredPlugins()).willReturn(disabledRequiredPlugins);
    given(pluginToEnable.getIdentifier()).willReturn("plugin");
    // when
    PluginDependencyResult result = manager.getDependenciesToEnable(singletonList((Plugin) pluginToEnable), pluginStatusResolver);
    // then
    assertFalse(result.isCyclic());
    assertEquals(0, result.getDependenciesToEnable().size());
    assertEquals(2, result.getUnsatisfiedDependencies().size());
    assertTrue(result.getUnsatisfiedDependencies().contains(new PluginDependencyInformation("testPlugin3", new VersionOfDependency("[2"))));
    assertTrue(result.getUnsatisfiedDependencies().contains(new PluginDependencyInformation("testPlugin4")));
    assertEquals(0, result.getDependenciesToDisable().size());
}
Also used : InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin) PluginDependencyResult(com.qcadoo.plugin.api.PluginDependencyResult) VersionOfDependency(com.qcadoo.plugin.api.VersionOfDependency) 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 19 with InternalPlugin

use of com.qcadoo.plugin.internal.api.InternalPlugin in project qcadoo by qcadoo.

the class PluginDependencyManagerTest method shouldReturnMultipleDisabledDependencyForOnePlugin.

@Test
public void shouldReturnMultipleDisabledDependencyForOnePlugin() throws Exception {
    // given
    given(plugin1.getState()).willReturn(PluginState.DISABLED);
    given(plugin1.getIdentifier()).willReturn("testPlugin1");
    given(plugin2.getState()).willReturn(PluginState.TEMPORARY);
    given(plugin2.getIdentifier()).willReturn("testPlugin2");
    given(plugin3.getState()).willReturn(PluginState.ENABLED);
    given(plugin3.getIdentifier()).willReturn("testPlugin3");
    given(pluginAccessor.getPlugin("testPlugin1")).willReturn(plugin1);
    given(pluginAccessor.getPlugin("testPlugin2")).willReturn(plugin2);
    given(pluginAccessor.getPlugin("testPlugin3")).willReturn(plugin3);
    InternalPlugin pluginToEnable = mock(InternalPlugin.class);
    Set<PluginDependencyInformation> disabledRequiredPlugins = new HashSet<PluginDependencyInformation>();
    disabledRequiredPlugins.add(dependencyInfo1);
    disabledRequiredPlugins.add(dependencyInfo2);
    disabledRequiredPlugins.add(dependencyInfo3);
    given(pluginToEnable.getRequiredPlugins()).willReturn(disabledRequiredPlugins);
    given(pluginToEnable.getIdentifier()).willReturn("plugin");
    // when
    PluginDependencyResult result = manager.getDependenciesToEnable(singletonList((Plugin) pluginToEnable), pluginStatusResolver);
    // then
    assertFalse(result.isCyclic());
    assertEquals(2, result.getDependenciesToEnable().size());
    assertTrue(result.getDependenciesToEnable().contains(new PluginDependencyInformation("testPlugin1")));
    assertTrue(result.getDependenciesToEnable().contains(new PluginDependencyInformation("testPlugin2")));
    assertEquals(0, result.getUnsatisfiedDependencies().size());
    assertEquals(0, result.getDependenciesToDisable().size());
}
Also used : InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin) 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 20 with InternalPlugin

use of com.qcadoo.plugin.internal.api.InternalPlugin in project qcadoo by qcadoo.

the class PluginManagerTest method shouldDisableMultipleEnabledPlugin.

@Test
public void shouldDisableMultipleEnabledPlugin() throws Exception {
    // given
    given(plugin.hasState(PluginState.ENABLED)).willReturn(true);
    InternalPlugin nextPlugin = mock(InternalPlugin.class, "nextPlugin");
    given(nextPlugin.hasState(PluginState.ENABLED)).willReturn(true);
    given(pluginAccessor.getPlugin("nextPluginname")).willReturn(nextPlugin);
    PluginDependencyResult pluginDependencyResult = PluginDependencyResultImpl.satisfiedDependencies();
    given(pluginDependencyManager.getDependenciesToDisable(Mockito.eq(newArrayList((Plugin) plugin, (Plugin) nextPlugin)), Mockito.any(SimplePluginStatusResolver.class))).willReturn(pluginDependencyResult);
    given(pluginDependencyManager.sortPluginsInDependencyOrder(newArrayList((Plugin) plugin, (Plugin) nextPlugin))).willReturn(newArrayList((Plugin) plugin, (Plugin) nextPlugin));
    // when
    PluginOperationResult pluginOperationResult = pluginManager.disablePlugin("pluginname", "nextPluginname");
    // then
    InOrder inOrder = inOrder(nextPlugin, plugin);
    inOrder.verify(nextPlugin).changeStateTo(PluginState.DISABLED);
    inOrder.verify(plugin).changeStateTo(PluginState.DISABLED);
    verify(pluginDao).save(nextPlugin);
    verify(pluginAccessor).savePlugin(nextPlugin);
    verify(pluginDao).save(plugin);
    verify(pluginAccessor).savePlugin(plugin);
    assertTrue(pluginOperationResult.isSuccess());
    assertEquals(PluginOperationStatus.SUCCESS, pluginOperationResult.getStatus());
    assertEquals(0, pluginOperationResult.getPluginDependencyResult().getDependenciesToDisable().size());
}
Also used : InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin) PluginDependencyResult(com.qcadoo.plugin.api.PluginDependencyResult) InOrder(org.mockito.InOrder) PluginOperationResult(com.qcadoo.plugin.api.PluginOperationResult) SimplePluginStatusResolver(com.qcadoo.plugin.internal.dependencymanager.SimplePluginStatusResolver) Plugin(com.qcadoo.plugin.api.Plugin) InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin) Test(org.junit.Test)

Aggregations

InternalPlugin (com.qcadoo.plugin.internal.api.InternalPlugin)31 Plugin (com.qcadoo.plugin.api.Plugin)21 Test (org.junit.Test)18 QcadooPluginPlugin (com.qcadoo.model.beans.qcadooPlugin.QcadooPluginPlugin)15 PluginDependencyResult (com.qcadoo.plugin.api.PluginDependencyResult)9 ArrayList (java.util.ArrayList)8 Version (com.qcadoo.plugin.api.Version)6 InOrder (org.mockito.InOrder)6 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)5 ModuleFactory (com.qcadoo.plugin.api.ModuleFactory)5 Module (com.qcadoo.plugin.api.Module)4 PluginDependencyInformation (com.qcadoo.plugin.api.PluginDependencyInformation)4 PluginOperationResult (com.qcadoo.plugin.api.PluginOperationResult)4 PluginException (com.qcadoo.plugin.internal.PluginException)4 SimplePluginStatusResolver (com.qcadoo.plugin.internal.dependencymanager.SimplePluginStatusResolver)3 File (java.io.File)3 HashSet (java.util.HashSet)3 MultiTenantCallback (com.qcadoo.tenant.api.MultiTenantCallback)2 Resource (org.springframework.core.io.Resource)2 PluginStateResolver (com.qcadoo.plugin.api.PluginStateResolver)1