Search in sources :

Example 1 with ParameterMeta

use of org.talend.sdk.component.runtime.manager.ParameterMeta in project component-runtime by Talend.

the class PropertiesServiceTest method buildProperties.

@Test
void buildProperties() {
    final String[] i18nPackages = { Config.class.getPackage().getName() };
    final ParameterMeta host = new ParameterMeta(null, Config.class, ParameterMeta.Type.STRING, "configuration.host", "host", i18nPackages, emptyList(), null, emptyMap());
    final ParameterMeta port = new ParameterMeta(null, Config.class, ParameterMeta.Type.NUMBER, "configuration.port", "port", i18nPackages, emptyList(), null, emptyMap());
    final ParameterMeta username = new ParameterMeta(null, Config.class, ParameterMeta.Type.STRING, "configuration.username", "username", i18nPackages, emptyList(), null, emptyMap());
    final ParameterMeta password = new ParameterMeta(null, Config.class, ParameterMeta.Type.STRING, "configuration.password", "password", i18nPackages, emptyList(), null, emptyMap());
    final ParameterMeta config = new ParameterMeta(null, Config.class, ParameterMeta.Type.OBJECT, "configuration", "configuration", i18nPackages, asList(host, port, username, password), null, emptyMap());
    final List<SimplePropertyDefinition> props = propertiesService.buildProperties(singletonList(config), getClass().getClassLoader(), Locale.getDefault(), null).collect(toList());
    assertEquals(5, props.size());
    assertEquals("Configuration", props.get(0).getDisplayName());
    assertEquals("Server Host Name", props.get(1).getDisplayName());
    assertEquals("Enter the server host name...", props.get(1).getPlaceholder());
    assertEquals("Password", props.get(2).getDisplayName());
    assertEquals("Server Port", props.get(3).getDisplayName());
    assertEquals("Enter the server port...", props.get(3).getPlaceholder());
    assertEquals("User Name", props.get(4).getDisplayName());
}
Also used : ParameterMeta(org.talend.sdk.component.runtime.manager.ParameterMeta) SimplePropertyDefinition(org.talend.sdk.component.server.front.model.SimplePropertyDefinition) Test(org.junit.jupiter.api.Test)

Example 2 with ParameterMeta

use of org.talend.sdk.component.runtime.manager.ParameterMeta in project component-runtime by Talend.

the class TaCoKitGuessSchema method buildActionConfig.

private Map<String, String> buildActionConfig(final ServiceMeta.ActionMeta action, final Map<String, String> configuration) {
    if (configuration == null || configuration.isEmpty()) {
        // no-mapping
        return configuration;
    }
    final ParameterMeta dataSet = action.getParameters().stream().filter(param -> param.getMetadata().containsKey("tcomp::configurationtype::type") && "dataset".equals(param.getMetadata().get("tcomp::configurationtype::type"))).findFirst().orElse(null);
    if (dataSet == null) {
        // no mapping to do
        return configuration;
    }
    // action configuration prefix.
    final String prefix = dataSet.getPath();
    final int dotIndex = configuration.keySet().iterator().next().indexOf(".");
    return configuration.entrySet().stream().collect(toMap(e -> prefix + "." + e.getKey().substring(dotIndex + 1, e.getKey().length()), Map.Entry::getValue));
}
Also used : ParameterMeta(org.talend.sdk.component.runtime.manager.ParameterMeta) IntStream(java.util.stream.IntStream) HashMap(java.util.HashMap) Modifier.isStatic(java.lang.reflect.Modifier.isStatic) Type(org.talend.sdk.component.api.service.schema.Type) Schema(org.talend.sdk.component.api.service.schema.Schema) HashSet(java.util.HashSet) JsonValue(javax.json.JsonValue) Collectors.toMap(java.util.stream.Collectors.toMap) ElementListener(org.talend.sdk.component.api.processor.ElementListener) Map(java.util.Map) Input(org.talend.sdk.component.runtime.input.Input) JobStateAware(org.talend.sdk.component.runtime.di.JobStateAware) Output(org.talend.sdk.component.api.processor.Output) ParameterMeta(org.talend.sdk.component.runtime.manager.ParameterMeta) PrintStream(java.io.PrintStream) JsonObject(javax.json.JsonObject) ContainerComponentRegistry(org.talend.sdk.component.runtime.manager.ContainerComponentRegistry) Collection(java.util.Collection) Set(java.util.Set) Delegated(org.talend.sdk.component.runtime.base.Delegated) Field(java.lang.reflect.Field) ChainedMapper(org.talend.sdk.component.runtime.manager.chain.ChainedMapper) Processor(org.talend.sdk.component.runtime.output.Processor) OutputEmitter(org.talend.sdk.component.api.processor.OutputEmitter) Mapper(org.talend.sdk.component.runtime.input.Mapper) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) ParameterizedType(java.lang.reflect.ParameterizedType) Optional(java.util.Optional) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) ServiceMeta(org.talend.sdk.component.runtime.manager.ServiceMeta) HashMap(java.util.HashMap) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map)

