use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class DefaultAtlasContext method process.
/**
* Process session lifecycle
*/
@Override
public void process(AtlasSession userSession) throws AtlasException {
if (!(userSession instanceof DefaultAtlasSession)) {
throw new AtlasException(String.format("Unsupported session class '%s'", userSession.getClass().getName()));
}
if (!this.equals(userSession.getAtlasContext())) {
throw new AtlasException("Cannot execute AtlasSession created by the other AtlasContext");
}
DefaultAtlasSession session = (DefaultAtlasSession) userSession;
if (LOG.isDebugEnabled()) {
LOG.debug("Begin process {}", (session == null ? null : session.toString()));
}
session.head().unset();
session.getAudits().getAudit().clear();
session.getValidations().getValidation().clear();
processValidation(session);
for (Validation v : session.getValidations().getValidation()) {
AtlasUtil.addAudit(session, v);
}
if (session.hasErrors()) {
if (LOG.isDebugEnabled()) {
LOG.error("Aborting due to {} errors in pre-validation", session.errorCount());
}
return;
}
for (AtlasModule module : getSourceModules().values()) {
module.processPreSourceExecution(session);
}
for (AtlasModule module : getTargetModules().values()) {
module.processPreTargetExecution(session);
}
if (session.hasErrors()) {
if (LOG.isDebugEnabled()) {
LOG.error("Aborting due to {} errors in pre-execution", session.errorCount());
}
return;
}
for (BaseMapping baseMapping : session.getMapping().getMappings().getMapping()) {
for (Mapping mapping : extractCollectionMappings(session, baseMapping)) {
session.head().setMapping(mapping).setLookupTable(lookupTables.get(mapping.getLookupTableName()));
if (mapping.getOutputField() == null || mapping.getOutputField().isEmpty()) {
AtlasUtil.addAudit(session, null, String.format("Mapping does not contain at least one output field: alias=%s desc=%s", mapping.getAlias(), mapping.getDescription()), null, AuditStatus.WARN, null);
continue;
}
if (mapping.getInputField() == null || mapping.getInputField().isEmpty()) {
AtlasUtil.addAudit(session, null, String.format("Mapping does not contain at least one source field: alias=%s desc=%s", mapping.getAlias(), mapping.getDescription()), null, AuditStatus.WARN, null);
} else {
processSourceFieldMappings(session, mapping.getInputField());
}
processTargetFieldMappings(session, mapping);
}
}
for (AtlasModule module : getSourceModules().values()) {
module.processPostValidation(session);
}
for (AtlasModule module : getTargetModules().values()) {
module.processPostValidation(session);
}
for (AtlasModule module : getSourceModules().values()) {
module.processPostSourceExecution(session);
}
for (AtlasModule module : getTargetModules().values()) {
module.processPostTargetExecution(session);
}
if (LOG.isDebugEnabled()) {
LOG.debug("End process {}", session == null ? null : session.toString());
}
}
use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class DefaultAtlasContext method init.
/**
* TODO: For dynamic re-load. This needs lock()
*
* @throws AtlasException
*/
protected void init() throws AtlasException {
registerJmx(this);
if (this.atlasMappingUri != null) {
this.mappingDefinition = factory.getMappingService().loadMapping(this.atlasMappingUri, atlasMappingFormat);
}
sourceModules.clear();
ConstantModule constant = new ConstantModule();
constant.setConversionService(factory.getConversionService());
constant.setFieldActionService(factory.getFieldActionService());
sourceModules.put(CONSTANTS_DOCUMENT_ID, constant);
PropertyModule propSource = new PropertyModule(factory.getPropertyStrategy());
propSource.setMode(AtlasModuleMode.SOURCE);
propSource.setConversionService(factory.getConversionService());
propSource.setFieldActionService(factory.getFieldActionService());
sourceModules.put(PROPERTIES_DOCUMENT_ID, propSource);
targetModules.clear();
lookupTables.clear();
if (mappingDefinition.getLookupTables() != null && mappingDefinition.getLookupTables().getLookupTable() != null) {
for (LookupTable table : mappingDefinition.getLookupTables().getLookupTable()) {
lookupTables.put(table.getName(), table);
}
}
AtlasModuleInfoRegistry moduleInfoRegistry = factory.getModuleInfoRegistry();
for (DataSource ds : mappingDefinition.getDataSource()) {
AtlasModuleInfo moduleInfo = moduleInfoRegistry.lookupByUri(ds.getUri());
if (moduleInfo == null) {
LOG.error("Cannot find module info for the DataSource uri '{}'", ds.getUri());
continue;
}
if (ds.getDataSourceType() != DataSourceType.SOURCE && ds.getDataSourceType() != DataSourceType.TARGET) {
LOG.error("Unsupported DataSource type '{}'", ds.getDataSourceType());
continue;
}
String docId = ds.getId();
if (docId == null || docId.isEmpty()) {
docId = ds.getDataSourceType() == DataSourceType.SOURCE ? AtlasConstants.DEFAULT_SOURCE_DOCUMENT_ID : AtlasConstants.DEFAULT_TARGET_DOCUMENT_ID;
}
if (ds.getDataSourceType() == DataSourceType.SOURCE && sourceModules.containsKey(docId)) {
LOG.error("Duplicated {} DataSource ID '{}' was detected, ignoring...", ds.getDataSourceType(), ds.getId());
continue;
}
if (ds.getDataSourceType() == DataSourceType.TARGET && targetModules.containsKey(docId)) {
LOG.error("Duplicated {} DataSource ID '{}' was detected, ignoring...", ds.getDataSourceType(), docId);
continue;
}
try {
AtlasModule module = moduleInfo.getModuleClass().newInstance();
module.setConversionService(factory.getConversionService());
module.setFieldActionService(factory.getFieldActionService());
module.setUri(ds.getUri());
if (ds.getDataSourceType() == DataSourceType.SOURCE) {
module.setMode(AtlasModuleMode.SOURCE);
getSourceModules().put(docId, module);
} else if (ds.getDataSourceType() == DataSourceType.TARGET) {
module.setMode(AtlasModuleMode.TARGET);
getTargetModules().put(docId, module);
}
module.setDocId(docId);
module.init();
} catch (Throwable t) {
LOG.error("Unable to initialize {} module: {}", ds.getDataSourceType(), moduleInfo.toString());
LOG.error(t.getMessage(), t);
throw new AtlasException(String.format("Unable to initialize %s module: %s", ds.getDataSourceType(), moduleInfo.toString()), t);
}
}
}
use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class XmlModule method convertDocumentToString.
private String convertDocumentToString(Document document) throws AtlasException {
DocumentBuilderFactory domFact = DocumentBuilderFactory.newInstance();
domFact.setNamespaceAware(true);
StringWriter writer = null;
try {
DOMSource domSource = new DOMSource(document);
writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
return writer.toString();
} catch (TransformerException e) {
LOG.error(String.format("Error converting Xml document to string msg=%s", e.getMessage()), e);
throw new AtlasException(e.getMessage(), e);
}
}
use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class XmlFieldReader method read.
public void read(AtlasInternalSession session) throws AtlasException {
if (document == null) {
throw new AtlasException(new IllegalArgumentException("'document' cannot be null"));
}
Field field = session.head().getSourceField();
if (field == null) {
throw new AtlasException(new IllegalArgumentException("Argument 'field' cannot be null"));
}
seedDocumentNamespaces(document);
XmlField xmlField = XmlField.class.cast(field);
if (LOG.isDebugEnabled()) {
LOG.debug("Reading source value for field: " + xmlField.getPath());
}
if (document == null) {
throw new AtlasException(new IllegalArgumentException("Argument 'document' cannot be null"));
}
if (xmlField == null) {
throw new AtlasException(new IllegalArgumentException("Argument 'xmlField' cannot be null"));
}
Element parentNode = document.getDocumentElement();
for (SegmentContext sc : new XmlPath(xmlField.getPath()).getSegmentContexts(false)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Now processing segment: " + sc.getSegment());
LOG.debug("Parent element is currently: " + XmlIOHelper.writeDocumentToString(true, parentNode));
}
if (sc.getPrev() == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Skipping root segment: " + sc);
}
// "/XOA/contact<>/firstName", skip.
continue;
}
if (!XmlPath.isAttributeSegment(sc.getSegment())) {
String childrenElementName = XmlPath.cleanPathSegment(sc.getSegment());
String namespaceAlias = XmlPath.getNamespace(sc.getSegment());
if (namespaceAlias != null && !"".equals(namespaceAlias)) {
childrenElementName = namespaceAlias + ":" + childrenElementName;
}
if (LOG.isDebugEnabled()) {
LOG.debug("Looking for children elements with name: " + childrenElementName);
}
List<Element> children = XmlIOHelper.getChildrenWithName(childrenElementName, parentNode);
if (children == null || children.isEmpty()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Skipping source value set, couldn't find children with name '" + childrenElementName + "', for segment: " + sc);
}
return;
}
parentNode = children.get(0);
if (XmlPath.isCollectionSegment(sc.getSegment())) {
int index = XmlPath.indexOfSegment(sc.getSegment());
if (index >= children.size()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Skipping source value set, children list can't fit index " + index + ", children list size: " + children.size());
}
return;
}
parentNode = children.get(index);
}
}
if (sc.getNext() == null) {
// last segment.
String value = parentNode.getTextContent();
if (XmlPath.isAttributeSegment(sc.getSegment())) {
String attributeName = XmlPath.getAttribute(sc.getSegment());
value = parentNode.getAttribute(attributeName);
}
if (value == null) {
return;
}
if (xmlField.getFieldType() == null) {
xmlField.setValue(value);
xmlField.setFieldType(FieldType.STRING);
} else {
Object convertedValue;
try {
convertedValue = conversionService.convertType(value, xmlField.getFormat(), xmlField.getFieldType(), null);
xmlField.setValue(convertedValue);
} catch (AtlasConversionException e) {
AtlasUtil.addAudit(session, xmlField.getDocId(), String.format("Failed to convert field value '%s' into type '%s'", value, xmlField.getFieldType()), xmlField.getPath(), AuditStatus.ERROR, value);
}
}
}
}
}
use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class JsonModule method getCollectionSize.
@Override
public int getCollectionSize(AtlasInternalSession session, Field field) throws AtlasException {
// TODO could this use FieldReader?
Object document = session.getSourceDocument(getDocId());
// make this a JSON document
JsonFactory jsonFactory = new JsonFactory();
ObjectMapper objectMapper = new ObjectMapper();
try {
JsonParser parser = jsonFactory.createParser(document.toString());
JsonNode rootNode = objectMapper.readTree(parser);
ObjectNode parentNode = (ObjectNode) rootNode;
String parentSegment = "[root node]";
for (SegmentContext sc : new AtlasPath(field.getPath()).getSegmentContexts(false)) {
JsonNode currentNode = JsonFieldWriter.getChildNode(parentNode, parentSegment, sc.getSegment());
if (currentNode == null) {
return 0;
}
if (AtlasPath.isCollectionSegment(sc.getSegment())) {
if (currentNode != null && currentNode.isArray()) {
return currentNode.size();
}
return 0;
}
parentNode = (ObjectNode) currentNode;
}
} catch (IOException e) {
throw new AtlasException(e.getMessage(), e);
}
return 0;
}
Aggregations