Search in sources :

Example 1 with JavaEnumField

use of io.atlasmap.java.v2.JavaEnumField in project atlasmap by atlasmap.

the class DocumentJavaFieldReader method read.

@Override
public void read(AtlasInternalSession session) throws AtlasException {
    try {
        Field sourceField = session.head().getSourceField();
        Method getter = null;
        if (sourceField.getFieldType() == null && (sourceField instanceof JavaField || sourceField instanceof JavaEnumField)) {
            getter = resolveGetMethod(sourceDocument, sourceField);
            if (getter == null) {
                AtlasUtil.addAudit(session, sourceField.getDocId(), String.format("Unable to auto-detect sourceField type path=%s docId=%s", sourceField.getPath(), sourceField.getDocId()), sourceField.getPath(), AuditStatus.WARN, null);
                return;
            }
            Class<?> returnType = getter.getReturnType();
            sourceField.setFieldType(conversionService.fieldTypeFromClass(returnType));
            if (LOG.isTraceEnabled()) {
                LOG.trace("Auto-detected sourceField type p=" + sourceField.getPath() + " t=" + sourceField.getFieldType());
            }
        }
        populateSourceFieldValue(session, sourceField, sourceDocument, getter);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Processed input field sPath=" + sourceField.getPath() + " sV=" + sourceField.getValue() + " sT=" + sourceField.getFieldType() + " docId: " + sourceField.getDocId());
        }
    } catch (Exception e) {
        throw new AtlasException(e);
    }
}
Also used : JavaEnumField(io.atlasmap.java.v2.JavaEnumField) Field(io.atlasmap.v2.Field) JavaField(io.atlasmap.java.v2.JavaField) JavaEnumField(io.atlasmap.java.v2.JavaEnumField) JavaField(io.atlasmap.java.v2.JavaField) Method(java.lang.reflect.Method) AtlasException(io.atlasmap.api.AtlasException) AtlasException(io.atlasmap.api.AtlasException)

Example 2 with JavaEnumField

use of io.atlasmap.java.v2.JavaEnumField in project atlasmap by atlasmap.

the class DocumentJavaFieldWriter method getClassForField.

