Search in sources :

Example 26 with InternalPlugin

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

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

the class DefaultPluginDescriptorParser method getTemporaryPlugins.

@Override
public Set<InternalPlugin> getTemporaryPlugins() {
    Set<InternalPlugin> plugins = new HashSet<InternalPlugin>();
    if (pluginsTmpPath == null || pluginsTmpPath.trim().isEmpty()) {
        return plugins;
    }
    File pluginsTmpFile = new File(pluginsTmpPath);
    if (!pluginsTmpFile.exists()) {
        LOG.warn("Plugins temporary directory does not exist: " + pluginsTmpPath);
        return plugins;
    }
    try {
        FilenameFilter jarsFilter = new WildcardFileFilter("*.jar");
        if (!pluginsTmpFile.exists()) {
            throw new IOException();
        }
        File[] pluginJars = pluginsTmpFile.listFiles(jarsFilter);
        for (int i = 0; i < pluginJars.length; ++i) {
            File jarRes = pluginJars[i];
            InternalPlugin plugin = parse(jarRes);
            plugin.changeStateTo(PluginState.TEMPORARY);
            plugins.add(plugin);
        }
        return plugins;
    } catch (IOException e) {
        throw new IllegalStateException("Failed to reading plugins from " + pluginsTmpPath, e);
    }
}
Also used : InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin) FilenameFilter(java.io.FilenameFilter) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) File(java.io.File) WildcardFileFilter(org.apache.commons.io.filefilter.WildcardFileFilter)

Example 28 with InternalPlugin

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

the class DefaultPluginDescriptorParser method parse.

private InternalPlugin parse(final InputStream inputStream, final boolean ignoreModules, final String fileName) throws IOException {
    try {
        Document document = documentBuilder.parse(inputStream);
        Node root = document.getDocumentElement();
        Builder pluginBuilder = parsePluginNode(root, ignoreModules);
        InternalPlugin plugin = pluginBuilder.withFileName(fileName).build();
        LOG.info("Parse complete");
        return plugin;
    } catch (SAXException e) {
        throw new PluginException(e.getMessage(), e);
    }
}
Also used : InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin) Node(org.w3c.dom.Node) Builder(com.qcadoo.plugin.internal.DefaultPlugin.Builder) DOMBuilder(org.jdom.input.DOMBuilder) DocumentBuilder(javax.xml.parsers.DocumentBuilder) PluginException(com.qcadoo.plugin.internal.PluginException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Example 29 with InternalPlugin

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

the class DefaultPluginAccessor method onApplicationEvent.

@Override
public void onApplicationEvent(final ContextRefreshedEvent event) {
    if (!alreadyInitialized) {
        synchronized (this) {
            if (!alreadyInitialized) {
                init();
                alreadyInitialized = true;
            }
        }
    }
    if (event != null && event.getSource() instanceof XmlWebApplicationContext) {
        XmlWebApplicationContext eventSource = (XmlWebApplicationContext) event.getSource();
        if (eventSource.getParent() != null) {
            return;
        }
    }
    long time = System.currentTimeMillis();
    List<Plugin> sortedPlugins = pluginDependencyManager.sortPluginsInDependencyOrder(plugins.values(), plugins);
    moduleFactoryAccessor.init(sortedPlugins);
    for (Plugin plugin : sortedPlugins) {
        if (plugin.hasState(PluginState.ENABLING)) {
            ((InternalPlugin) plugin).changeStateTo(PluginState.ENABLED);
            pluginDao.save(plugin);
        }
    }
    LOG.info("Plugin Framework started in " + (System.currentTimeMillis() - time) + "ms");
}
Also used : InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) QcadooPluginPlugin(com.qcadoo.model.beans.qcadooPlugin.QcadooPluginPlugin) Plugin(com.qcadoo.plugin.api.Plugin) InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin)

Example 30 with InternalPlugin

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

the class DefaultPluginAccessor method init.

public void init() {
    LOG.info("Plugin Framework initialization");
    long time = System.currentTimeMillis();
    pluginStateResolver.setPluginAccessor(this);
    Set<InternalPlugin> enabledPluginsFromDescriptor = pluginDescriptorParser.loadPlugins();
    Set<QcadooPluginPlugin> pluginsFromDatabase = pluginDao.list();
    for (InternalPlugin plugin : enabledPluginsFromDescriptor) {
        QcadooPluginPlugin existingPlugin = null;
        for (QcadooPluginPlugin databasePlugin : pluginsFromDatabase) {
            if (plugin.getIdentifier().equals(databasePlugin.getIdentifier())) {
                existingPlugin = databasePlugin;
                break;
            }
        }
        if (existingPlugin == null) {
            plugin.changeStateTo(PluginState.ENABLING);
        } else {
            plugin.changeStateTo(PluginState.valueOf(existingPlugin.getState()));
        }
        if (existingPlugin == null || plugin.compareVersion(new Version(existingPlugin.getVersion())) > 0) {
            pluginDao.save(plugin);
        } else if (plugin.compareVersion(new Version(existingPlugin.getVersion())) < 0) {
            throw new IllegalStateException("Unsupported operation: downgrade, for plugin: " + plugin.getIdentifier());
        }
        LOG.info("Registering plugin " + plugin);
        plugins.put(plugin.getIdentifier(), plugin);
    }
    for (QcadooPluginPlugin databasePlugin : pluginsFromDatabase) {
        if (databasePlugin.getState().equals(PluginState.TEMPORARY.toString())) {
            continue;
        }
        Plugin existingPlugin = null;
        for (Plugin plugin : enabledPluginsFromDescriptor) {
            if (databasePlugin.getIdentifier().equals(plugin.getIdentifier())) {
                existingPlugin = plugin;
                break;
            }
        }
        if (existingPlugin == null) {
            pluginDao.delete(databasePlugin);
        }
    }
    Set<InternalPlugin> temporaryPlugins = pluginDescriptorParser.getTemporaryPlugins();
    for (InternalPlugin plugin : temporaryPlugins) {
        plugins.put(plugin.getIdentifier(), plugin);
    }
    LOG.info("Plugin Framework initialized in " + (System.currentTimeMillis() - time) + "ms");
}
Also used : InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin) QcadooPluginPlugin(com.qcadoo.model.beans.qcadooPlugin.QcadooPluginPlugin) Version(com.qcadoo.plugin.api.Version) 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