Example 3 with ParameterMeta

use of org.talend.sdk.component.runtime.manager.ParameterMeta in project component-runtime by Talend.

the class ConfigurationMapper method map.

private Map<String, String> map(final List<ParameterMeta> nestedParameters, final Object instance, final Map<Integer, Integer> indexes) {
    if (nestedParameters == null) {
        return emptyMap();
    }
    return nestedParameters.stream().map(param -> {
        final Object value = getValue(instance, param.getName());
        if (value == null) {
            return Collections.<String, String>emptyMap();
        }
        switch(param.getType()) {
            case OBJECT:
                return map(param.getNestedParameters(), value, indexes);
            case ARRAY:
                final Collection<Object> values = Collection.class.isInstance(value) ? Collection.class.cast(value) : /* array */
                asList(Object[].class.cast(value));
                final int arrayIndex = indexes.keySet().size();
                final AtomicInteger valuesIndex = new AtomicInteger(0);
                final Map<String, String> config = values.stream().map((Object item) -> {
                    indexes.put(arrayIndex, valuesIndex.getAndIncrement());
                    final Map<String, String> res = param.getNestedParameters().stream().filter(ConfigurationMapper::isPrimitive).collect(toMap(p -> evaluateIndexes(p.getPath(), indexes), p -> getValue(item, p.getName()).toString()));
                    res.putAll(map(param.getNestedParameters().stream().filter(p -> !isPrimitive(p)).collect(toList()), item, indexes));
                    return res;
                }).flatMap(m -> m.entrySet().stream()).collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
                // clear index after the end of array handling
                indexes.clear();
                return config;
            default:
                // primitives
                return singletonMap(evaluateIndexes(param.getPath(), indexes), value.toString());
        }
    }).flatMap(m -> m.entrySet().stream()).collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
}
Also used : Collections.emptyMap(java.util.Collections.emptyMap) Collection(java.util.Collection) HashMap(java.util.HashMap) Field(java.lang.reflect.Field) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Stream(java.util.stream.Stream) Collectors.toMap(java.util.stream.Collectors.toMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) Collections(java.util.Collections) ParameterMeta(org.talend.sdk.component.runtime.manager.ParameterMeta) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Collection(java.util.Collection) Collections.emptyMap(java.util.Collections.emptyMap) HashMap(java.util.HashMap) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap)

Example 4 with ParameterMeta

use of org.talend.sdk.component.runtime.manager.ParameterMeta in project component-runtime by Talend.

the class PropertiesServiceTest method validateProp.

// the class BaseConfig don't contains attribute
@Test
void validateProp() {
    assertThrows(IllegalArgumentException.class, () -> {
        ParameterMeta attribute = new ParameterMeta(null, BadConfig.class, ParameterMeta.Type.STRING, "configuration.attribute", "attribute", null, emptyList(), null, emptyMap());
        ParameterMeta config = new ParameterMeta(null, Config.class, ParameterMeta.Type.OBJECT, "configuration", "configuration", null, singletonList(attribute), null, emptyMap());
        propertiesService.buildProperties(singletonList(config), getClass().getClassLoader(), Locale.ROOT, null).forEach(Objects::nonNull);
    });
}
Also used : ParameterMeta(org.talend.sdk.component.runtime.manager.ParameterMeta) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test)

Example 5 with ParameterMeta

use of org.talend.sdk.component.runtime.manager.ParameterMeta in project component-runtime by Talend.

the class MigrationHandlerFactory method findMigrationHandler.