private Class<?> getClassForField(Field field, SegmentContext segmentContext, Object parentObject, boolean unwrapCollectionType) throws AtlasException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Looking up class to use for segment: " + segmentContext + "\n\tparentObject: " + parentObject);
    }
    Class<?> clz = null;
    if (LOG.isDebugEnabled()) {
        LOG.debug("Looking for configured class for field: " + field + ".");
    }
    String className = null;
    if (field instanceof JavaField) {
        className = ((JavaField) field).getClassName();
    } else if (field instanceof JavaEnumField) {
        className = ((JavaEnumField) field).getClassName();
    }
    if (className != null) {
        try {
            clz = className == null ? null : Class.forName(className);
        } catch (Exception e) {
            throw new AtlasException("Could not find class for '" + className + "', for segment: " + segmentContext + ", on field: " + field, e);
        }
    }
    if (clz == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Couldn't find class on field. Looking for configured class for segment: " + segmentContext + ".");
        }
        String normalizedSegment = AtlasPath.removeCollectionIndexes(segmentContext.getSegmentPath());
        clz = this.classesForFields.get(normalizedSegment);
    }
    Type clzType = null;
    if (clz == null) {
        // attempt to determine it from the parent object.
        if (LOG.isDebugEnabled()) {
            LOG.debug("Couldn't find configured class for segment: " + segmentContext + ", looking up getter method.");
        }
        Method m = null;
        try {
            String methodName = "get" + JavaWriterUtil.capitalizeFirstLetter(AtlasPath.cleanPathSegment(segmentContext.getSegment()));
            m = ClassHelper.detectGetterMethod(parentObject.getClass(), methodName);
        } catch (NoSuchMethodException e) {
            // it's ok, we didnt find a getter.
            if (LOG.isDebugEnabled()) {
                LOG.debug("Couldn't find getter method for segment: " + segmentContext, e);
            }
        }
        clz = m == null ? null : m.getReturnType();
        clzType = m.getGenericReturnType();
    }
    if (clz == null) {
        throw new AtlasException("Could not create object, can't find class to instantiate for segment: " + segmentContext);
    }
    if (unwrapCollectionType) {
        clz = unwrapCollectionType(field, segmentContext, parentObject, clz, clzType);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Found class '" + clz.getName() + "' to use for segment: " + segmentContext);
    }
    return clz;
}
Also used : JavaEnumField(io.atlasmap.java.v2.JavaEnumField) FieldType(io.atlasmap.v2.FieldType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JavaField(io.atlasmap.java.v2.JavaField) Method(java.lang.reflect.Method) AtlasException(io.atlasmap.api.AtlasException) AtlasException(io.atlasmap.api.AtlasException)

Example 3 with JavaEnumField

use of io.atlasmap.java.v2.JavaEnumField in project atlasmap by atlasmap.

the class ClassInspectionService method inspectClass.

private void inspectClass(ClassLoader classLoader, Class<?> clazz, JavaClass javaClass, Set<String> cachedClasses, String pathPrefix) {
    Class<?> clz = clazz;
    if (clazz.isArray()) {
        javaClass.setArrayDimensions(detectArrayDimensions(clazz));
        javaClass.setCollectionType(CollectionType.ARRAY);
        clz = detectArrayClass(clazz);
    } else {
        clz = clazz;
    }
    if (isMapList(clz.getCanonicalName())) {
        javaClass.setCollectionType(CollectionType.MAP);
    }
    javaClass.setClassName(clz.getCanonicalName());
    javaClass.setPackageName((clz.getPackage() != null ? clz.getPackage().getName() : null));
    javaClass.setAnnotation(clz.isAnnotation());
    javaClass.setAnnonymous(clz.isAnonymousClass());
    javaClass.setEnumeration(clz.isEnum());
    javaClass.setInterface(clz.isInterface());
    javaClass.setLocalClass(clz.isLocalClass());
    javaClass.setMemberClass(clz.isMemberClass());
    javaClass.setPrimitive(clz.isPrimitive());
    javaClass.setSynthetic(clz.isSynthetic());
    if (javaClass.getUri() == null) {
        javaClass.setUri(String.format(AtlasJavaModelFactory.URI_FORMAT, clz.getCanonicalName()));
    }
    if (clz.isPrimitive() || JdkPackages.contains(clz.getPackage().getName())) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Skipping class " + clz.getName() + " which is a Jdk core class");
        }
        return;
    }
    // Process super class fields and methods first, so child class fields
    // and methods override
    Class<?> tmpClazz = clz;
    Class<?> superClazz = tmpClazz.getSuperclass();
    while (superClazz != null) {
        if (JdkPackages.contains(superClazz.getPackage().getName())) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("Skipping SuperClass " + superClazz.getName() + " which is a Jdk core class");
            }
            superClazz = null;
        } else {
            inspectClassFields(classLoader, superClazz, javaClass, cachedClasses, pathPrefix);
            inspectClassMethods(classLoader, superClazz, javaClass, cachedClasses, pathPrefix);
            tmpClazz = superClazz;
            superClazz = tmpClazz.getSuperclass();
        }
    }
    inspectClassFields(classLoader, clz, javaClass, cachedClasses, pathPrefix);
    Object[] enumConstants = clz.getEnumConstants();
    if (enumConstants != null) {
        javaClass.setEnumeration(true);
        for (Object o : enumConstants) {
            JavaEnumField out = new JavaEnumField();
            if (o instanceof Enum) {
                Enum<?> in = (Enum<?>) o;
                out.setName(in.name());
                out.setOrdinal(in.ordinal());
                javaClass.getJavaEnumFields().getJavaEnumField().add(out);
                out.setStatus(FieldStatus.SUPPORTED);
            } else {
                out.setClassName(o.getClass().getCanonicalName());
                out.setStatus(FieldStatus.ERROR);
            }
        }
    } else {
        javaClass.setEnumeration(false);
    }
    inspectClassMethods(classLoader, clz, javaClass, cachedClasses, pathPrefix);
    if (javaClass.getModifiers() == null) {
        javaClass.setModifiers(new ModifierList());
    } else {
        javaClass.getModifiers().getModifier().clear();
    }
    javaClass.getModifiers().getModifier().addAll(detectModifiers(clz.getModifiers()));
