use of com.qlangtech.tis.util.XStream2 in project tis by qlangtech.
the class ComponentMeta method synchronizePluginsPackageFromRemote.
/**
* 同步插件包
*
* @return 本地被更新的插件包
*/
public List<XStream2.PluginMeta> synchronizePluginsPackageFromRemote() {
List<XStream2.PluginMeta> updateTpiPkgs = Lists.newArrayList();
Set<XStream2.PluginMeta> pluginMetas = loadPluginMeta();
try {
for (XStream2.PluginMeta m : pluginMetas) {
List<File> pluginFileCollector = Lists.newArrayList();
if (m.copyFromRemote(pluginFileCollector)) {
// 本地包已经被更新
updateTpiPkgs.add(m);
if (TIS.permitInitialize) {
for (File f : pluginFileCollector) {
// 动态安装插件
TIS.get().getPluginManager().dynamicLoad(f, true, null);
}
}
}
}
} catch (Throwable e) {
throw new RuntimeException(e);
}
logger.info("download plugin from remote repository:" + updateTpiPkgs.stream().map((m) -> m.toString()).collect(Collectors.joining(",")));
return updateTpiPkgs;
}
use of com.qlangtech.tis.util.XStream2 in project tis by qlangtech.
the class Descriptor method buildPluginInstance.
private <TARGET> TARGET buildPluginInstance(IPluginContext pluginContext, Map<String, JSONObject> keyValMap, ParseDescribable<TARGET> result, PluginFormProperties propertyTypes) {
TARGET describable = result.instance;
String attr;
PropertyType attrDesc;
JSONObject valJ;
String impl;
Descriptor descriptor;
String attrVal;
for (Map.Entry<String, PropertyType> entry : propertyTypes.getKVTuples()) {
attr = entry.getKey();
attrDesc = entry.getValue();
valJ = keyValMap.get(attr);
if (valJ == null && attrDesc.isInputRequired()) {
throw new IllegalStateException("prop:" + attr + " can not be empty");
}
if (valJ == null) {
valJ = new JSONObject();
}
if (attrDesc.isDescribable()) {
JSONObject descVal = valJ.getJSONObject("descVal");
impl = descVal.getString("impl");
descriptor = TIS.get().getDescriptor(impl);
if (descriptor == null) {
throw new IllegalStateException("impl:" + impl + " relevant descripotor can not be null");
}
ParseDescribable vals = descriptor.newInstance(pluginContext, parseAttrValMap(descVal.get("vals")), Optional.empty());
attrDesc.setVal(describable, vals.instance);
} else {
if (attrDesc.typeIdentity() == FormFieldType.MULTI_SELECTABLE.getIdentity()) {
List<FormFieldType.SelectedItem> selectedItems = getSelectedMultiItems(valJ);
List<String> multi = selectedItems.stream().filter((item) -> item.isChecked()).map((item) -> (String) item.getValue()).collect(Collectors.toList());
attrDesc.setVal(describable, multi);
} else {
boolean containVal = valJ.containsKey(KEY_primaryVal) && StringUtils.isNotBlank(valJ.getString(KEY_primaryVal));
// describable
if (!containVal && attrDesc.isInputRequired()) {
throw new IllegalStateException("prop:" + attr + " can not be empty");
}
if (containVal) {
attrVal = valJ.getString(KEY_primaryVal);
attrDesc.setVal(describable, attrVal);
if (valJ.containsKey(KEY_OPTIONS)) {
JSONArray options = valJ.getJSONArray(KEY_OPTIONS);
JSONObject opt = null;
for (int i = 0; i < options.size(); i++) {
opt = options.getJSONObject(i);
try {
// 将options中的选中的插件来源记录下来,后续在集群中各组件中传输插件可以用
if (StringUtils.equals(attrVal, opt.getString("name"))) {
Class<?> implClass = TIS.get().pluginManager.uberClassLoader.loadClass(opt.getString("impl"));
PluginWrapper pluginWrapper = TIS.get().pluginManager.whichPlugin(implClass);
XStream2.PluginMeta pluginMeta = pluginWrapper.getDesc();
result.extraPluginMetas.add(pluginMeta);
break;
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}
}
}
}
}
return describable;
}
use of com.qlangtech.tis.util.XStream2 in project tis by qlangtech.
the class TestXmlFile method testMashall.
// public void testArrayPropSave() throws Exception {
// File testFile = new File("/tmp/test_file.xml");
// XmlFile xmlFile = new XmlFile(testFile);
// TestObject tstObject = new TestObject();
// tstObject.setCols(Collections.singletonList("hello"));
// xmlFile.write(tstObject, Collections.emptySet());
// }
/**
* 测试序列化
*/
public void testMashall() throws Exception {
File testFile = new File("/tmp/test_file.xml");
XmlFile xmlFile = new XmlFile(testFile);
List<TestBean> plugins = Lists.newArrayList();
plugins.add(new TestBean("baisui"));
plugins.add(new TestBean("dabao"));
plugins.add(new TestBean("xiaobao"));
Set<XStream2.PluginMeta> pluginsMeta = Sets.newHashSet();
pluginsMeta.addAll(XStream2.PluginMeta.parse("test1@1.1"));
pluginsMeta.addAll(XStream2.PluginMeta.parse("mock2@1.2"));
xmlFile.write(plugins, pluginsMeta);
List<IRepositoryResource> resources = Lists.newArrayList();
resources.add(new FileRepositoryResource(testFile));
ComponentMeta componentMeta = new ComponentMeta(resources);
Set<XStream2.PluginMeta> pluginMetaSet = componentMeta.loadPluginMeta();
assertEquals(2, pluginMetaSet.size());
for (XStream2.PluginMeta pm : pluginsMeta) {
assertTrue(pm.toString(), pluginMetaSet.contains(pm));
}
FileUtils.deleteQuietly(testFile);
}
use of com.qlangtech.tis.util.XStream2 in project tis by qlangtech.
the class TestComponentMeta method testSynchronizePluginsPackageFromRemote.
/**
* 通过 执行 synchronizePluginsPackageFromRemote 可以动态加载类
*/
public void testSynchronizePluginsPackageFromRemote() {
// 加载完成之后,本地class 要能够加载出来
PluginManager pluginManager = TIS.get().getPluginManager();
String MySQLV5DataSourceFactory = "com.qlangtech.tis.plugin.ds.mysql.MySQLV5DataSourceFactory";
try {
pluginManager.uberClassLoader.loadClass(MySQLV5DataSourceFactory);
fail("shall not find class");
} catch (ClassNotFoundException e) {
}
ComponentMeta componentMeta = new ComponentMeta(Collections.emptyList()) {
public Set<XStream2.PluginMeta> loadPluginMeta() {
XStream2.PluginMeta pluginMeta = new XStream2.PluginMeta("tis-ds-mysql-v5-plugin", "2.3.0");
return Collections.singleton(pluginMeta);
}
};
componentMeta.synchronizePluginsPackageFromRemote();
try {
assertNotNull(pluginManager.uberClassLoader.loadClass(MySQLV5DataSourceFactory));
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
use of com.qlangtech.tis.util.XStream2 in project tis by qlangtech.
the class TestParamsConfigPluginStore method testWriteAndGet.
public void testWriteAndGet() {
UploadPluginMeta pluginMeta = UploadPluginMeta.parse(UploadPluginMeta.KEY_TARGET_PLUGIN_DESC + "_test1");
ParamsConfigPluginStore paramsCfgPluginStore = new ParamsConfigPluginStore(pluginMeta);
List<Descriptor.ParseDescribable<ParamsConfig>> dlist = Lists.newArrayList();
Test1ParamsConfig cfg1 = new Test1ParamsConfig();
cfg1.name = "id1";
Descriptor.ParseDescribable<ParamsConfig> pluginDesc1 = new Descriptor.ParseDescribable<>(cfg1);
pluginDesc1.extraPluginMetas.add(new XStream2.PluginMeta("test1meta", "1.0.0"));
dlist.add(pluginDesc1);
Test2ParamsConfig cfg2 = new Test2ParamsConfig();
cfg2.name = "id1";
Descriptor.ParseDescribable<ParamsConfig> pluginDesc2 = new Descriptor.ParseDescribable<>(cfg2);
pluginDesc2.extraPluginMetas.add(new XStream2.PluginMeta("test2meta", "1.0.0"));
dlist.add(pluginDesc2);
paramsCfgPluginStore.setPlugins(null, Optional.empty(), dlist);
List<ParamsConfig> plugins = paramsCfgPluginStore.getPlugins();
assertEquals(3, plugins.size());
cfg2 = new Test2ParamsConfig();
cfg2.name = "id1";
pluginDesc2 = new Descriptor.ParseDescribable<>(cfg2);
pluginDesc2.extraPluginMetas.add(new XStream2.PluginMeta("test2meta", "1.0.0"));
dlist.add(pluginDesc2);
// 保存应该是要出错的,因为cfg2存在Id重复的问题
paramsCfgPluginStore.setPlugins(null, Optional.empty(), dlist);
}
Aggregations