Search in sources :

Example 1 with AtlasConverter

use of io.atlasmap.api.AtlasConverter in project atlasmap by atlasmap.

the class DefaultAtlasConversionService method loadConverters.

@SuppressWarnings("rawtypes")
private void loadConverters() {
    ClassLoader classLoader = this.getClass().getClassLoader();
    final ServiceLoader<AtlasConverter> converterServiceLoader = ServiceLoader.load(AtlasConverter.class, classLoader);
    // used to load up methods first;
    Map<ConverterKey, ConverterMethodHolder> methodsLoadMap = new LinkedHashMap<>();
    Map<ConverterKey, ConverterMethodHolder> customMethodsLoadMap = new LinkedHashMap<>();
    for (final AtlasConverter<?> atlasConverter : converterServiceLoader) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Loading converter : " + atlasConverter.getClass().getCanonicalName());
        }
        boolean inbuiltConverter = atlasConverter.getClass().getPackage().getName().startsWith("io.atlasmap");
        Class<?> klass = atlasConverter.getClass();
        // collect all the specific conversion methods on the class
        while (klass != Object.class) {
            final List<Method> allMethods = new ArrayList<>(Arrays.asList(klass.getDeclaredMethods()));
            for (final Method method : allMethods) {
                // also filter out methods which are synthetic methods to avoid duplicates
                if (method.isAnnotationPresent(AtlasConversionInfo.class) && method.getParameters().length > 0 && !method.isSynthetic()) {
                    String sourceClassName = method.getParameters()[0].getType().getCanonicalName();
                    ConverterKey coordinate = new ConverterKey(sourceClassName, method.getReturnType().getCanonicalName());
                    // if the method has three arguments and the last two as strings then they used
                    // as the format attributes
                    boolean containsFormat = false;
                    if (method.getParameters().length == 3 && method.getParameters()[1].getType() == String.class && method.getParameters()[2].getType() == String.class) {
                        containsFormat = true;
                    }
                    boolean staticMethod = Modifier.isStatic(method.getModifiers());
                    ConverterMethodHolder methodHolder = new ConverterMethodHolder(atlasConverter, method, staticMethod, containsFormat);
                    if (inbuiltConverter) {
                        if (!methodsLoadMap.containsKey(coordinate)) {
                            methodsLoadMap.put(coordinate, methodHolder);
                        } else {
                            LOG.warn("Converter between " + sourceClassName + " and " + method.getReturnType().getCanonicalName() + " aleady exists.");
                        }
                    } else {
                        if (!customMethodsLoadMap.containsKey(coordinate)) {
                            customMethodsLoadMap.put(coordinate, methodHolder);
                        } else {
                            LOG.warn("Custom converter between " + sourceClassName + " and " + method.getReturnType().getCanonicalName() + " aleady exists.");
                        }
                    }
                }
            }
            // move to the upper class in the hierarchy in search for more methods
            klass = klass.getSuperclass();
        }
    }
    if (!methodsLoadMap.isEmpty()) {
        converterMethods = Collections.unmodifiableMap(methodsLoadMap);
    }
    if (!methodsLoadMap.isEmpty()) {
        customConverterMethods = Collections.unmodifiableMap(customMethodsLoadMap);
    }
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) LinkedHashMap(java.util.LinkedHashMap) AtlasConverter(io.atlasmap.api.AtlasConverter)

Example 2 with AtlasConverter

use of io.atlasmap.api.AtlasConverter in project atlasmap by atlasmap.

the class DefaultAtlasConversionServiceTest method findMatchingConverterByFieldTypes.