// TODO: annotations, generics, enums, class modifiers (public,
// synchronized, etc),
// more of these here:
// https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#isPrimitive--
// TODO: exceptions
// TODO: lists
// return javaClass;
}
Also used : JavaEnumField(io.atlasmap.java.v2.JavaEnumField) ModifierList(io.atlasmap.java.v2.ModifierList)

Example 4 with JavaEnumField

use of io.atlasmap.java.v2.JavaEnumField in project atlasmap by atlasmap.

the class DocumentJavaFieldWriter method write.

public void write(AtlasInternalSession session) throws AtlasException {
    LookupTable lookupTable = session.head().getLookupTable();
    Field sourceField = session.head().getSourceField();
    Field targetField = session.head().getTargetField();
    try {
        if (targetField == null) {
            throw new AtlasException(new IllegalArgumentException("Argument 'field' cannot be null"));
        }
        String targetFieldClassName = (targetField instanceof JavaField) ? ((JavaField) targetField).getClassName() : ((JavaEnumField) targetField).getClassName();
        if (LOG.isDebugEnabled()) {
            LOG.debug("Now processing field: " + targetField);
            LOG.debug("Field type: " + targetField.getFieldType());
            LOG.debug("Field path: " + targetField.getPath());
            LOG.debug("Field value: " + targetField.getValue());
            LOG.debug("Field className: " + targetFieldClassName);
        }
        processedPaths.add(targetField.getPath());
        AtlasPath path = new AtlasPath(targetField.getPath());
        Object parentObject = rootObject;
        boolean segmentIsComplexSegment = true;
        for (SegmentContext segmentContext : path.getSegmentContexts(true)) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Now processing segment: " + segmentContext);
                LOG.debug("Parent object is currently: " + writeDocumentToString(false, parentObject));
            }
            if ("/".equals(segmentContext.getSegmentPath())) {
                if (rootObject == null) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Creating root node: " + segmentContext);
                    }
                    rootObject = createParentObject(targetField, parentObject, segmentContext);
                } else {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Root node already exists, skipping segment: " + segmentContext);
                    }
                }
                parentObject = rootObject;
                continue;
            }
            // if we're on the last segment, the
            boolean segmentIsLastSegment = (segmentContext.getNext() == null);
            if (segmentIsLastSegment) {
                // detect field type from class name if exists
                if (targetField.getFieldType() == null && targetFieldClassName != null && (targetField instanceof JavaField)) {
                    FieldType fieldTypeFromClass = conversionService.fieldTypeFromClass(targetFieldClassName);
                    targetField.setFieldType(fieldTypeFromClass);
                }
                if (FieldType.COMPLEX.equals(targetField.getFieldType())) {
                    segmentIsComplexSegment = true;
                } else {
                    segmentIsComplexSegment = false;
                }
                if (targetField instanceof JavaEnumField) {
                    segmentIsComplexSegment = false;
                }
            }
            if (LOG.isDebugEnabled()) {
                if (segmentIsComplexSegment) {
                    LOG.debug("Now processing complex segment: " + segmentContext);
                } else if (targetField instanceof JavaEnumField) {
                    LOG.debug("Now processing field enum value segment: " + segmentContext);
                } else {
                    LOG.debug("Now processing field value segment: " + segmentContext);
                }
            }
            if (segmentIsComplexSegment) {
                // processing parent object
                Object childObject = findChildObject(targetField, segmentContext, parentObject);
                if (childObject == null) {
                    childObject = createParentObject(targetField, parentObject, segmentContext);
                }
                parentObject = childObject;
            } else {
                // processing field value
                if (AtlasPath.isCollectionSegment(segmentContext.getSegment())) {
                    parentObject = findOrCreateOrExpandParentCollectionObject(targetField, parentObject, segmentContext);
                }
                Object value = converter.convert(session, lookupTable, sourceField, parentObject, targetField);
                addChildObject(targetField, segmentContext, parentObject, value);
            }
        }
    } catch (Throwable t) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Error occured while writing field: " + targetField.getPath(), t);
        }
        if (t instanceof AtlasException) {
            throw (AtlasException) t;
        }
        throw new AtlasException(t);
    }
}
Also used : AtlasException(io.atlasmap.api.AtlasException) FieldType(io.atlasmap.v2.FieldType) JavaEnumField(io.atlasmap.java.v2.JavaEnumField) Field(io.atlasmap.v2.Field) JavaField(io.atlasmap.java.v2.JavaField) SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) JavaEnumField(io.atlasmap.java.v2.JavaEnumField) JavaField(io.atlasmap.java.v2.JavaField) LookupTable(io.atlasmap.v2.LookupTable) AtlasPath(io.atlasmap.core.AtlasPath)

