Search in sources :

Example 31 with InternalPlugin

use of com.qcadoo.plugin.internal.api.InternalPlugin 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

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