You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
168 lines
6.2 KiB
168 lines
6.2 KiB
package com.fanruan.boot.env; |
|
|
|
import com.fanruan.boot.PluginComponent; |
|
import com.fanruan.carina.Carina; |
|
import com.fanruan.carina.annotions.DependsOn; |
|
import com.fanruan.carina.annotions.FineComponent; |
|
import com.fanruan.carina.annotions.Start; |
|
import com.fanruan.carina.annotions.Stop; |
|
import com.fanruan.carina.annotions.Supplemental; |
|
import com.fanruan.plugin.autonomy.AutonomyClassManagerFactory; |
|
import com.fanruan.plugin.autonomy.AutonomyClassManagerGroup; |
|
import com.fanruan.plugin.autonomy.AutonomyClassManagerProvider; |
|
import com.fr.config.ConfigContext; |
|
import com.fr.invoke.ClassFactory; |
|
import com.fr.json.JSONObject; |
|
import com.fr.plugin.beforeload.embed.PluginEmbedInfo; |
|
import com.fr.plugin.config.PluginConfigContext; |
|
import com.fr.plugin.db.PluginDBManager; |
|
import com.fr.plugin.injectable.LevelChecker; |
|
import com.fr.plugin.injectable.PluginInjectionFilter; |
|
import com.fr.plugin.injectable.PluginInjectionValidator; |
|
import com.fr.plugin.injectable.SpecialLevel; |
|
import com.fr.plugin.manage.PluginManager; |
|
import com.fr.plugin.manage.PluginSyncModuleType; |
|
import com.fr.plugin.observer.PluginEvent; |
|
import com.fr.plugin.observer.PluginEventListener; |
|
import com.fr.plugin.observer.PluginListenerRegistration; |
|
import com.fr.plugin.validate.PluginValidator; |
|
import com.fr.stable.CommonUtils; |
|
import com.fr.stable.fun.Level; |
|
import com.fr.stable.project.ProjectConstants; |
|
import com.fr.stable.resource.ResourceLoader; |
|
import com.fr.startup.web.annotation.MappingJackson2HttpMessageConverter; |
|
import com.fr.workspace.WorkContext; |
|
import com.fr.workspace.server.repository.plugin.PluginRepository; |
|
|
|
import javax.servlet.ServletContext; |
|
import java.io.File; |
|
import java.net.MalformedURLException; |
|
import java.net.URL; |
|
import java.net.URLClassLoader; |
|
import java.util.Collection; |
|
import java.util.List; |
|
import java.util.function.Supplier; |
|
|
|
/** |
|
* 设计器插件模块,环境切换的中间模块 |
|
* |
|
* @author Destiny.Lin |
|
* @since 11.0 |
|
* Created on 2024/5/17 |
|
*/ |
|
@FineComponent(name = "design_plugin") |
|
@DependsOn(dependencies = {"design_version"}) |
|
public class DesignPluginComponent extends PluginComponent { |
|
|
|
|
|
/** |
|
* prepare |
|
*/ |
|
@Supplemental |
|
public void prepare() { |
|
super.prepare(); |
|
} |
|
/** |
|
* start |
|
*/ |
|
@Start |
|
public void start() { |
|
//收集插件对象的过滤器 |
|
Collection<PluginInjectionFilter> filters = Carina.getApplicationContext().group(PluginInjectionFilter.GroupKey.class).getAll(); |
|
for (PluginInjectionFilter filter: filters) { |
|
PluginManager.addInjectionFilter(filter); |
|
} |
|
|
|
//装备外部的验证器 |
|
assembleValidator(); |
|
// 注册内置插件信息 |
|
registerEmbedPluginInfo(); |
|
// 注册插件配置类Provider |
|
registerPluginConfigProvider(); |
|
|
|
PluginInjectionValidator.setLevelChecker(new LevelChecker() { |
|
@Override |
|
public boolean isStandard(String name) { |
|
//不在特殊表中的Level |
|
return SpecialLevel.convert(name) == null; |
|
} |
|
|
|
@Override |
|
public boolean validateSpecificLevel(String name, Level injectionObject) { |
|
SpecialLevel specialLevel = SpecialLevel.convert(name); |
|
return specialLevel == null || specialLevel.getCurrentLevel() <= injectionObject.currentAPILevel(); |
|
} |
|
}); |
|
|
|
|
|
AutonomyClassManagerFactory.getInstance().registerAutonomyClassManager(Carina.getApplicationContext().group(AutonomyClassManagerGroup.class).getAll().toArray(new AutonomyClassManagerProvider[0])); |
|
|
|
Supplier<List<JSONObject>> supplier = () -> PluginRepository.getInstance().getPluginStatus(); |
|
PluginManager.registerRemotePluginSupplier(supplier); |
|
PluginManager.setSyncModuleType(PluginSyncModuleType.DESIGN); |
|
//PluginManager要在环境切换和模块启动之前初始化 |
|
PluginManager.startEngine(); |
|
|
|
PluginListenerRegistration.getInstance().listenRunningChanged(new PluginEventListener() { |
|
@Override |
|
public void on(PluginEvent event) { |
|
MappingJackson2HttpMessageConverter.KEY.refreshObjectMapper(); |
|
} |
|
}); |
|
registerPluginClassFinder(); |
|
PluginDBManager.getInstance().init(); |
|
} |
|
|
|
@Override |
|
protected void addPluginStoreFilter(ServletContext servletContext) { |
|
// do nothing |
|
} |
|
|
|
private void registerEmbedPluginInfo() { |
|
Collection<PluginEmbedInfo> pluginEmbedInfos = Carina.getApplicationContext().group(PluginEmbedInfo.class).getAll(); |
|
|
|
PluginManager.getPluginEmbedManager().addPluginEmbedInfos(pluginEmbedInfos); |
|
} |
|
|
|
private void registerPluginConfigProvider() { |
|
ConfigContext.setPluginConfigContextProvider(new PluginConfigContext()); |
|
} |
|
|
|
private void assembleValidator() { |
|
PluginManager.getValidatorManager().register(Carina.getApplicationContext().group(PluginValidator.GroupKey.class).getAll()); |
|
} |
|
|
|
|
|
private void registerPluginClassFinder() { |
|
ClassFactory.getInstance().addFinder(className -> { |
|
//再找. |
|
// alex:currentEnv可能是null |
|
String envPath = WorkContext.getCurrent().getPath(); |
|
if (envPath == null) { |
|
throw new ClassNotFoundException(className); |
|
} |
|
File clssesFile = new File(CommonUtils.pathJoin(envPath, ProjectConstants.CLASSES_NAME)); |
|
ClassLoader urlClassLoader; |
|
try { |
|
urlClassLoader = new URLClassLoader(new URL[]{clssesFile.toURI().toURL()}, CommonUtils.class.getClassLoader()); |
|
} catch (MalformedURLException malformedURLException) { |
|
urlClassLoader = ClassLoader.getSystemClassLoader(); |
|
} |
|
return Class.forName(className, true, urlClassLoader); |
|
}); |
|
|
|
//从插件中拿 |
|
ClassFactory.getInstance().addFinder(ResourceLoader::classForName); |
|
//从插件引擎中拿 |
|
ClassFactory.getInstance().addFinder(className -> Class.forName(className, true, PluginManager.getController().getClass().getClassLoader())); |
|
} |
|
|
|
/** |
|
* stop |
|
*/ |
|
@Stop |
|
public void stop() { |
|
super.stop(); |
|
PluginDBManager.getInstance().destroy(); |
|
} |
|
}
|
|
|