Example 5 with JavaEnumField

use of io.atlasmap.java.v2.JavaEnumField in project atlasmap by atlasmap.

the class TargetValueConverter method convert.

public Object convert(AtlasInternalSession session, LookupTable lookupTable, Field sourceField, Object parentObject, Field targetField) throws AtlasException {
    FieldType sourceType = sourceField.getFieldType();
    Object sourceValue = sourceField.getValue();
    Object targetValue = null;
    FieldType targetType = targetField.getFieldType();
    if (LOG.isDebugEnabled()) {
        LOG.debug("processTargetMapping iPath=" + sourceField.getPath() + " iV=" + sourceValue + " iT=" + sourceType + " oPath=" + targetField.getPath() + " docId: " + targetField.getDocId());
    }
    if (sourceValue == null) {
        // TODO: Finish targetValue = null processing
        AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Null sourceValue for targetDocId=%s, targetPath=%s", targetField.getDocId(), targetField.getPath()), targetField.getPath(), AuditStatus.WARN, sourceValue != null ? sourceValue.toString() : null);
        return null;
    }
    String targetClassName = (targetField instanceof JavaField) ? ((JavaField) targetField).getClassName() : null;
    targetClassName = (targetField instanceof JavaEnumField) ? ((JavaEnumField) targetField).getClassName() : targetClassName;
    if (targetType == null || targetClassName == null) {
        try {
            Method setter = resolveTargetSetMethod(parentObject, targetField, null);
            if (setter != null && setter.getParameterCount() == 1) {
                if (targetField instanceof JavaField) {
                    ((JavaField) targetField).setClassName(setter.getParameterTypes()[0].getName());
                } else if (targetField instanceof JavaEnumField) {
                    ((JavaEnumField) targetField).setClassName(setter.getParameterTypes()[0].getName());
                }
                targetType = conversionService.fieldTypeFromClass(setter.getParameterTypes()[0]);
                targetField.setFieldType(targetType);
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Auto-detected targetType as {} for class={} path={}", targetType, parentObject.toString(), targetField.getPath());
                }
            }
        } catch (Exception e) {
            LOG.debug("Unable to auto-detect targetType for class={} path={}", parentObject.toString(), targetField.getPath());
        }
    }
    if (sourceField instanceof JavaEnumField || targetField instanceof JavaEnumField) {
        if (!(sourceField instanceof JavaEnumField) || !(targetField instanceof JavaEnumField)) {
            AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Value conversion between enum fields and non-enum fields is not yet supported: sourceType=%s targetType=%s targetPath=%s msg=%s", sourceType, targetType, targetField.getPath()), targetField.getPath(), AuditStatus.ERROR, sourceValue != null ? sourceValue.toString() : null);
        }
        return populateEnumValue(session, lookupTable, (JavaEnumField) sourceField, (JavaEnumField) targetField);
    }
    Class<?> targetClazz = null;
    if (targetClassName == null) {
        if (targetType != null) {
            targetClazz = conversionService.classFromFieldType(targetType);
        } else {
            AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Target field doesn't have fieldType nor className: automatic conversion won't work: targetPath=%s", targetField.getPath()), targetField.getPath(), AuditStatus.WARN, sourceValue != null ? sourceValue.toString() : null);
        }
    } else if (conversionService.isPrimitive(targetClassName)) {
        targetClazz = conversionService.boxOrUnboxPrimitive(targetClassName);
    } else {
        try {
            targetClazz = classLoader.loadClass(targetClassName);
        } catch (ClassNotFoundException e) {
            AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Target field class '%s' was not found: sourceType=%s targetType=%s targetPath=%s msg=%s", ((JavaField) targetField).getClassName(), sourceType, targetType, targetField.getPath(), e.getMessage()), targetField.getPath(), AuditStatus.ERROR, targetValue != null ? targetValue.toString() : null);
            return null;
        }
    }
    if (targetClazz != null) {
        targetValue = conversionService.convertType(sourceValue, null, targetClazz, null);
    } else {
        targetValue = sourceValue;
    }
    AtlasFieldActionService fieldActionService = session.getAtlasContext().getContextFactory().getFieldActionService();
    try {
        targetValue = fieldActionService.processActions(targetField.getActions(), targetValue, targetType);
        if (targetValue != null) {
            if (targetClazz != null) {
                targetValue = conversionService.convertType(targetValue, null, targetClazz, null);
            } else {
                FieldType conversionInputType = conversionService.fieldTypeFromClass(targetValue.getClass());
                targetValue = conversionService.convertType(targetValue, conversionInputType, targetType);
            }
        }
    } catch (AtlasConversionException e) {
        AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Unable to auto-convert for sourceType=%s targetType=%s targetPath=%s msg=%s", sourceType, targetType, targetField.getPath(), e.getMessage()), targetField.getPath(), AuditStatus.ERROR, targetValue != null ? targetValue.toString() : null);
        return null;
    }
    return targetValue;
}
Also used : JavaEnumField(io.atlasmap.java.v2.JavaEnumField) JavaField(io.atlasmap.java.v2.JavaField) AtlasFieldActionService(io.atlasmap.api.AtlasFieldActionService) AtlasConversionException(io.atlasmap.api.AtlasConversionException) Method(java.lang.reflect.Method) AtlasException(io.atlasmap.api.AtlasException) AtlasConversionException(io.atlasmap.api.AtlasConversionException) FieldType(io.atlasmap.v2.FieldType)

Aggregations

JavaEnumField (io.atlasmap.java.v2.JavaEnumField)7 AtlasException (io.atlasmap.api.AtlasException)5 JavaField (io.atlasmap.java.v2.JavaField)5 FieldType (io.atlasmap.v2.FieldType)3 Method (java.lang.reflect.Method)3 AtlasPath (io.atlasmap.core.AtlasPath)2 Field (io.atlasmap.v2.Field)2 AtlasConversionException (io.atlasmap.api.AtlasConversionException)1 AtlasFieldActionService (io.atlasmap.api.AtlasFieldActionService)1 SegmentContext (io.atlasmap.core.AtlasPath.SegmentContext)1 ModifierList (io.atlasmap.java.v2.ModifierList)1 LookupTable (io.atlasmap.v2.LookupTable)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1