public MigrationHandler findMigrationHandler(final List<ParameterMeta> parameterMetas, final Class<?> type, final ComponentManager.AllServices services) {
    final MigrationHandler implicitMigrationHandler = ofNullable(parameterMetas).map(Collection::stream).orElseGet(Stream::empty).filter(p -> p.getMetadata().keySet().stream().anyMatch(k -> k.startsWith("tcomp::configurationtype::"))).filter(p -> Class.class.isInstance(p.getJavaType())).map(p -> {
        // for now we can assume it is not in arrays
        final MigrationHandler handler = findMigrationHandler(emptyList(), Class.class.cast(p.getJavaType()), services);
        if (handler == NO_MIGRATION) {
            return null;
        }
        final String prefix = p.getPath();
        return (Function<Map<String, String>, Map<String, String>>) map -> {
            final String version = map.get(String.format("%s.__version", prefix));
            final Map<String, String> result;
            if (version != null) {
                final Map<String, String> migrated = ofNullable(handler.migrate(Integer.parseInt(version.trim()), map.entrySet().stream().filter(e -> e.getKey().startsWith(prefix + '.')).collect(toMap(e -> e.getKey().substring(prefix.length() + 1), Map.Entry::getValue)))).orElseGet(Collections::emptyMap);
                result = migrated.entrySet().stream().collect(toMap(e -> prefix + '.' + e.getKey(), Map.Entry::getValue));
            } else {
                log.debug("No version for {} so skipping any potential migration", p.getJavaType().toString());
                result = map;
            }
            return result;
        };
    }).filter(Objects::nonNull).reduce(NO_MIGRATION, (current, partial) -> (incomingVersion, incomingData) -> current.migrate(incomingVersion, partial.apply(incomingData)), (migrationHandler, migrationHandler2) -> (incomingVersion, incomingData) -> migrationHandler2.migrate(incomingVersion, migrationHandler.migrate(incomingVersion, incomingData)));
    return ofNullable(type.getAnnotation(Version.class)).map(Version::migrationHandler).filter(t -> t != MigrationHandler.class).flatMap(t -> Stream.of(t.getConstructors()).sorted((o1, o2) -> o2.getParameterCount() - o1.getParameterCount()).findFirst()).map(t -> services.getServices().computeIfAbsent(t.getDeclaringClass(), k -> {
        try {
            return t.newInstance(reflections.parameterFactory(t, services.getServices()).apply(emptyMap()));
        } catch (final InstantiationException | IllegalAccessException e) {
            throw new IllegalArgumentException(e);
        } catch (final InvocationTargetException e) {
            throw toRuntimeException(e);
        }
    })).map(MigrationHandler.class::cast).map(h -> {
        if (implicitMigrationHandler == NO_MIGRATION) {
            return h;
        }
        return (MigrationHandler) (incomingVersion, incomingData) -> h.migrate(incomingVersion, implicitMigrationHandler.migrate(incomingVersion, incomingData));
    }).orElse(implicitMigrationHandler);
}
Also used : Collections.emptyMap(java.util.Collections.emptyMap) Collections.emptyList(java.util.Collections.emptyList) Optional.ofNullable(java.util.Optional.ofNullable) Collection(java.util.Collection) MigrationHandler(org.talend.sdk.component.api.component.MigrationHandler) Function(java.util.function.Function) InvocationTargetException(java.lang.reflect.InvocationTargetException) Objects(java.util.Objects) Version(org.talend.sdk.component.api.component.Version) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) AllArgsConstructor(lombok.AllArgsConstructor) Collections(java.util.Collections) InvocationExceptionWrapper.toRuntimeException(org.talend.sdk.component.runtime.base.lang.exception.InvocationExceptionWrapper.toRuntimeException) ParameterMeta(org.talend.sdk.component.runtime.manager.ParameterMeta) MigrationHandler(org.talend.sdk.component.api.component.MigrationHandler) InvocationTargetException(java.lang.reflect.InvocationTargetException) Function(java.util.function.Function) Version(org.talend.sdk.component.api.component.Version) Collection(java.util.Collection) Collections(java.util.Collections)

Aggregations

ParameterMeta (org.talend.sdk.component.runtime.manager.ParameterMeta)7 Stream (java.util.stream.Stream)4 Collection (java.util.Collection)3 Collections.emptyMap (java.util.Collections.emptyMap)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 Collectors.toMap (java.util.stream.Collectors.toMap)3 Test (org.junit.jupiter.api.Test)3 ComponentManager (org.talend.sdk.component.runtime.manager.ComponentManager)3 Field (java.lang.reflect.Field)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Collections (java.util.Collections)2 Collections.emptyList (java.util.Collections.emptyList)2 Objects (java.util.Objects)2 Optional.ofNullable (java.util.Optional.ofNullable)2 Collectors.toList (java.util.stream.Collectors.toList)2 Slf4j (lombok.extern.slf4j.Slf4j)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1