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();
}
}
}
Aggregations