Search in sources :

Example 1 with Version

use of org.talend.sdk.component.api.component.Version 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)

Example 2 with Version

use of org.talend.sdk.component.api.component.Version in project component-runtime by Talend.

the class RepositoryModelBuilder method createConfig.

private Config createConfig(final ComponentManager.AllServices services, final ParameterMeta config, final String familyName, final String familyIcon, final MigrationHandlerFactory migrationHandlerFactory) {
    final Config c = new Config();
    c.setIcon(familyIcon);
    c.setKey(getKey(familyName, config.getMetadata()));
    c.setMeta(config);
    c.setId(IdGenerator.get(c.getKey().getFamily(), c.getKey().getConfigType(), c.getKey().getConfigName()));
    if (Class.class.isInstance(config.getJavaType())) {
        final Class<?> clazz = Class.class.cast(config.getJavaType());
        final Version version = clazz.getAnnotation(Version.class);
        if (version != null) {
            c.setVersion(version.value());
            if (version.migrationHandler() != MigrationHandler.class) {
                c.setMigrationHandler(migrationHandlerFactory.findMigrationHandler(config.getNestedParameters(), clazz, services));
            }
        } else {
            c.setVersion(-1);
        }
    }
    if (c.getMigrationHandler() == null) {
        c.setMigrationHandler((v, d) -> d);
    }
    return c;
}
Also used : Version(org.talend.sdk.component.api.component.Version)

Aggregations

Version (org.talend.sdk.component.api.component.Version)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.emptyMap (java.util.Collections.emptyMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional.ofNullable (java.util.Optional.ofNullable)1 Function (java.util.function.Function)1 Collectors.toMap (java.util.stream.Collectors.toMap)1 Stream (java.util.stream.Stream)1 AllArgsConstructor (lombok.AllArgsConstructor)1 Slf4j (lombok.extern.slf4j.Slf4j)1 MigrationHandler (org.talend.sdk.component.api.component.MigrationHandler)1 InvocationExceptionWrapper.toRuntimeException (org.talend.sdk.component.runtime.base.lang.exception.InvocationExceptionWrapper.toRuntimeException)1 ComponentManager (org.talend.sdk.component.runtime.manager.ComponentManager)1 ParameterMeta (org.talend.sdk.component.runtime.manager.ParameterMeta)1