Search in sources :

Example 1 with ClassLoaderFactoryImpl

use of com.shulie.instrument.simulator.core.classloader.impl.ClassLoaderFactoryImpl in project LinkAgent by shulieTech.

the class DefaultCoreModuleManager method loadModule.

/**
 * 模块加载
 *
 * @param moduleSpec
 * @param action
 */
private void loadModule(final ModuleSpec moduleSpec, String action) {
    if (isInfoEnabled) {
        logger.info("SIMULATOR: prepare to load module {} ,file={}", moduleSpec.getModuleId(), moduleSpec.getFile().getAbsolutePath());
    }
    if (!moduleSpec.getFile().exists() || !moduleSpec.getFile().canRead()) {
        moduleSpec.setValid(false);
        logger.warn("SIMULATOR: {} modules[{}]: module-lib can not access, cause by file is not exists or can't read. module-lib={}, exists={}, canRead={}", action, moduleSpec.getModuleId(), moduleSpec.getFile(), moduleSpec.getFile().exists(), moduleSpec.getFile().canRead());
        return;
    }
    /**
     * 如果是版本不支持则也没办法正常加载 Module
     */
    if (!(VersionUtils.isLeVersion(moduleSpec.getSinceVersion(), simulatorConfig.getSimulatorVersion()) && VersionUtils.isGeVersion(moduleSpec.getUntilVersion(), simulatorConfig.getSimulatorVersion()))) {
        moduleSpec.setValid(false);
        logger.warn("SIMULATOR: {} modules[{}]: module is not enabled, cause by module version is not support simulator version, will be ignored. module-lib={}, simulator-version:{} module-support-version:{}-{}", action, moduleSpec.getModuleId(), moduleSpec.getFile(), simulatorConfig.getSimulatorVersion(), moduleSpec.getSinceVersion(), moduleSpec.getUntilVersion());
        return;
    }
    /**
     * 只有非必须的模块才可以设置禁用,不然则可以忽略
     */
    if (!moduleSpec.isMustUse()) {
        if (disabledModules.contains(moduleSpec.getModuleId())) {
            moduleSpec.setValid(false);
            logger.warn("SIMULATOR: {} modules[{}]: module is disabled, will be ignored. module-lib={}", action, moduleSpec.getModuleId(), moduleSpec.getFile());
            return;
        }
    }
    try {
        final ClassLoaderFactory moduleClassLoader = new ClassLoaderFactoryImpl(classLoaderService, config, moduleSpec.getFile(), moduleSpec.getModuleId(), moduleSpec.isMiddlewareModule());
        classLoaderService.load(moduleSpec, moduleClassLoader);
    } catch (Throwable e) {
        logger.info("load module [{}] fail, set module invalid", moduleSpec.getModuleId(), e);
        moduleSpec.setValid(false);
    }
    if (isInfoEnabled) {
        logger.info("SIMULATOR: {} modules[{}]: load module success. module-lib={}", action, moduleSpec.getModuleId(), moduleSpec.getFile());
    }
    if (CollectionUtils.isNotEmpty(moduleSpec.getDependencies())) {
        /**
         * 如果开关已经是开启状态,则直接执行即可
         */
        if (switcherManager.isAllSwitchOn(moduleSpec.getDependencies())) {
            loadModule(moduleSpec);
            if (isInfoEnabled) {
                logger.info("SIMULATOR: load module {} successful,file={}", moduleSpec.getModuleId(), moduleSpec.getFile().getAbsolutePath());
            }
        } else {
            switcherManager.registerMultiSwitchOnCallback(moduleSpec.getDependencies(), new Runnable() {

                @Override
                public void run() {
                    /**
                     * 当开启状态时执行加载
                     */
                    if (switcherManager.isAllSwitchOn(moduleSpec.getDependencies())) {
                        loadModule(moduleSpec);
                        if (isInfoEnabled) {
                            logger.info("SIMULATOR: load module {} successful,file={}", moduleSpec.getModuleId(), moduleSpec.getFile().getAbsolutePath());
                        }
                    } else {
                        /**
                         * 否则重新注册开关,因为回调执行一次就会销毁
                         */
                        switcherManager.registerMultiSwitchOnCallback(moduleSpec.getDependencies(), this);
                    }
                }
            });
        }
    } else {
        loadModule(moduleSpec);
        if (isInfoEnabled) {
            logger.info("SIMULATOR: load module {} successful,file={}", moduleSpec.getModuleId(), moduleSpec.getFile().getAbsolutePath());
        }
    }
}
Also used : ClassLoaderFactory(com.shulie.instrument.simulator.core.classloader.ClassLoaderFactory) ClassLoaderFactoryImpl(com.shulie.instrument.simulator.core.classloader.impl.ClassLoaderFactoryImpl)

Aggregations

ClassLoaderFactory (com.shulie.instrument.simulator.core.classloader.ClassLoaderFactory)1 ClassLoaderFactoryImpl (com.shulie.instrument.simulator.core.classloader.impl.ClassLoaderFactoryImpl)1