@Test
public void findMatchingConverterByFieldTypes() {
    assertNotNull(service);
    Optional<AtlasConverter<?>> atlasConverter = service.findMatchingConverter(FieldType.STRING, FieldType.BOOLEAN);
    assertTrue(atlasConverter.isPresent());
    assertNotNull(atlasConverter);
    assertEquals(StringConverter.class, atlasConverter.get().getClass());
    StringConverter stringConverter = (StringConverter) atlasConverter.get();
    assertNotNull(stringConverter);
    assertThat("io.atlasmap.converters.StringConverter", is(atlasConverter.get().getClass().getCanonicalName()));
    Boolean t = stringConverter.toBoolean("T", null, null);
    assertNotNull(t);
    assertTrue(t);
    Boolean f = stringConverter.toBoolean("F", null, null);
    assertNotNull(f);
    assertFalse(f);
    service.findMatchingConverter(null, FieldType.BOOLEAN);
    service.findMatchingConverter(FieldType.STRING, null);
    FieldType fieldType = null;
    service.findMatchingConverter(fieldType, fieldType);
}
Also used : AtlasConverter(io.atlasmap.api.AtlasConverter) StringConverter(io.atlasmap.converters.StringConverter) FieldType(io.atlasmap.v2.FieldType) Test(org.junit.Test)

Example 3 with AtlasConverter

use of io.atlasmap.api.AtlasConverter in project atlasmap by atlasmap.

the class JavaValidationService method validateClassConversion.

private void validateClassConversion(String mappingId, JavaField inputField, JavaField outField, List<Validation> validations) {
    Optional<AtlasConverter<?>> atlasConverter = getConversionService().findMatchingConverter(inputField.getClassName(), outField.getClassName());
    if (!atlasConverter.isPresent()) {
        Validation validation = new Validation();
        validation.setScope(ValidationScope.MAPPING);
        validation.setId(mappingId);
        validation.setMessage(String.format("Conversion from '%s' to '%s' is required but no converter is available", inputField.getClassName(), outField.getClassName()));
        validation.setStatus(ValidationStatus.WARN);
        validations.add(validation);
    } else {
        AtlasConversionInfo conversionInfo;
        // find the method that does the conversion
        Method[] methods = atlasConverter.get().getClass().getMethods();
        conversionInfo = Arrays.stream(methods).map(method -> method.getAnnotation(AtlasConversionInfo.class)).filter(atlasConversionInfo -> atlasConversionInfo != null).filter(atlasConversionInfo -> atlasConversionInfo.sourceType().compareTo(inputField.getFieldType()) == 0 && atlasConversionInfo.targetType().compareTo(outField.getFieldType()) == 0).findFirst().orElse(null);
        if (conversionInfo != null) {
            populateConversionConcerns(mappingId, conversionInfo, getFieldName(inputField), getFieldName(outField), validations);
        }
    }
}
Also used : Validation(io.atlasmap.v2.Validation) DataInputStream(java.io.DataInputStream) Arrays(java.util.Arrays) FieldDirection(io.atlasmap.spi.FieldDirection) ValidationScope(io.atlasmap.v2.ValidationScope) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) AtlasValidator(io.atlasmap.spi.AtlasValidator) AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo) BaseModuleValidationService(io.atlasmap.core.BaseModuleValidationService) NonNullValidator(io.atlasmap.validators.NonNullValidator) FieldType(io.atlasmap.v2.FieldType) Validation(io.atlasmap.v2.Validation) Field(io.atlasmap.v2.Field) Map(java.util.Map) AtlasConversionService(io.atlasmap.api.AtlasConversionService) Method(java.lang.reflect.Method) AtlasConverter(io.atlasmap.api.AtlasConverter) Logger(org.slf4j.Logger) IOException(java.io.IOException) ValidationStatus(io.atlasmap.v2.ValidationStatus) List(java.util.List) AtlasModuleDetail(io.atlasmap.spi.AtlasModuleDetail) JavaField(io.atlasmap.java.v2.JavaField) Optional(java.util.Optional) InputStream(java.io.InputStream) AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo) AtlasConverter(io.atlasmap.api.AtlasConverter) Method(java.lang.reflect.Method)

Example 4 with AtlasConverter

use of io.atlasmap.api.AtlasConverter in project atlasmap by atlasmap.

the class BaseModuleValidationService method validateFieldTypeConversion.

