blob: e6ab5d737ff0c62d7443153a28c99ee744c0bfa4 [file] [log] [blame]
/**
********************************************************************************
* Copyright (c) 2018-2019 Robert Bosch GmbH.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Robert Bosch GmbH - initial API and implementation
********************************************************************************
*/
package org.eclipse.app4mc.transformation.application.base
import com.google.inject.Guice
import com.google.inject.Injector
import java.util.ArrayList
import java.util.List
import java.util.Map
import java.util.Properties
import org.apache.log4j.Logger
import org.eclipse.app4mc.transformation.extensions.CustomObjectsStore
import org.eclipse.app4mc.transformation.extensions.base.templates.Model2ModelRootTransformer
import org.eclipse.app4mc.transformation.extensions.base.templates.Model2TextRootTransformer
import org.eclipse.app4mc.transformation.extensions.base.templates.AbstractTransformer
class ExecuteTransformation {
def static void start(Logger logger, Properties properties) {
var Map<String, TransformationConfig> enabledTransformationConfigurations = ExtensionExecution.
getEnabledTransformationConfigElementsFromExtensions(logger);
var List<TransformationConfig> configuredTransformations = getTransformationElementsSpecifiedByUser(
enabledTransformationConfigurations, properties, logger);
if (configuredTransformations.size == 0) {
var msg = "Configured org.eclipse.app4mc.transformation.configuration are not enabled (or) they are not defined"
logger.error(msg, new RuntimeException(msg))
}
for (transformationConfig : configuredTransformations) {
val injector = Guice::createInjector(transformationConfig.injectorModule)
/*-- Injection of created objects into Google Guice -- */
injectPreCreatedObjects(injector, properties)
/*------------------------------------------------------- */
val model2ModelTransformer = injector.getInstance(typeof(Model2ModelRootTransformer));
/*- M2M transformation */
var m2mConfig = transformationConfig.model2ModelConfig;
if (m2mConfig !== null && model2ModelTransformer !== null) {
m2mConfig.properties = properties
m2mConfig.logger = logger
Model2ModelRootTransformer.logger=logger
Model2ModelRootTransformer.properties=properties
Model2ModelRootTransformer.injector=injector
model2ModelTransformer.m2mTransformation(m2mConfig.inputResourceSet, m2mConfig.ouputResourceSet)
}
/*- M2T transformation */
val model2TextTransformer = injector.getInstance(typeof(Model2TextRootTransformer));
var m2tConfig = transformationConfig.model2TextConfig;
if (m2tConfig !== null && model2TextTransformer !== null) {
m2tConfig.properties = properties
m2tConfig.logger = logger
Model2TextRootTransformer.logger=logger
Model2TextRootTransformer.properties=properties
Model2TextRootTransformer.injector=injector
model2TextTransformer.m2tTransformation(m2tConfig.inputResourceSet)
}
}
}
def static List<TransformationConfig> getTransformationElementsSpecifiedByUser(
Map<String, TransformationConfig> map, Properties properties, Logger logger) {
val List<TransformationConfig> transformationConfigElements = new ArrayList
if (properties.containsKey("tranformationConfigIDs")) {
val transformationConfigIDs = (properties.get("tranformationConfigIDs") as String).split("\\;")
for (id : transformationConfigIDs) {
val transConfig = map.get(id)
if (transConfig !== null) {
transformationConfigElements.add(transConfig)
}
}
} else {
logger.error("Input properties file doesn't contain key with name: \"tranformationConfigIDs\" ",
new Exception("Missing required parameter : \"tranformationConfigIDs\""))
}
return transformationConfigElements
}
protected def static void injectPreCreatedObjects(Injector injector, Object... inputObjs) {
val customObjectsStore = injector.getInstance(CustomObjectsStore)
for (element : inputObjs) {
customObjectsStore.injectMembers(element.class, element)
}
}
}