Search in sources :

Example 1 with IDynamicExtension

use of org.talend.core.runtime.dynamic.IDynamicExtension in project tbd-studio-se by Talend.

the class DynamicPluginAdapter method getDynamicExtension.

public static IDynamicExtension getDynamicExtension(IDynamicPlugin dynamicPlugin, String extensionPoint) {
    List<IDynamicExtension> allExtensions = dynamicPlugin.getAllExtensions();
    IDynamicExtension libNeededExtension = null;
    for (IDynamicExtension extension : allExtensions) {
        if (extensionPoint.equals(extension.getExtensionPoint())) {
            libNeededExtension = extension;
            break;
        }
    }
    return libNeededExtension;
}
Also used : IDynamicExtension(org.talend.core.runtime.dynamic.IDynamicExtension)

Example 2 with IDynamicExtension

use of org.talend.core.runtime.dynamic.IDynamicExtension in project tbd-studio-se by Talend.

the class DynamicPluginAdapter method cleanUnusedAndRefresh.

/**
 * can't use adapt and clean same time
 *
 * @throws Exception
 */
public void cleanUnusedAndRefresh() throws Exception {
    buildIdMaps();
    // 1. unify module versions to use latest version
    unifyModuleVersions();
    // 2. clean unused modules
    Set<String> usedModulesSet = new HashSet<String>();
    Collection<IDynamicConfiguration> moduleGroups = moduleGroupTemplateMap.values();
    Iterator<IDynamicConfiguration> moduleGroupIter = moduleGroups.iterator();
    while (moduleGroupIter.hasNext()) {
        IDynamicConfiguration moduleGroup = moduleGroupIter.next();
        List<IDynamicConfiguration> childConfigurations = moduleGroup.getChildConfigurations();
        if (childConfigurations != null) {
            Set<String> curUsedModules = new HashSet<>();
            Set<IDynamicConfiguration> oldVersionModuleConfigs = new HashSet<>();
            Map<String, IDynamicConfiguration> latestGaIdMap = new HashMap<>();
            Iterator<IDynamicConfiguration> libraryIter = childConfigurations.iterator();
            while (libraryIter.hasNext()) {
                IDynamicConfiguration childConfig = libraryIter.next();
                String libraryId = (String) childConfig.getAttribute(DynamicModuleGroupAdapter.ATTR_LIBRARY_ID);
                if (libraryId != null) {
                    if (curUsedModules.contains(libraryId)) {
                        libraryIter.remove();
                    } else {
                        String mvnUri = getMvnUri(libraryId);
                        if (mvnUri != null) {
                            MavenArtifact curMa = MavenUrlHelper.parseMvnUrl(mvnUri);
                            String key = getGaKey(curMa);
                            IDynamicConfiguration storedConfig = latestGaIdMap.get(key);
                            if (storedConfig == null) {
                                latestGaIdMap.put(key, childConfig);
                                curUsedModules.add(libraryId);
                            } else {
                                String storedId = (String) storedConfig.getAttribute(DynamicModuleGroupAdapter.ATTR_LIBRARY_ID);
                                String curVersion = curMa.getVersion();
                                String storedMvnUri = getMvnUri(storedId);
                                MavenArtifact storedMa = MavenUrlHelper.parseMvnUrl(storedMvnUri);
                                String type = storedMa.getType();
                                boolean isPom = TYPE_POM.equalsIgnoreCase(type);
                                if (isPom) {
                                    curUsedModules.add(libraryId);
                                } else {
                                    String storedVersion = storedMa.getVersion();
                                    if (0 < versionComparator.compare(curVersion, storedVersion)) {
                                        curUsedModules.remove(storedId);
                                        curUsedModules.add(libraryId);
                                        oldVersionModuleConfigs.add(storedConfig);
                                        latestGaIdMap.put(key, childConfig);
                                    } else {
                                        libraryIter.remove();
                                    }
                                }
                            }
                        } else {
                            curUsedModules.add(libraryId);
                        }
                    }
                }
            }
            childConfigurations.removeAll(oldVersionModuleConfigs);
            Collections.sort(childConfigurations, new DynamicAttributeComparator(DynamicModuleGroupAdapter.ATTR_LIBRARY_ID));
            usedModulesSet.addAll(curUsedModules);
        }
    }
    Set<String> unusedModulesSet = new HashSet<String>(moduleMap.keySet());
    unusedModulesSet.removeAll(usedModulesSet);
    if (!unusedModulesSet.isEmpty()) {
        Set<IDynamicConfiguration> unusedConfigs = new HashSet<>();
        for (String unusedModule : unusedModulesSet) {
            IDynamicConfiguration moduleById = getModuleById(unusedModule);
            if (moduleById != null) {
                unusedConfigs.add(moduleById);
            }
            moduleMap.remove(unusedModule);
        }
        if (!unusedConfigs.isEmpty()) {
            IDynamicExtension libNeededExtension = getLibraryNeededExtension(plugin);
            List<IDynamicConfiguration> configurations = libNeededExtension.getConfigurations();
            configurations.removeAll(unusedConfigs);
            Collections.sort(configurations, new DynamicAttributeComparator());
        }
    }
    // 3. refresh classLoader
    IDynamicExtension classLoaderExtension = getClassLoaderExtension(plugin);
    List<IDynamicConfiguration> classLoaders = classLoaderExtension.getConfigurations();
    if (classLoaders != null) {
        for (IDynamicConfiguration classLoader : classLoaders) {
            String moduleGroupTemplateId = (String) classLoader.getAttribute(DynamicClassloaderAdapter.ATTR_MODULE_GROUP_TEMPLATE_ID);
            IDynamicConfiguration moduleGroupByTemplateId = getModuleGroupByTemplateId(moduleGroupTemplateId);
            List<IDynamicConfiguration> libraries = moduleGroupByTemplateId.getChildConfigurations();
            List<String> libraryIds = new ArrayList<>();
            for (IDynamicConfiguration library : libraries) {
                String libraryId = (String) library.getAttribute(DynamicModuleGroupAdapter.ATTR_LIBRARY_ID);
                if (!libraryIds.contains(libraryId)) {
                    libraryIds.add(libraryId);
                }
            }
            if (!libraryIds.isEmpty()) {
                Collections.sort(libraryIds);
                String buildLibrariesString = DynamicClassloaderAdapter.buildLibrariesString(libraryIds);
                classLoader.setAttribute(DynamicClassloaderAdapter.ATTR_LIBRARIES, buildLibrariesString);
            }
        }
    }
}
Also used : IDynamicConfiguration(org.talend.core.runtime.dynamic.IDynamicConfiguration) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IDynamicExtension(org.talend.core.runtime.dynamic.IDynamicExtension) MavenArtifact(org.talend.core.runtime.maven.MavenArtifact) DynamicAttributeComparator(org.talend.hadoop.distribution.dynamic.comparator.DynamicAttributeComparator) HashSet(java.util.HashSet)

