use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class DocumentJavaFieldWriter method addChildObject.
@SuppressWarnings({ "rawtypes", "unchecked" })
private void addChildObject(Field field, SegmentContext segmentContext, Object parentObject, Object childObject) throws AtlasException {
if (LOG.isDebugEnabled()) {
LOG.debug("Adding child object for segment: " + segmentContext + "\n\tparentObject: " + parentObject + "\n\tchild: " + childObject);
}
if (this.rootObject == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Setting root object: " + childObject);
}
this.rootObject = childObject;
return;
}
boolean parentIsCollection = (parentObject instanceof Collection) || (parentObject.getClass().isArray());
if (parentIsCollection) {
String segment = segmentContext.getSegment();
int index = AtlasPath.indexOfSegment(segment);
if (parentObject instanceof List) {
List list = (List) parentObject;
if (index >= list.size()) {
throw new AtlasException("Cannot fit item in list, list size: " + list.size() + ", item index: " + index + ", segment: " + segmentContext);
}
list.set(index, childObject);
} else if (parentObject instanceof Map) {
throw new AtlasException("FIXME: Cannot yet handle adding children to maps");
} else if (parentObject.getClass().isArray()) {
if (index >= Array.getLength(parentObject)) {
throw new AtlasException("Cannot fit item in array, array size: " + Array.getLength(parentObject) + ", item index: " + index + ", segment: " + segmentContext);
}
try {
Array.set(parentObject, index, childObject);
} catch (Exception e) {
String parentClass = parentObject == null ? null : parentObject.getClass().getName();
String childClass = childObject == null ? null : childObject.getClass().getName();
throw new AtlasException("Could not set child class '" + childClass + "' on parent '" + parentClass + "' for: " + segmentContext, e);
}
} else {
throw new AtlasException("Cannot determine collection type for: " + parentObject);
}
} else {
writerUtil.setObjectOnParent(field, segmentContext, parentObject, childObject);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Finished adding child object for segment: " + segmentContext + "\n\tparentObject: " + parentObject + "\n\t: " + childObject);
}
}
use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class DocumentJavaFieldWriter method unwrapCollectionType.
private Class<?> unwrapCollectionType(Field field, SegmentContext segmentContext, Object parentObject, Class<?> clz, Type clzType) throws AtlasException {
Class<?> answer = clz;
if (answer.isArray()) {
Class<?> oldClass = answer;
answer = answer.getComponentType();
if (LOG.isDebugEnabled()) {
LOG.debug("Unwrapped type '" + answer.getName() + "' from wrapper array type '" + oldClass.getName() + "'.");
}
} else if (Collection.class.isAssignableFrom(answer)) {
Class<?> oldClass = answer;
answer = null;
String cleanedSegment = AtlasPath.cleanPathSegment(segmentContext.getSegment());
// From return type of getter method
if (clzType instanceof Class) {
// No type parameter, use Object
answer = Object.class;
} else if (clzType instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) clzType;
String typeName = pt.getActualTypeArguments()[0].getTypeName();
try {
answer = typeName == null ? null : Class.forName(typeName);
} catch (Exception e) {
throw new AtlasException("Could not find class for '" + typeName + "', for segment: " + segmentContext + ", on field: " + field, e);
}
// No getter found - check fields of parent object
} else if (answer == null) {
Class<?> parentClass = parentObject.getClass();
while (parentClass != Object.class && answer == null) {
answer = findClassOfNamedField(parentClass, cleanedSegment);
parentClass = parentClass.getSuperclass();
}
}
if (answer == null) {
throw new AtlasException("Could not unwrap list collection's generic type for segment: " + segmentContext);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Unwrapped type '" + answer.getName() + "' from wrapper list type '" + oldClass.getName() + "'.");
}
}
return answer;
}
use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class JavaWriterUtil method setObjectOnParent.
/**
* Set the given object within the parentObject.
*
* @param field
* - provided if we need it, I don't think we will since we already
* have the value in hand?
* @param segmentContext
* - current segment for the field's path, this will be the last
* segment in the path.
* @param parentObject
* - the object we're setting the value in
* @param childObject
* - the childObject to set
*/
public void setObjectOnParent(Field javaField, SegmentContext segmentContext, Object parentObject, Object childObject) throws AtlasException {
if (LOG.isDebugEnabled()) {
LOG.debug("Setting object for path:'" + javaField.getPath() + "'.\n\tchildObject: " + childObject + "\n\tparentObject: " + parentObject);
}
AtlasPath pathUtil = new AtlasPath(javaField.getPath());
try {
Class<?> childClass = childObject == null ? null : childObject.getClass();
Method targetMethod = resolveSetMethod(parentObject, segmentContext, childClass);
Object targetObject = parentObject;
// We already know we have a 1 paramter setter here
if (childObject == null && conversionService.isPrimitive(targetMethod.getParameterTypes()[0])) {
if (LOG.isDebugEnabled()) {
LOG.debug("Not setting null value for primitive method paramter for path:'" + javaField.getPath() + "'.\n\tchildObject: " + childObject + "\n\tparentObject: " + parentObject);
}
return;
}
if (targetMethod != null) {
targetMethod.invoke(targetObject, childObject);
javaField.setValue(childObject);
} else {
try {
java.lang.reflect.Field field = targetObject.getClass().getField(pathUtil.getLastSegment());
field.setAccessible(true);
field.set(targetObject, childObject);
javaField.setValue(field.get(targetObject));
} catch (NoSuchFieldException nsfe) {
throw new AtlasException("Unable to find matting setter method or field for path: " + javaField.getPath() + " on parentObject: " + parentObject.getClass().getName());
}
}
} catch (Exception e) {
String parentClassName = parentObject == null ? null : parentObject.getClass().getName();
String childClassName = childObject == null ? null : childObject.getClass().getName();
throw new AtlasException("Unable to set value for path: " + javaField.getPath() + " parentObject: " + parentClassName + " childObject: " + childClassName, e);
}
}
use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class JavaWriterUtil method getObjectFromParent.
/**
* Retrieve a child object (which may be a complex class or collection class)
* from the given parentObject.
*
* @param field
* - provided for convenience, probably not needed here
* @param ParentObject
* - the object to find the child on
* @param segmentContext
* - the segment of the field's path that references the child object
*/
public Object getObjectFromParent(Field field, Object parentObject, SegmentContext segmentContext) throws AtlasException {
if (LOG.isDebugEnabled()) {
LOG.debug("Retrieving child '" + segmentContext.getSegmentPath() + "'.\n\tparentObject: " + parentObject);
}
if (parentObject == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Cannot find child '" + segmentContext.getSegmentPath() + "', parent is null.");
}
return null;
}
// clean up our segment from something like "@addressLine1" to "addressLine1".
// collection segments like "orders[4]" will be cleaned to "orders"
String cleanedSegment = AtlasPath.cleanPathSegment(segmentContext.getSegment());
// FIXME: this doesn't work if there isn't a getter but there is a private
// member variable
List<String> getters = ClassHelper.getterMethodNames(cleanedSegment);
Method getterMethod = null;
for (String getter : getters) {
try {
getterMethod = ClassHelper.detectGetterMethod(parentObject.getClass(), getter);
break;
} catch (NoSuchMethodException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Looking for getter for '" + segmentContext.getSegmentPath() + " on this class: " + parentObject.getClass().getName(), e);
}
}
}
if (getterMethod == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Unable to detect getter method for: " + segmentContext.getSegment() + " from " + segmentContext.getSegmentPath() + " on parent: " + parentObject);
}
return null;
}
getterMethod.setAccessible(true);
Object childObject;
try {
childObject = getterMethod.invoke(parentObject);
} catch (Exception e) {
throw new AtlasException(e);
}
if (LOG.isDebugEnabled()) {
if (childObject == null) {
LOG.debug("Could not find child object for path: " + segmentContext.getSegmentPath());
} else {
LOG.debug("Found child object for path '" + segmentContext.getSegmentPath() + "': " + childObject);
}
}
return childObject;
}
use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class JavaModule method processPreTargetExecution.
@Override
public void processPreTargetExecution(AtlasInternalSession atlasSession) throws AtlasException {
if (atlasSession == null || atlasSession.getMapping() == null || atlasSession.getMapping().getMappings() == null || atlasSession.getMapping().getMappings().getMapping() == null) {
throw new AtlasException("AtlasSession not properly intialized with a mapping that contains field mappings");
}
if (javaInspectionService == null) {
javaInspectionService = new ClassInspectionService();
javaInspectionService.setConversionService(getConversionService());
}
List<BaseMapping> mapping = atlasSession.getMapping().getMappings().getMapping();
Object rootObject;
String targetClassName = AtlasUtil.getUriParameterValue(getUri(), "className");
JavaClass inspectClass = getJavaInspectionService().inspectClass(targetClassName);
merge(inspectClass, mapping);
List<String> targetPaths = AtlasModuleSupport.listTargetPaths(mapping);
try {
rootObject = getJavaConstructService().constructClass(inspectClass, targetPaths);
} catch (Exception e) {
throw new AtlasException(e);
}
DocumentJavaFieldWriter writer = new DocumentJavaFieldWriter(getConversionService());
writer.setRootObject(rootObject);
writer.setTargetValueConverter(targetValueConverter);
atlasSession.setFieldWriter(getDocId(), writer);
if (LOG.isDebugEnabled()) {
LOG.debug("{}: processPreTargetExcution completed", getDocId());
}
}
Aggregations