Search in sources :

Example 1 with AtlasModuleInfo

use of io.atlasmap.spi.AtlasModuleInfo in project atlasmap by atlasmap.

the class DefaultAtlasContextFactory method loadModules.

protected void loadModules(String moduleClassProperty, Class<?> moduleInterface) {
    Class<?> moduleClass = null;
    String moduleClassName = null;
    Set<String> serviceClasses = new HashSet<>();
    ClassLoader classLoader = this.getClass().getClassLoader();
    try {
        Enumeration<URL> urls = classLoader.getResources("META-INF/services/atlas/module/atlas.module");
        while (urls.hasMoreElements()) {
            URL tmp = urls.nextElement();
            Properties prop = AtlasUtil.loadPropertiesFromURL(tmp);
            String serviceClassPropertyValue = (String) prop.get(moduleClassProperty);
            if (!AtlasUtil.isEmpty(serviceClassPropertyValue)) {
                serviceClasses.add((serviceClassPropertyValue));
            }
        }
    } catch (Exception e) {
        LOG.warn("Error loading module resources", e);
    }
    for (String clazz : serviceClasses) {
        try {
            moduleClass = Class.forName(clazz);
            moduleClassName = moduleClass.getName();
            if (isClassAtlasModule(moduleClass, moduleInterface)) {
                @SuppressWarnings("unchecked") Class<AtlasModule> atlasModuleClass = (Class<AtlasModule>) moduleClass;
                Constructor<AtlasModule> constructor = atlasModuleClass.getDeclaredConstructor();
                if (constructor != null) {
                    AtlasModuleInfo module = new DefaultAtlasModuleInfo(getModuleName(moduleClass), getModuleUri(moduleClass), atlasModuleClass, constructor, getSupportedDataFormats(moduleClass), getConfigPackages(moduleClass));
                    getModuleInfoRegistry().register(module);
                } else {
                    LOG.warn("Invalid module class " + moduleClassName + ": constructor is not present");
                }
            } else {
                LOG.warn("Invalid module class  " + moduleClassName + ": unsupported AtlasModule");
            }
        } catch (NoSuchMethodException e) {
            LOG.warn("Invalid module class " + moduleClassName + ": constructor is not present.", e);
        } catch (ClassNotFoundException e) {
            LOG.warn("Invalid module class " + moduleClassName + " not found in classLoader", e);
        } catch (Exception e) {
            LOG.warn("Invalid module class " + moduleClassName + " unknown error", e);
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Loaded: " + getModuleInfoRegistry().size() + " of " + serviceClasses.size() + " detected modules");
    }
}
Also used : Properties(java.util.Properties) URL(java.net.URL) AtlasException(io.atlasmap.api.AtlasException) MalformedObjectNameException(javax.management.MalformedObjectNameException) AtlasModule(io.atlasmap.spi.AtlasModule) AtlasModuleInfo(io.atlasmap.spi.AtlasModuleInfo) HashSet(java.util.HashSet)

Example 2 with AtlasModuleInfo

use of io.atlasmap.spi.AtlasModuleInfo 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);
        }
    }
}
Also used : AtlasModule(io.atlasmap.spi.AtlasModule) AtlasModuleInfo(io.atlasmap.spi.AtlasModuleInfo) AtlasModuleInfoRegistry(io.atlasmap.spi.AtlasModuleInfoRegistry) LookupTable(io.atlasmap.v2.LookupTable) AtlasException(io.atlasmap.api.AtlasException) DataSource(io.atlasmap.v2.DataSource)

Aggregations

AtlasException (io.atlasmap.api.AtlasException)2 AtlasModule (io.atlasmap.spi.AtlasModule)2 AtlasModuleInfo (io.atlasmap.spi.AtlasModuleInfo)2 AtlasModuleInfoRegistry (io.atlasmap.spi.AtlasModuleInfoRegistry)1 DataSource (io.atlasmap.v2.DataSource)1 LookupTable (io.atlasmap.v2.LookupTable)1 URL (java.net.URL)1 HashSet (java.util.HashSet)1 Properties (java.util.Properties)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1