Example 3 with IDynamicExtension

use of org.talend.core.runtime.dynamic.IDynamicExtension in project tbd-studio-se by Talend.

the class DynamicPluginAdapter method buildIdMaps.

public void buildIdMaps() throws Exception {
    moduleMap.clear();
    moduleGroupTemplateMap.clear();
    IDynamicExtension libNeededExtension = getLibraryNeededExtension(plugin);
    if (libNeededExtension == null) {
        throw new Exception("Can't find extension: " + DynamicLibraryNeededExtensionAdaper.ATTR_POINT);
    }
    List<IDynamicConfiguration> configurations = libNeededExtension.getConfigurations();
    if (configurations == null || configurations.isEmpty()) {
        throw new Exception("No libraryModuelGroup configured");
    }
    for (IDynamicConfiguration configuration : configurations) {
        if (DynamicModuleGroupAdapter.TAG_NAME.equals(configuration.getTagName())) {
            String templateId = (String) configuration.getAttribute(DynamicModuleGroupAdapter.ATTR_GROUP_TEMPLATE_ID);
            if (StringUtils.isEmpty(templateId)) {
                throw new Exception("Template id is not configured!");
            }
            moduleGroupTemplateMap.put(templateId, configuration);
        } else if (DynamicModuleAdapter.TAG_NAME.equals(configuration.getTagName())) {
            String moduleId = (String) configuration.getAttribute(DynamicModuleAdapter.ATTR_ID);
            if (StringUtils.isEmpty(moduleId)) {
                throw new Exception("Module id is empty!");
            }
            moduleMap.put(moduleId, configuration);
        }
    }
}
Also used : IDynamicConfiguration(org.talend.core.runtime.dynamic.IDynamicConfiguration) IDynamicExtension(org.talend.core.runtime.dynamic.IDynamicExtension)

Example 4 with IDynamicExtension

use of org.talend.core.runtime.dynamic.IDynamicExtension in project tbd-studio-se by Talend.

the class DynamicLibraryNeededExtensionAdaper method adapt.

public IDynamicExtension adapt(IDynamicMonitor monitor) throws Exception {
    DynamicDistributionUtils.checkCancelOrNot(monitor);
    resolve();
    TemplateBean templateBean = getTemplateBean();
    DynamicConfiguration configuration = getConfiguration();
    String templateId = templateBean.getId();
    String distributionName = configuration.getDistribution();
    String id = configuration.getId();
    IDynamicExtension libNeededExtension = DynamicFactory.getInstance().createDynamicExtension();
    libNeededExtension.setExtensionId(DynamicDistributionUtils.getExtensionId(DynamicDistributionUtils.getPluginKey(distributionName, templateId, id, ATTR_POINT)));
    libNeededExtension.setExtensionPoint(ATTR_POINT);
    List<ModuleBean> modules = templateBean.getModules();
    if (modules != null) {
        try {
            Exception[] ex = new Exception[1];
            Set<String> registedModules = Collections.synchronizedSet(new LinkedHashSet<>());
            for (ModuleBean moduleBean : modules) {
                DynamicDistributionUtils.checkCancelOrNot(monitor);
                Runnable runnable = new Runnable() {

                    int count = 0;

                    @Override
                    public void run() {
                        try {
                            DynamicModuleAdapter dynamicModuleAdapter = new DynamicModuleAdapter(templateBean, configuration, moduleBean, dependencyResolver, registedModules);
                            List<IDynamicConfiguration> librariesNeeded = dynamicModuleAdapter.adapt(monitor, isEnableMultiThread());
                            if (librariesNeeded != null && !librariesNeeded.isEmpty()) {
                                addDynamicConfigurations(libNeededExtension, librariesNeeded);
                            }
                            String beanId = moduleBean.getId();
                            moduleBeanAdapterMap.put(beanId, dynamicModuleAdapter);
                        } catch (Exception e) {
                            Throwable rootCause = ExceptionUtils.getRootCause(e);
                            if (e instanceof FileNotFoundException || rootCause instanceof FileNotFoundException) {
                                if (count < 5) {
                                    count++;
                                    run();
                                } else {
                                    ex[0] = e;
                                }
                            } else {
                                ex[0] = e;
                            }
                        }
                    }
                };
                if (isEnableMultiThread()) {
                    getThreadPool().execute(runnable);
                    continue;
                } else {
                    runnable.run();
                    if (ex[0] != null) {
                        throw ex[0];
                    }
                }
            }
            if (isEnableMultiThread()) {
                int totalTasks = modules.size();
                if (monitor != null) {
                    monitor.beginTask(// $NON-NLS-1$
                    Messages.getString("DynamicLibraryNeededExtensionAdaper.monitor.waitAllFinish", totalTasks), totalTasks);
                }
                int completed = 0;
                while (true) {
                    int activeCount = getThreadPool().getActiveCount();
                    if (activeCount <= 0) {
                        break;
                    }
                    if (monitor != null) {
                        int newCompleted = (int) getThreadPool().getCompletedTaskCount();
                        int newWorked = newCompleted - completed;
                        completed = newCompleted;
                        monitor.setTaskName(// $NON-NLS-1$
                        Messages.getString(// $NON-NLS-1$
                        "DynamicLibraryNeededExtensionAdaper.monitor.waitAllFinish", activeCount));
                        monitor.worked(newWorked);
                    }
                    Thread.sleep(200);
                    DynamicDistributionUtils.checkCancelOrNot(monitor);
                    if (ex[0] != null) {
                        throw ex[0];
                    }
                }
            }
        } finally {
            if (isEnableMultiThread()) {
                if (monitor != null) {
                    // $NON-NLS-1$
                    monitor.beginTask("", IProgressMonitor.UNKNOWN);
                }
                getThreadPool().clearThreads();
                threadPool = null;
            }
        }
    }
    List<ModuleGroupBean> moduleGroups = templateBean.getModuleGroups();
    if (moduleGroups != null) {
        for (ModuleGroupBean moduleGroupBean : moduleGroups) {
            DynamicDistributionUtils.checkCancelOrNot(monitor);
            DynamicModuleGroupAdapter libNeededGroupAdapter = new DynamicModuleGroupAdapter(templateBean, configuration, moduleGroupBean, moduleBeanAdapterMap);
            IDynamicConfiguration dynamicModuleGroup = libNeededGroupAdapter.adapt(monitor);
            if (dynamicModuleGroup != null) {
                libNeededExtension.addConfiguration(dynamicModuleGroup);
                String groupId = moduleGroupBean.getId();
                moduleGroupBeanAdapterMap.put(groupId, libNeededGroupAdapter);
            }
        }
    }
    return libNeededExtension;
}
Also used : IDynamicConfiguration(org.talend.core.runtime.dynamic.IDynamicConfiguration) ModuleGroupBean(org.talend.hadoop.distribution.dynamic.bean.ModuleGroupBean) FileNotFoundException(java.io.FileNotFoundException) FileNotFoundException(java.io.FileNotFoundException) IDynamicConfiguration(org.talend.core.runtime.dynamic.IDynamicConfiguration) DynamicConfiguration(org.talend.hadoop.distribution.dynamic.DynamicConfiguration) IDynamicExtension(org.talend.core.runtime.dynamic.IDynamicExtension) ModuleBean(org.talend.hadoop.distribution.dynamic.bean.ModuleBean) TemplateBean(org.talend.hadoop.distribution.dynamic.bean.TemplateBean)

Example 5 with IDynamicExtension

use of org.talend.core.runtime.dynamic.IDynamicExtension in project tbd-studio-se by Talend.

the class DynamicDistributionDetailsForm method initTableViewData.

private void initTableViewData(IDynamicPlugin dynamicPlugin) {
    if (dynamicPlugin == null) {
        baseJarsTable.setInput(null);
    } else {
        IDynamicExtension libNeededExtension = DynamicPluginAdapter.getLibraryNeededExtension(dynamicPlugin);
        List<IDynamicConfiguration> configurations = libNeededExtension.getConfigurations();
        Iterator<IDynamicConfiguration> iter = configurations.iterator();
        List<IDynamicConfiguration> moduleGroups = new ArrayList<>();
        while (iter.hasNext()) {
            IDynamicConfiguration dynConfig = iter.next();
            if (DynamicModuleGroupAdapter.TAG_NAME.equals(dynConfig.getTagName())) {
                moduleGroups.add(dynConfig);
            }
        }
        Collections.sort(moduleGroups, new DynamicAttributeComparator());
        baseJarsTable.setInput(moduleGroups);
    }
}
Also used : IDynamicConfiguration(org.talend.core.runtime.dynamic.IDynamicConfiguration) IDynamicExtension(org.talend.core.runtime.dynamic.IDynamicExtension) ArrayList(java.util.ArrayList) DynamicAttributeComparator(org.talend.hadoop.distribution.dynamic.comparator.DynamicAttributeComparator)

Aggregations

IDynamicExtension (org.talend.core.runtime.dynamic.IDynamicExtension)9 IDynamicConfiguration (org.talend.core.runtime.dynamic.IDynamicConfiguration)7 MavenArtifact (org.talend.core.runtime.maven.MavenArtifact)3 DynamicConfiguration (org.talend.hadoop.distribution.dynamic.DynamicConfiguration)3 TemplateBean (org.talend.hadoop.distribution.dynamic.bean.TemplateBean)3 DynamicAttributeComparator (org.talend.hadoop.distribution.dynamic.comparator.DynamicAttributeComparator)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 FileNotFoundException (java.io.FileNotFoundException)1 IDynamicPluginConfiguration (org.talend.core.runtime.dynamic.IDynamicPluginConfiguration)1 DynamicDistributionManager (org.talend.hadoop.distribution.dynamic.DynamicDistributionManager)1 ClassLoaderBean (org.talend.hadoop.distribution.dynamic.bean.ClassLoaderBean)1 ModuleBean (org.talend.hadoop.distribution.dynamic.bean.ModuleBean)1 ModuleGroupBean (org.talend.hadoop.distribution.dynamic.bean.ModuleGroupBean)1 IDependencyResolver (org.talend.hadoop.distribution.dynamic.resolver.IDependencyResolver)1