| package org.eclipse.app4mc.transformation.application.base; |
| |
| import java.io.File; |
| import java.io.FileInputStream; |
| import java.io.FileNotFoundException; |
| import java.io.IOException; |
| import java.util.Dictionary; |
| import java.util.Hashtable; |
| import java.util.List; |
| import java.util.Properties; |
| |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| import org.eclipse.app4mc.transformation.extensions.ICustomObjectsStore; |
| import org.eclipse.app4mc.transformation.extensions.base.templates.Model2ModelRootTransformer; |
| import org.eclipse.app4mc.transformation.extensions.base.templates.Model2TextRootTransformer; |
| import org.eclipse.app4mc.transformation.extensions.executiontype.IModelToModelConfig; |
| import org.eclipse.app4mc.transformation.extensions.executiontype.IModelToTextConfig; |
| import org.osgi.service.component.ComponentFactory; |
| import org.osgi.service.component.ComponentInstance; |
| import org.osgi.service.component.annotations.Component; |
| import org.osgi.service.component.annotations.Reference; |
| |
| @Component(service = StartTransformationCommand.class) |
| public class StartTransformationCommand { |
| |
| @Reference(target = "(component.factory=TRANSFORMER_FACTORY)") |
| ComponentFactory<TransformerFactoryComponent> factory; |
| |
| @Reference |
| ICustomObjectsStore customObjectStore; |
| |
| public void starttest() { |
| System.out.println("Hello World !!"); |
| } |
| |
| public void startTransformation(String[] args) { |
| |
| try { |
| |
| String propertiesFilePath = null; |
| |
| for (int i = 0; i < args.length; i++) { |
| String arg = args[i]; |
| if (arg.equals("--properties")) { |
| propertiesFilePath = args[++i]; |
| } |
| } |
| |
| if (propertiesFilePath == null) { |
| System.out.println("Model transformation can not be performed as Properties file path is not supplied"); |
| } |
| Properties inputParameters = getInputParameters(propertiesFilePath); |
| |
| if (inputParameters != null) { |
| |
| Logger logger = getLogger(inputParameters); |
| |
| logger.info("Starting Model transformation ..."); |
| |
| Dictionary<String, Object> dictionaryForFactory = new Hashtable<>(); |
| |
| for (final String name : inputParameters.stringPropertyNames()) { |
| dictionaryForFactory.put(name, inputParameters.getProperty(name)); |
| customObjectStore.indexData(name, inputParameters.getProperty(name)); |
| } |
| |
| customObjectStore.injectMembers(Properties.class, inputParameters); |
| |
| customObjectStore.injectMembers(Logger.class, logger); |
| |
| ComponentInstance<TransformerFactoryComponent> newInstance = factory.newInstance(dictionaryForFactory); |
| |
| TransformerFactoryComponent factoryInstance = newInstance.getInstance(); |
| |
| List<Model2ModelRootTransformer> m2mTransformers = factoryInstance.getAllM2MTransformers(); |
| |
| List<IModelToModelConfig> allModelToModelConfig = factoryInstance.getAllModelToModelConfig(); |
| |
| List<Model2TextRootTransformer> m2tTransformers = factoryInstance.getAllM2TTransformers(); |
| |
| List<IModelToTextConfig> allModelToTextConfig = factoryInstance.getAllModelToTextConfig(); |
| |
| logger.debug("Model2Model transformers : " + m2mTransformers); |
| logger.debug("Model2Text transformers : " + m2tTransformers); |
| logger.debug("Model2Model transformation configuration : " + allModelToModelConfig); |
| logger.debug("Model2Text transformation configuration : " + allModelToTextConfig); |
| |
| // - Model to Model transformation |
| |
| if(m2mTransformers.size()>0) |
| logger.info("****************** Model to Model Transformation *******************************"); |
| for (Model2ModelRootTransformer model2ModelRootTransformer : m2mTransformers) { |
| |
| System.out.println("** Executing M2M transformer : "+model2ModelRootTransformer.getClass().getTypeName()); |
| for (IModelToModelConfig iModelToModelConfig : allModelToModelConfig) { |
| model2ModelRootTransformer.m2mTransformation(iModelToModelConfig.getInputResourceSet(), |
| iModelToModelConfig.getOuputResourceSet()); |
| } |
| |
| } |
| |
| if(m2tTransformers.size()>0) |
| logger.info("****************** Model to Text Transformation *******************************"); |
| // - Model to Text transformation |
| |
| for (Model2TextRootTransformer model2TextRootTransformer : m2tTransformers) { |
| System.out.println("** Executing M2T transformer : "+model2TextRootTransformer.getClass().getTypeName()); |
| |
| for (IModelToTextConfig iModelToTextConfig : allModelToTextConfig) { |
| model2TextRootTransformer.m2tTransformation(iModelToTextConfig.getInputResourceSet()); |
| } |
| |
| } |
| |
| } else { |
| System.out.println( |
| "ERROR !! Unable to start transformation as required parameters are not set in input properties file"); |
| |
| } |
| |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| protected Properties getInputParameters(String inputPropsFile) throws IOException, FileNotFoundException { |
| |
| if (inputPropsFile != null && inputPropsFile.length() > 0) { |
| |
| File propertiesFile = new File(inputPropsFile); |
| |
| Properties properties = new Properties(); |
| |
| properties.load(new FileInputStream(propertiesFile)); |
| |
| // Now checking if the user has specified absolute paths in the properties file |
| // ? |
| |
| Object inputModelsFolder = properties.get("input_models_folder"); |
| Object m2m_outputModelsFolder = properties.get("m2m_output_folder"); |
| Object m2t_output_folder = properties.get("m2t_output_folder"); |
| Object logFile = properties.get("log_file"); |
| |
| if (inputModelsFolder != null) { |
| String path = inputModelsFolder.toString(); |
| |
| String newPath = new File(path).exists() ? path |
| : new File(propertiesFile.getParent() + File.separator + path).getCanonicalPath(); |
| |
| properties.put("input_models_folder", newPath); |
| |
| } |
| if (m2m_outputModelsFolder != null) { |
| String path = m2m_outputModelsFolder.toString(); |
| |
| String newPath = new File(path).exists() ? path |
| : new File(propertiesFile.getParent() + File.separator + path).getCanonicalPath(); |
| |
| properties.put("m2m_output_folder", newPath); |
| |
| } |
| if (m2t_output_folder != null) { |
| String path = m2t_output_folder.toString(); |
| |
| String newPath = new File(path).exists() ? path |
| : new File(propertiesFile.getParent() + File.separator + path).getCanonicalPath(); |
| |
| properties.put("m2t_output_folder", newPath); |
| |
| } |
| if (logFile != null) { |
| String path = logFile.toString(); |
| |
| String newPath = new File(path).exists() ? path |
| : new File(propertiesFile.getParent() + File.separator + path).getCanonicalPath(); |
| |
| properties.put("log_file", newPath); |
| |
| } |
| |
| return properties; |
| } |
| |
| return null; |
| |
| } |
| |
| protected Logger getLogger(Properties inputParameters) { |
| |
| |
| String logFilePath = inputParameters.getProperty("log_file"); |
| System.setProperty("APP4MC_TRANSFORMATION_LOG_FILE", logFilePath); |
| Logger logger = LoggerFactory.getLogger(StartTransformationCommand.class); |
| |
| System.out.println("Following log4j configuration is considered :"+System.getProperty("log4j.configuration")); |
| |
| return logger; |
| } |
| |
| } |