protected void validateFieldTypeConversion(String mappingId, Field inputField, Field outField, List<Validation> validations) {
    FieldType inFieldType = inputField.getFieldType();
    FieldType outFieldType = outField.getFieldType();
    Optional<AtlasConverter<?>> atlasConverter = conversionService.findMatchingConverter(inFieldType, outFieldType);
    if (!atlasConverter.isPresent()) {
        Validation validation = new Validation();
        validation.setScope(ValidationScope.MAPPING);
        validation.setId(mappingId);
        validation.setMessage(String.format("Conversion from '%s' to '%s' is required but no converter is available", inputField.getFieldType(), outField.getFieldType()));
        validation.setStatus(ValidationStatus.ERROR);
        validations.add(validation);
    } else {
        AtlasConversionInfo conversionInfo;
        // find the method that does the conversion
        Method[] methods = atlasConverter.get().getClass().getMethods();
        conversionInfo = Arrays.stream(methods).map(method -> method.getAnnotation(AtlasConversionInfo.class)).filter(atlasConversionInfo -> atlasConversionInfo != null).filter(atlasConversionInfo -> (atlasConversionInfo.sourceType().compareTo(inFieldType) == 0 && atlasConversionInfo.targetType().compareTo(outFieldType) == 0)).findFirst().orElse(null);
        if (conversionInfo != null) {
            populateConversionConcerns(mappingId, conversionInfo, getFieldName(inputField), getFieldName(outField), validations);
        }
    }
}
Also used : Validation(io.atlasmap.v2.Validation) Arrays(java.util.Arrays) FieldDirection(io.atlasmap.spi.FieldDirection) ValidationScope(io.atlasmap.v2.ValidationScope) DataSource(io.atlasmap.v2.DataSource) MappingType(io.atlasmap.v2.MappingType) AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo) ValidationStatus(io.atlasmap.v2.ValidationStatus) AtlasConversionConcern(io.atlasmap.spi.AtlasConversionConcern) FieldType(io.atlasmap.v2.FieldType) Validation(io.atlasmap.v2.Validation) ArrayList(java.util.ArrayList) Mapping(io.atlasmap.v2.Mapping) List(java.util.List) Field(io.atlasmap.v2.Field) AtlasModuleDetail(io.atlasmap.spi.AtlasModuleDetail) AtlasMapping(io.atlasmap.v2.AtlasMapping) AtlasModuleMode(io.atlasmap.spi.AtlasModuleMode) Optional(java.util.Optional) AtlasConversionService(io.atlasmap.api.AtlasConversionService) Method(java.lang.reflect.Method) BaseMapping(io.atlasmap.v2.BaseMapping) AtlasConverter(io.atlasmap.api.AtlasConverter) AtlasValidationService(io.atlasmap.api.AtlasValidationService) AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo) AtlasConverter(io.atlasmap.api.AtlasConverter) Method(java.lang.reflect.Method) FieldType(io.atlasmap.v2.FieldType)

Aggregations

AtlasConverter (io.atlasmap.api.AtlasConverter)4 FieldType (io.atlasmap.v2.FieldType)3 Method (java.lang.reflect.Method)3 AtlasConversionService (io.atlasmap.api.AtlasConversionService)2 AtlasConversionInfo (io.atlasmap.spi.AtlasConversionInfo)2 AtlasModuleDetail (io.atlasmap.spi.AtlasModuleDetail)2 FieldDirection (io.atlasmap.spi.FieldDirection)2 Field (io.atlasmap.v2.Field)2 Validation (io.atlasmap.v2.Validation)2 ValidationScope (io.atlasmap.v2.ValidationScope)2 ValidationStatus (io.atlasmap.v2.ValidationStatus)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Optional (java.util.Optional)2 AtlasValidationService (io.atlasmap.api.AtlasValidationService)1 StringConverter (io.atlasmap.converters.StringConverter)1 BaseModuleValidationService (io.atlasmap.core.BaseModuleValidationService)1 JavaField (io.atlasmap.java.v2.JavaField)1 AtlasConversionConcern (io.atlasmap.spi.AtlasConversionConcern)1