All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.plugin.integration.operator.PluginOperatorWrapper Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.plugin.integration.operator;

import org.pf4j.PluginWrapper;
import org.pf4j.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.GenericApplicationContext;
import org.zodiac.commons.util.spring.Springs;
import org.zodiac.plugin.factory.PluginFactory;
import org.zodiac.plugin.factory.process.pipe.PluginInfoContainers;
import org.zodiac.plugin.integration.IntegrationConfiguration;
import org.zodiac.plugin.integration.listener.PluginInitializerListener;
import org.zodiac.plugin.integration.operator.module.PluginInfo;
import org.zodiac.plugin.realize.UnRegistryValidator;
import org.zodiac.sdk.toolkit.io.FileObject;

import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * 插件操作包装器。
 * 
 */
public class PluginOperatorWrapper implements PluginOperator {

    protected final Logger log = LoggerFactory.getLogger(this.getClass());

    private final PluginOperator pluginOperator;
    private final IntegrationConfiguration integrationConfiguration;

    public PluginOperatorWrapper(PluginOperator pluginOperator, IntegrationConfiguration integrationConfiguration) {
        this.pluginOperator = pluginOperator;
        this.integrationConfiguration = integrationConfiguration;
    }

    @Override
    public boolean initPlugins(PluginInitializerListener pluginInitializerListener) throws Exception {
        return pluginOperator.initPlugins(pluginInitializerListener);
    }

    @Override
    public boolean verify(Path jarPath) throws Exception {
        if (isDisable()) {
            return false;
        }
        return pluginOperator.verify(jarPath);
    }

    @Override
    public PluginInfo install(Path jarPath) throws Exception {
        if (isDisable()) {
            return null;
        }
        return pluginOperator.install(jarPath);
    }

    @Override
    public PluginInfo load(Path jarPath) throws Exception {
        if (isDisable()) {
            return null;
        }
        return pluginOperator.install(jarPath);
    }

    @Override
    public PluginInfo load(FileObject pluginFile) throws Exception {
        if (isDisable()) {
            return null;
        }
        return pluginOperator.load(pluginFile);
    }

    @Override
    public boolean unload(String pluginId, boolean isBackup) throws Exception {
        if (isDisable()) {
            return false;
        }
        return pluginOperator.unload(pluginId, isBackup);
    }

    @Override
    public boolean uninstall(String pluginId, boolean isBackup) throws Exception {
        if (isDisable()) {
            return false;
        }
        checkIsUnRegistry(pluginId);
        return pluginOperator.uninstall(pluginId, isBackup);
    }

    @Override
    public boolean start(String pluginId) throws Exception {
        if (isDisable()) {
            return false;
        }
        return pluginOperator.start(pluginId);
    }

    @Override
    public boolean stop(String pluginId) throws Exception {
        if (isDisable()) {
            return false;
        }
        checkIsUnRegistry(pluginId);
        return pluginOperator.stop(pluginId);
    }

    @Override
    public PluginInfo uploadPluginAndStart(FileObject pluginFile) throws Exception {
        if (isDisable()) {
            return null;
        }
        return pluginOperator.uploadPluginAndStart(pluginFile);
    }

    @Override
    public boolean installConfigFile(Path configFilePath) throws Exception {
        if (isDisable()) {
            return false;
        }
        return pluginOperator.installConfigFile(configFilePath);
    }

    @Override
    public boolean uploadConfigFile(FileObject configFile) throws Exception {
        if (isDisable()) {
            return false;
        }
        return pluginOperator.uploadConfigFile(configFile);
    }

    @Override
    public boolean backupPlugin(Path backDirPath, String sign) throws Exception {
        if (isDisable()) {
            return false;
        }
        return pluginOperator.backupPlugin(backDirPath, sign);
    }

    @Override
    public boolean backupPlugin(String pluginId, String sign) throws Exception {
        if (isDisable()) {
            return false;
        }
        return pluginOperator.backupPlugin(pluginId, sign);
    }

    @Override
    public List getPluginInfo() {
        if (isDisable()) {
            return Collections.emptyList();
        }
        return pluginOperator.getPluginInfo();
    }

    @Override
    public PluginInfo getPluginInfo(String pluginId) {
        if (isDisable()) {
            return null;
        }
        return pluginOperator.getPluginInfo(pluginId);
    }

    @Override
    public Set getPluginFilePaths() throws Exception {
        if (isDisable()) {
            return Collections.emptySet();
        }
        return pluginOperator.getPluginFilePaths();
    }

    @Override
    public List getPluginWrapper() {
        if (isDisable()) {
            return Collections.emptyList();
        }
        return pluginOperator.getPluginWrapper();
    }

    @Override
    public PluginWrapper getPluginWrapper(String pluginId) {
        if (isDisable()) {
            return null;
        }
        return pluginOperator.getPluginWrapper(pluginId);
    }

    @Override
    public PluginFactory getPluginFactory() {
        return pluginOperator.getPluginFactory();
    }

    /**
     * 是否被禁用
     * 
     * @return true 禁用
     */
    private boolean isDisable() {
        if (integrationConfiguration.enable()) {
            return false;
        }
        // 如果禁用的话, 直接返回
        log.info("The Plugin module is disabled!");
        return true;
    }

    /**
     * 检查是否可卸载
     * 
     * @param pluginId
     *            插件id
     * @throws Exception
     *             检查异常
     */
    private void checkIsUnRegistry(String pluginId) throws Exception {
        GenericApplicationContext pluginApplicationContext = PluginInfoContainers.getPluginApplicationContext(pluginId);
        if (pluginApplicationContext == null) {
            log.error("Plugin '{}' Not found ApplicationContext. So cannot found and execute unRegistryValidator",
                pluginId);
            return;
        }
        List unRegistryValidators =
            Springs.getBeanList(pluginApplicationContext, UnRegistryValidator.class);
        for (UnRegistryValidator unRegistryValidator : unRegistryValidators) {
            UnRegistryValidator.Result result = unRegistryValidator.verify();
            if (result.isVerify()) {
                return;
            }
            String message = result.getMessage();
            if (StringUtils.isNullOrEmpty(message)) {
                message = "Plugin [" + pluginId + "] Stop or Uninstall be banned";
            }
            throw new Exception(message);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy