Compare commits

...

1 Commits

Author SHA1 Message Date
Chao MENG
0587f4fa17 feat(TUP-29653): Improve Tacokit loading
https://jira.talendforge.org/browse/TUP-29653
2020-12-17 11:43:51 +08:00
3 changed files with 141 additions and 6 deletions

View File

@@ -34,18 +34,56 @@ import org.talend.updates.runtime.service.ITaCoKitUpdateService;
public class TaCoKitService implements ITaCoKitService {
private final Object startLock = new Object();
@Override
public void start() throws Exception {
ServerManager.getInstance().start();
boolean isComponentsConfigChanged = true;
try {
TaCoKitUtil.registAllTaCoKitRepositoryTypes();
isComponentsConfigChanged = TaCoKitUtil.isComponentsConfigChanged();
} catch (Exception e) {
Exception ex = Lookups.manager().getLoadException();
if (ex == null) {
Lookups.manager().setLoadException(e);
}
ExceptionHandler.process(e);
}
if (isComponentsConfigChanged) {
startInner();
} else {
new Thread(new Runnable() {
@Override
public void run() {
try {
startInner();
} catch (Exception e) {
ExceptionHandler.process(e);
}
}
}, "Starting TaCoKit in background...").start();
}
}
private void startInner() throws Exception {
if (isStarted()) {
return;
}
synchronized (startLock) {
if (!isStarted()) {
ServerManager.getInstance().start();
try {
TaCoKitUtil.registAllTaCoKitRepositoryTypes();
} catch (Exception e) {
Exception ex = Lookups.manager().getLoadException();
if (ex == null) {
Lookups.manager().setLoadException(e);
}
ExceptionHandler.process(e);
}
}
}
}
@Override
public void updateComponentsCache() throws Exception {
TaCoKitUtil.updateComponentConfigCache();
}
@Override

View File

@@ -71,4 +71,9 @@ public class TaCoKitConst {
public static final String MAVEN_INF = "MAVEN-INF"; //$NON-NLS-1$
public static final String TALEND_INF = "TALEND-INF"; //$NON-NLS-1$
public static final String PREF_CACHE_COMPONENT_COORDINATES = PROP_COMPONENT;
public static final String PREF_CACHE_COMPONENT_REGISTRY = PROP_REGISTRY;
}

View File

@@ -12,6 +12,7 @@
*/
package org.talend.sdk.component.studio.util;
import java.io.BufferedReader;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
@@ -33,6 +34,8 @@ import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.EList;
import org.eclipse.jface.preference.IPersistentPreferenceStore;
import org.eclipse.jface.preference.IPreferenceStore;
import org.talend.commons.utils.data.container.Container;
import org.talend.commons.utils.system.EnvironmentUtils;
import org.talend.core.model.components.ComponentCategory;
@@ -54,6 +57,7 @@ import org.talend.sdk.component.server.front.model.ConfigTypeNode;
import org.talend.sdk.component.server.front.model.ConfigTypeNodes;
import org.talend.sdk.component.studio.ComponentModel;
import org.talend.sdk.component.studio.Lookups;
import org.talend.sdk.component.studio.SdkComponentPlugin;
import org.talend.sdk.component.studio.metadata.TaCoKitCache;
import org.talend.sdk.component.studio.metadata.WizardRegistry;
import org.talend.sdk.component.studio.metadata.model.TaCoKitConfigurationModel;
@@ -502,6 +506,94 @@ public class TaCoKitUtil {
.map(ComponentModel.class::cast);
}
public static boolean isComponentsConfigChanged() throws Exception {
IPreferenceStore prefStore = SdkComponentPlugin.getDefault().getPreferenceStore();
String cachedCoordinatesStr = prefStore.getString(TaCoKitConst.PREF_CACHE_COMPONENT_COORDINATES);
String cachedRegistryStr = prefStore.getString(TaCoKitConst.PREF_CACHE_COMPONENT_REGISTRY);
if (StringUtils.isBlank(cachedCoordinatesStr) && StringUtils.isBlank(cachedRegistryStr)) {
return true;
}
final String EMPTY = "";
if (cachedCoordinatesStr == null) {
cachedCoordinatesStr = EMPTY;
}
String[] cachedCoordinatesArr = cachedCoordinatesStr.split(TaCoKitConst.PROP_COMPONENT_SEPARATOR);
Set<String> cachedComponents = new HashSet<>();
for (String coordinate : cachedCoordinatesArr) {
if (StringUtils.isNotBlank(coordinate)) {
cachedComponents.add(coordinate);
}
}
if (cachedRegistryStr == null) {
cachedRegistryStr = EMPTY;
}
String[] cachedRegistryArr = cachedRegistryStr.split(TaCoKitConst.PROP_COMPONENT_SEPARATOR);
for (String registry : cachedRegistryArr) {
if (StringUtils.isNotBlank(registry)) {
cachedComponents.add(registry);
}
}
String coordinatesStr = System.getProperty(TaCoKitConst.PROP_COMPONENT);
Properties componentRegistry = TaCoKitUtil.loadComponentRegistry();
if (StringUtils.isBlank(coordinatesStr) && (componentRegistry == null || componentRegistry.isEmpty())) {
return false;
}
if (coordinatesStr == null) {
coordinatesStr = EMPTY;
}
String[] coordinatesArr = coordinatesStr.split(TaCoKitConst.PROP_COMPONENT_SEPARATOR);
Set<String> installedComponents = new HashSet<>();
for (String coordinate : coordinatesArr) {
if (StringUtils.isNotBlank(coordinate)) {
installedComponents.add(coordinate);
}
}
if (componentRegistry != null) {
for (Object compObj : componentRegistry.values()) {
if (compObj != null) {
String component = (String) compObj;
if (StringUtils.isNotBlank(component)) {
installedComponents.add(component);
}
}
}
}
boolean equals = cachedComponents.size() == installedComponents.size()
&& cachedComponents.containsAll(installedComponents);
return !equals;
}
public static void updateComponentConfigCache() throws Exception {
IPreferenceStore prefStore = SdkComponentPlugin.getDefault().getPreferenceStore();
prefStore.setValue(TaCoKitConst.PREF_CACHE_COMPONENT_COORDINATES, System.getProperty(TaCoKitConst.PROP_COMPONENT));
StringBuilder registryComponentsStrBuilder = new StringBuilder();
Properties loadComponentRegistry = loadComponentRegistry();
if (loadComponentRegistry != null) {
for (Object obj : loadComponentRegistry.values()) {
if (obj != null) {
registryComponentsStrBuilder.append(obj.toString()).append(TaCoKitConst.PROP_COMPONENT_SEPARATOR);
}
}
}
prefStore.setValue(TaCoKitConst.PREF_CACHE_COMPONENT_REGISTRY, registryComponentsStrBuilder.toString());
if (prefStore instanceof IPersistentPreferenceStore && prefStore.needsSaving()) {
((IPersistentPreferenceStore) prefStore).save();
}
}
public static Properties loadComponentRegistry() throws Exception {
String registryPath = System.getProperty(TaCoKitConst.PROP_REGISTRY);
if (StringUtils.isBlank(registryPath)) {
return null;
}
Properties prop = new Properties();
try (BufferedReader reader = Files.newBufferedReader(new File(registryPath).toPath())) {
prop.load(reader);
}
return prop;
}
/**
* Check if <code>components</code> holds component-runtime components.
*