use of io.atlasmap.v2.Constant in project atlasmap by atlasmap.
the class DefaultAtlasContext method init.
/**
* TODO: For dynamic re-load. This needs lock()
*
* @throws AtlasException failed to initialize
*/
protected synchronized void init() throws AtlasException {
if (this.initialized) {
return;
}
registerJmx(this);
if (this.atlasMappingUri != null) {
this.admHandler = new ADMArchiveHandler(factory.getClassLoader());
this.admHandler.setIgnoreLibrary(true);
this.admHandler.load(Paths.get(this.atlasMappingUri));
this.dataSourceMetadataMap = this.admHandler.getDataSourceMetadataMap();
}
if (this.admHandler == null || this.admHandler.getMappingDefinition() == null) {
LOG.warn("AtlasMap context cannot initialize without mapping definition, ignoring:" + " Mapping URI={}", this.atlasMappingUri);
return;
}
sourceModules.clear();
ConstantModule constant = new ConstantModule();
constant.setConversionService(factory.getConversionService());
constant.setFieldActionService(factory.getFieldActionService());
sourceModules.put(AtlasConstants.CONSTANTS_DOCUMENT_ID, constant);
PropertyModule property = new PropertyModule(factory.getPropertyStrategy());
property.setConversionService(factory.getConversionService());
property.setFieldActionService(factory.getFieldActionService());
property.setMode(AtlasModuleMode.SOURCE);
sourceModules.put(AtlasConstants.PROPERTIES_SOURCE_DOCUMENT_ID, property);
targetModules.clear();
property = new PropertyModule(factory.getPropertyStrategy());
property.setConversionService(factory.getConversionService());
property.setFieldActionService(factory.getFieldActionService());
property.setMode(AtlasModuleMode.TARGET);
targetModules.put(AtlasConstants.PROPERTIES_TARGET_DOCUMENT_ID, property);
lookupTables.clear();
if (admHandler.getMappingDefinition().getLookupTables() != null && admHandler.getMappingDefinition().getLookupTables().getLookupTable() != null) {
for (LookupTable table : admHandler.getMappingDefinition().getLookupTables().getLookupTable()) {
lookupTables.put(table.getName(), table);
}
}
AtlasModuleInfoRegistry moduleInfoRegistry = factory.getModuleInfoRegistry();
for (DataSource ds : admHandler.getMappingDefinition().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().getDeclaredConstructor().newInstance();
module.setClassLoader(factory.getClassLoader());
module.setConversionService(factory.getConversionService());
module.setFieldActionService(factory.getFieldActionService());
module.setDataSource(ds);
if (ds.getDataSourceType() == DataSourceType.SOURCE) {
getSourceModules().put(docId, module);
} else if (ds.getDataSourceType() == DataSourceType.TARGET) {
getTargetModules().put(docId, module);
}
if (this.dataSourceMetadataMap != null) {
DataSourceKey dskey = new DataSourceKey(ds.getDataSourceType() == DataSourceType.SOURCE, docId);
DataSourceMetadata meta = this.dataSourceMetadataMap.get(dskey);
if (meta != null) {
module.setDataSourceMetadata(meta);
}
}
module.init();
} catch (Exception t) {
LOG.error("Unable to initialize {} module: {}", ds.getDataSourceType(), moduleInfo);
LOG.error(t.getMessage(), t);
throw new AtlasException(String.format("Unable to initialize %s module: %s", ds.getDataSourceType(), moduleInfo.toString()), t);
}
}
initialized = true;
}
use of io.atlasmap.v2.Constant in project atlasmap by atlasmap.
the class AtlasField method readConstant.
/**
* Reads the constant.
* @param name name
* @return read field
* @throws AtlasException unexpected error
*/
public AtlasField readConstant(String name) throws AtlasException {
ConstantModule module = session.getConstantModule();
List<Constant> constants = session.getMapping().getConstants().getConstant();
for (Constant constant : constants) {
if (constant.getName() != null && constant.getName().equals(name)) {
Field sourceField = module.createField();
sourceField.setName(constant.getName());
sourceField.setFieldType(constant.getFieldType());
sourceField.setValue(constant.getValue());
session.head().setSourceField(sourceField);
module.readSourceValue(session);
setRawField(sourceField);
return this;
}
}
throw new AtlasException(String.format("Constant '%s' not found", name));
}
use of io.atlasmap.v2.Constant in project atlasmap by atlasmap.
the class JsonMarshallerTest method testComplexRequests.
@Test
public void testComplexRequests() throws Exception {
runJSONSerializationTest(generatePropertyReferenceMapping(), "atlasmapping-property-request.json");
runJSONSerializationTest(generateConstantMapping(), "atlasmapping-constant-request.json");
runJSONSerializationTest(generateMultiSourceMapping(), "atlasmapping-multisource-request.json");
runJSONSerializationTest(generateCollectionMapping(), "atlasmapping-collection-request.json");
runJSONSerializationTest(generateCombineMapping(), "atlasmapping-combine-request.json");
runJSONSerializationTest(generateActionMapping(), "atlasmapping-field-action-request.json");
AtlasMapping action = mapper.readValue(new File("target" + File.separator + "junit" + File.separator + testMethodName + File.separator + "atlasmapping-field-action-request.json"), AtlasMapping.class);
assertNotNull(action);
validateAtlasMapping(action);
AtlasMapping collection = mapper.readValue(new File("target" + File.separator + "junit" + File.separator + testMethodName + File.separator + "atlasmapping-collection-request.json"), AtlasMapping.class);
assertNotNull(collection);
validateCollectionAtlasMapping(collection);
AtlasMapping multisource = mapper.readValue(new File("target" + File.separator + "junit" + File.separator + testMethodName + File.separator + "atlasmapping-multisource-request.json"), AtlasMapping.class);
assertNotNull(multisource);
validateMultisourceAtlasMapping(multisource);
AtlasMapping propertyMapping = mapper.readValue(new File("target" + File.separator + "junit" + File.separator + testMethodName + File.separator + "atlasmapping-property-request.json"), AtlasMapping.class);
assertNotNull(propertyMapping);
validatePropertyAtlasMapping(propertyMapping);
AtlasMapping constantMapping = mapper.readValue(new File("target" + File.separator + "junit" + File.separator + testMethodName + File.separator + "atlasmapping-constant-request.json"), AtlasMapping.class);
assertNotNull(constantMapping);
validateConstantAtlasMapping(constantMapping);
AtlasMapping combineAtlasMapping = mapper.readValue(new File("target" + File.separator + "junit" + File.separator + testMethodName + File.separator + "atlasmapping-combine-request.json"), AtlasMapping.class);
assertNotNull(combineAtlasMapping);
validateCombineAtlasMapping(combineAtlasMapping);
}
use of io.atlasmap.v2.Constant in project atlasmap by atlasmap.
the class ConstantPropertyTest method test.
@Test
public void test() throws Exception {
URL url = Thread.currentThread().getContextClassLoader().getResource("mappings/issue/constant-property-mapping.xml");
AtlasMapping mapping = mappingService.loadMapping(url, AtlasMappingFormat.XML);
AtlasContext context = DefaultAtlasContextFactory.getInstance().createContext(mapping);
AtlasSession session = context.createSession();
context.process(session);
assertFalse(TestHelper.printAudit(session), session.hasErrors());
TargetClass output = TargetClass.class.cast(session.getTargetDocument("io.atlasmap.core.issue.TargetClass"));
assertEquals("testValue", output.getTargetName());
assertNotEquals("testPath", output.getTargetFirstName());
assertEquals(777, output.getTargetInteger());
System.setProperty("testProp", "testProp-sysProp");
System.setProperty("PATH", "PATH-sysProp");
context.process(session);
assertFalse(TestHelper.printAudit(session), session.hasErrors());
output = TargetClass.class.cast(session.getTargetDocument("io.atlasmap.core.issue.TargetClass"));
assertEquals("testProp-sysProp", output.getTargetName());
assertEquals("PATH-sysProp", output.getTargetFirstName());
assertEquals(777, output.getTargetInteger());
session.getProperties().put("testProp", "testProp-runtimeProp");
session.getProperties().put("PATH", "PATH-runtimeProp");
context.process(session);
assertFalse(TestHelper.printAudit(session), session.hasErrors());
output = TargetClass.class.cast(session.getTargetDocument("io.atlasmap.core.issue.TargetClass"));
assertEquals("testProp-runtimeProp", output.getTargetName());
assertEquals("PATH-runtimeProp", output.getTargetFirstName());
assertEquals(777, output.getTargetInteger());
}
use of io.atlasmap.v2.Constant in project atlasmap by atlasmap.
the class BaseDefaultAtlasContextTest method populateConstant.
protected void populateConstant(String name, String value) {
Constant c = new Constant();
c.setName(name);
c.setValue(value);
session.getMapping().getConstants().getConstant().add(c);
}
Aggregations