Search in sources :

Example 1 with XStream2

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;
}
Also used : XStream2(com.qlangtech.tis.util.XStream2) Logger(org.slf4j.Logger) XmlFile(com.qlangtech.tis.extension.impl.XmlFile) TIS(com.qlangtech.tis.TIS) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) RobustReflectionConverter(com.qlangtech.tis.util.RobustReflectionConverter) List(java.util.List) Lists(com.google.common.collect.Lists) XStream2PluginInfoReader(com.qlangtech.tis.util.XStream2PluginInfoReader) Collections(java.util.Collections) XmlFile(com.qlangtech.tis.extension.impl.XmlFile) File(java.io.File) XStream2(com.qlangtech.tis.util.XStream2)

Example 2 with XStream2

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;
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) java.util(java.util) XStream2(com.qlangtech.tis.util.XStream2) TIS(com.qlangtech.tis.TIS) ClassUtils(org.apache.commons.lang.ClassUtils) ISelectedTab(com.qlangtech.tis.plugin.ds.ISelectedTab) Option(com.qlangtech.tis.manage.common.Option) FormField(com.qlangtech.tis.plugin.annotation.FormField) SubForm(com.qlangtech.tis.plugin.annotation.SubForm) Callable(java.util.concurrent.Callable) Context(com.alibaba.citrus.turbine.Context) IFieldErrorHandler(com.qlangtech.tis.runtime.module.misc.IFieldErrorHandler) JSONArray(com.alibaba.fastjson.JSONArray) ValidatorCommons(com.qlangtech.tis.plugin.ValidatorCommons) Lists(com.google.common.collect.Lists) Matcher(java.util.regex.Matcher) com.qlangtech.tis.extension.impl(com.qlangtech.tis.extension.impl) IDataxProcessor(com.qlangtech.tis.datax.IDataxProcessor) FormFieldType(com.qlangtech.tis.plugin.annotation.FormFieldType) DefaultFieldErrorHandler(com.qlangtech.tis.runtime.module.misc.impl.DefaultFieldErrorHandler) GroovyShellEvaluate(com.qlangtech.tis.extension.util.GroovyShellEvaluate) Validator(com.qlangtech.tis.plugin.annotation.Validator) java.lang.reflect(java.lang.reflect) PluginExtraProps(com.qlangtech.tis.extension.util.PluginExtraProps) ImmutableMap(com.google.common.collect.ImmutableMap) DataxWriter(com.qlangtech.tis.datax.impl.DataxWriter) IdentityName(com.qlangtech.tis.plugin.IdentityName) AttrValMap(com.qlangtech.tis.util.AttrValMap) IOException(java.io.IOException) Maps(com.google.common.collect.Maps) IPluginContext(com.qlangtech.tis.util.IPluginContext) Collectors(java.util.stream.Collectors) File(java.io.File) Types(org.jvnet.tiger_types.Types) JSON(com.alibaba.fastjson.JSON) IControlMsgHandler(com.qlangtech.tis.runtime.module.misc.IControlMsgHandler) ISelectOptionsGetter(com.qlangtech.tis.util.ISelectOptionsGetter) JSONObject(com.alibaba.fastjson.JSONObject) Pattern(java.util.regex.Pattern) IMessageHandler(com.qlangtech.tis.runtime.module.misc.IMessageHandler) JSONArray(com.alibaba.fastjson.JSONArray) XStream2(com.qlangtech.tis.util.XStream2) JSONObject(com.alibaba.fastjson.JSONObject) ImmutableMap(com.google.common.collect.ImmutableMap) AttrValMap(com.qlangtech.tis.util.AttrValMap)

Example 3 with XStream2

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);
}
Also used : ComponentMeta(com.qlangtech.tis.plugin.ComponentMeta) IRepositoryResource(com.qlangtech.tis.plugin.IRepositoryResource) File(java.io.File) XStream2(com.qlangtech.tis.util.XStream2)

Example 4 with XStream2

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);
    }
}
Also used : PluginManager(com.qlangtech.tis.extension.PluginManager) XStream2(com.qlangtech.tis.util.XStream2)

Example 5 with XStream2

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);
}
Also used : UploadPluginMeta(com.qlangtech.tis.util.UploadPluginMeta) XStream2(com.qlangtech.tis.util.XStream2) ParamsConfig(com.qlangtech.tis.config.ParamsConfig) Descriptor(com.qlangtech.tis.extension.Descriptor)

Aggregations

XStream2 (com.qlangtech.tis.util.XStream2)5 File (java.io.File)3 Lists (com.google.common.collect.Lists)2 TIS (com.qlangtech.tis.TIS)2 IOException (java.io.IOException)2 Context (com.alibaba.citrus.turbine.Context)1 JSON (com.alibaba.fastjson.JSON)1 JSONArray (com.alibaba.fastjson.JSONArray)1 JSONObject (com.alibaba.fastjson.JSONObject)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Maps (com.google.common.collect.Maps)1 ParamsConfig (com.qlangtech.tis.config.ParamsConfig)1 IDataxProcessor (com.qlangtech.tis.datax.IDataxProcessor)1 DataxWriter (com.qlangtech.tis.datax.impl.DataxWriter)1 Descriptor (com.qlangtech.tis.extension.Descriptor)1 PluginManager (com.qlangtech.tis.extension.PluginManager)1 com.qlangtech.tis.extension.impl (com.qlangtech.tis.extension.impl)1 XmlFile (com.qlangtech.tis.extension.impl.XmlFile)1 GroovyShellEvaluate (com.qlangtech.tis.extension.util.GroovyShellEvaluate)1 PluginExtraProps (com.qlangtech.tis.extension.util.PluginExtraProps)1