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);
}
}
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);
}
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);
}
}
}
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);
}
}
}
Aggregations