Search in sources :

Example 1 with ConfigFile

use of org.apache.hop.core.config.plugin.ConfigFile in project hop by apache.

the class Project method modifyVariables.

public void modifyVariables(IVariables variables, ProjectConfig projectConfig, List<String> configurationFiles, String environmentName) throws HopException {
    if (variables == null) {
        variables = Variables.getADefaultVariableSpace();
    }
    // See if we don't have an infinite loop in the project-parent-parent-... hierarchy...
    // 
    verifyProjectsChain(projectConfig.getProjectName(), variables);
    // If there is a parent project we want to pick up the variables defined in the project
    // definition as well
    // 
    Project parentProject = null;
    String realParentProjectName = variables.resolve(parentProjectName);
    if (StringUtils.isNotEmpty(realParentProjectName)) {
        ProjectConfig parentProjectConfig = ProjectsConfigSingleton.getConfig().findProjectConfig(realParentProjectName);
        if (parentProjectConfig != null) {
            try {
                parentProject = parentProjectConfig.loadProject(variables);
                // Apply the variables set in the parent project
                // 
                parentProject.modifyVariables(variables, parentProjectConfig, new ArrayList<>(), null);
            } catch (HopException he) {
                LogChannel.GENERAL.logError("Error loading configuration file of parent project '" + realParentProjectName + "'", he);
            }
        }
    }
    // Set the name of the active environment
    // 
    variables.setVariable(Defaults.VARIABLE_HOP_PROJECT_NAME, Const.NVL(projectConfig.getProjectName(), ""));
    variables.setVariable(Defaults.VARIABLE_HOP_ENVIRONMENT_NAME, Const.NVL(environmentName, ""));
    // 
    if (StringUtils.isNotEmpty(projectConfig.getProjectHome())) {
        String realValue = variables.resolve(projectConfig.getProjectHome());
        variables.setVariable(ProjectsUtil.VARIABLE_PROJECT_HOME, realValue);
    }
    // 
    for (String configurationFile : configurationFiles) {
        String realConfigurationFile = variables.resolve(configurationFile);
        FileObject file = HopVfs.getFileObject(realConfigurationFile);
        try {
            if (file.exists()) {
                ConfigFile configFile = new DescribedVariablesConfigFile(realConfigurationFile);
                configFile.readFromFile();
                // 
                for (DescribedVariable describedVariable : configFile.getDescribedVariables()) {
                    variables.setVariable(describedVariable.getName(), describedVariable.getValue());
                }
            } else {
                LogChannel.GENERAL.logError("Configuration file '" + realConfigurationFile + "' does not exist to read variables from.");
            }
        } catch (Exception e) {
            LogChannel.GENERAL.logError("Error reading described variables from configuration file '" + realConfigurationFile + "'", e);
        }
    }
    if (StringUtils.isNotEmpty(metadataBaseFolder)) {
        String realMetadataBaseFolder = variables.resolve(metadataBaseFolder);
        // 
        if (parentProject != null) {
            // HOP_METADATA_FOLDER was set above in the variables.
            // We're going to simply append to it.
            // 
            String parentMetadataFolder = variables.getVariable(Const.HOP_METADATA_FOLDER);
            if (StringUtils.isNotEmpty(parentMetadataFolder)) {
                realMetadataBaseFolder = parentMetadataFolder + "," + realMetadataBaseFolder;
            }
        }
        variables.setVariable(Const.HOP_METADATA_FOLDER, realMetadataBaseFolder);
    }
    if (StringUtils.isNotEmpty(unitTestsBasePath)) {
        String realValue = variables.resolve(unitTestsBasePath);
        variables.setVariable(ProjectsUtil.VARIABLE_HOP_UNIT_TESTS_FOLDER, realValue);
    }
    if (StringUtils.isNotEmpty(dataSetsCsvFolder)) {
        String realValue = variables.resolve(dataSetsCsvFolder);
        variables.setVariable(ProjectsUtil.VARIABLE_HOP_DATASETS_FOLDER, realValue);
    }
    for (DescribedVariable variable : getDescribedVariables()) {
        if (variable.getName() != null) {
            variables.setVariable(variable.getName(), variable.getValue());
        }
    }
}
Also used : DescribedVariable(org.apache.hop.core.config.DescribedVariable) HopException(org.apache.hop.core.exception.HopException) IConfigFile(org.apache.hop.core.config.IConfigFile) DescribedVariablesConfigFile(org.apache.hop.core.config.DescribedVariablesConfigFile) ConfigFile(org.apache.hop.core.config.plugin.ConfigFile) DescribedVariablesConfigFile(org.apache.hop.core.config.DescribedVariablesConfigFile) FileObject(org.apache.commons.vfs2.FileObject) HopException(org.apache.hop.core.exception.HopException)

Aggregations

FileObject (org.apache.commons.vfs2.FileObject)1 DescribedVariable (org.apache.hop.core.config.DescribedVariable)1 DescribedVariablesConfigFile (org.apache.hop.core.config.DescribedVariablesConfigFile)1 IConfigFile (org.apache.hop.core.config.IConfigFile)1 ConfigFile (org.apache.hop.core.config.plugin.ConfigFile)1 HopException (org.apache.hop.core.exception.HopException)1