001/**
002 * Copyright (C) 2006-2025 Talend Inc. - www.talend.com
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.talend.sdk.component.runtime.manager.extension;
017
018import static java.util.Collections.emptyMap;
019
020import java.lang.annotation.Annotation;
021import java.lang.reflect.Type;
022import java.util.Arrays;
023import java.util.HashMap;
024import java.util.HashSet;
025import java.util.Map;
026import java.util.Optional;
027import java.util.Set;
028import java.util.stream.Stream;
029
030import org.talend.sdk.component.api.input.Emitter;
031import org.talend.sdk.component.api.input.PartitionMapper;
032import org.talend.sdk.component.api.processor.Processor;
033import org.talend.sdk.component.api.service.schema.FixedSchema;
034import org.talend.sdk.component.api.standalone.DriverRunner;
035import org.talend.sdk.component.runtime.output.Branches;
036import org.talend.sdk.component.spi.component.ComponentMetadataEnricher;
037
038public class ComponentSchemaEnricher implements ComponentMetadataEnricher {
039
040    public static final String FIXED_SCHEMA_META_PREFIX = "tcomp::ui::schema::fixed";
041
042    public static final String FIXED_SCHEMA_FLOWS_META_PREFIX = "tcomp::ui::schema::flows::fixed";
043
044    private static final Set<Class<? extends Annotation>> SUPPORTED_ANNOTATIONS =
045            new HashSet<>(Arrays.asList(PartitionMapper.class, Emitter.class, Processor.class, DriverRunner.class));
046
047    @Override
048    public Map<String, String> onComponent(final Type type, final Annotation[] annotations) {
049        if (Stream.of(annotations).map(Annotation::annotationType).noneMatch(SUPPORTED_ANNOTATIONS::contains)) {
050            return emptyMap();
051        }
052
053        final Optional<FixedSchema> fixed = Arrays.stream(annotations)
054                .filter(a -> a.annotationType().equals(FixedSchema.class))
055                .findFirst()
056                .map(FixedSchema.class::cast);
057
058        if (!fixed.isPresent()) {
059            return emptyMap();
060        }
061
062        final FixedSchema fixedSchema = fixed.get();
063        final Map<String, String> metadata = new HashMap<>();
064        metadata.put(FIXED_SCHEMA_META_PREFIX, fixedSchema.value());
065        if (fixedSchema.flows().length > 0) {
066            metadata.put(FIXED_SCHEMA_FLOWS_META_PREFIX, String.join(",", fixedSchema.flows()));
067        } else {
068            metadata.put(FIXED_SCHEMA_FLOWS_META_PREFIX, Branches.DEFAULT_BRANCH);
069        }
070
071        return metadata;
072    }
073}