Search in sources :

Example 1 with DescribedVariablesConfigFile

use of org.apache.hop.core.config.DescribedVariablesConfigFile in project hop by apache.

the class LifecycleEnvironmentDialog method editConfigFile.

private void editConfigFile(Event event) {
    try {
        int index = wConfigFiles.getSelectionIndex();
        if (index < 0) {
            return;
        }
        String configFilename = wConfigFiles.getItem(index, 1);
        if (StringUtils.isEmpty(configFilename)) {
            return;
        }
        String realConfigFilename = variables.resolve(configFilename);
        DescribedVariablesConfigFile variablesConfigFile = new DescribedVariablesConfigFile(realConfigFilename);
        FileObject file = HopVfs.getFileObject(realConfigFilename);
        if (!file.exists()) {
            MessageBox box = new MessageBox(HopGui.getInstance().getShell(), SWT.YES | SWT.NO | SWT.ICON_QUESTION);
            box.setText("Create file?");
            box.setMessage("This configuration file doesn't exist.  Do you want to create it?");
            int answer = box.open();
            if ((answer & SWT.NO) != 0) {
                return;
            }
        } else {
            variablesConfigFile.readFromFile();
        }
        boolean changed = HopGui.editConfigFile(shell, realConfigFilename, variablesConfigFile, null);
        if (changed) {
            needingEnvironmentRefresh = true;
        }
    } catch (Exception e) {
        new ErrorDialog(shell, "Error", "Error editing configuration file", e);
    }
}
Also used : ErrorDialog(org.apache.hop.ui.core.dialog.ErrorDialog) DescribedVariablesConfigFile(org.apache.hop.core.config.DescribedVariablesConfigFile) FileObject(org.apache.commons.vfs2.FileObject) HopException(org.apache.hop.core.exception.HopException)

Example 2 with DescribedVariablesConfigFile

use of org.apache.hop.core.config.DescribedVariablesConfigFile in project hop by apache.

the class ManageConfigFileOptionPlugin method handleOption.

@Override
public boolean handleOption(ILogChannel log, IHasHopMetadataProvider hasHopMetadataProvider, IVariables variables) throws HopException {
    String realConfigFile = variables.resolve(configFile);
    if (StringUtils.isEmpty(realConfigFile)) {
        return false;
    }
    try {
        boolean changed = false;
        DescribedVariablesConfigFile variablesConfigFile = new DescribedVariablesConfigFile(realConfigFile);
        if (HopVfs.fileExists(realConfigFile)) {
            variablesConfigFile.readFromFile();
        }
        // 
        if (configSetVariables != null && configSetVariables.length > 0) {
            for (String varValue : configSetVariables) {
                int equalsIndex = varValue.indexOf("=");
                if (equalsIndex > 0) {
                    String variableName = varValue.substring(0, equalsIndex);
                    String variableValue = varValue.substring(equalsIndex + 1);
                    DescribedVariable describedVariable = variablesConfigFile.findDescribedVariable(variableName);
                    if (describedVariable == null) {
                        describedVariable = new DescribedVariable(variableName, variableValue, "");
                    } else {
                        describedVariable.setValue(variableValue);
                    }
                    variablesConfigFile.setDescribedVariable(describedVariable);
                    changed = true;
                }
            }
        }
        // 
        if (configDescribeVariables != null && configDescribeVariables.length > 0) {
            for (String varDesc : configDescribeVariables) {
                int equalsIndex = varDesc.indexOf("=");
                if (equalsIndex > 0) {
                    String variableName = varDesc.substring(0, equalsIndex);
                    String variableDescription = varDesc.substring(equalsIndex + 1);
                    DescribedVariable describedVariable = variablesConfigFile.findDescribedVariable(variableName);
                    if (describedVariable == null) {
                        describedVariable = new DescribedVariable(variableName, null, variableDescription);
                    } else {
                        describedVariable.setDescription(variableDescription);
                    }
                    variablesConfigFile.setDescribedVariable(describedVariable);
                    changed = true;
                }
            }
        }
        if (changed) {
            variablesConfigFile.saveToFile();
            log.logBasic("Configuration file '" + configFile + "' was modified.");
        }
        return changed;
    } catch (Exception e) {
        throw new HopException("Error managing a configuration file", e);
    }
}
Also used : DescribedVariable(org.apache.hop.core.config.DescribedVariable) HopException(org.apache.hop.core.exception.HopException) DescribedVariablesConfigFile(org.apache.hop.core.config.DescribedVariablesConfigFile) HopException(org.apache.hop.core.exception.HopException)

Example 3 with DescribedVariablesConfigFile

use of org.apache.hop.core.config.DescribedVariablesConfigFile 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)

Example 4 with DescribedVariablesConfigFile

use of org.apache.hop.core.config.DescribedVariablesConfigFile in project hop by apache.

the class ManageEnvironmentsOptionPlugin method validateConfigFiles.

private void validateConfigFiles(ILogChannel log, IVariables variables, LifecycleEnvironment environment) throws Exception {
    if (environment == null || environmentConfigFiles == null) {
        return;
    }
    for (String environmentConfigFilename : environment.getConfigurationFiles()) {
        String realEnvConfFilename = variables.resolve(environmentConfigFilename);
        DescribedVariablesConfigFile variablesConfigFile = new DescribedVariablesConfigFile(realEnvConfFilename);
        // Create the config file if it doesn't exist
        if (!HopVfs.getFileObject(realEnvConfFilename).exists()) {
            variablesConfigFile.saveToFile();
            log.logBasic("Created empty environment configuration file : " + realEnvConfFilename);
        } else {
            log.logBasic("Found existing environment configuration file: " + realEnvConfFilename);
        }
    }
}
Also used : DescribedVariablesConfigFile(org.apache.hop.core.config.DescribedVariablesConfigFile)

Example 5 with DescribedVariablesConfigFile

use of org.apache.hop.core.config.DescribedVariablesConfigFile in project hop by apache.

the class HopImportVariables method callExtensionPoint.

@Override
public void callExtensionPoint(ILogChannel iLogChannel, IVariables variables, Object[] payload) throws HopException {
    String configFilename = (String) payload[0];
    IVariables collectedVariables = (IVariables) payload[1];
    try {
        DescribedVariablesConfigFile configFile = new DescribedVariablesConfigFile(configFilename);
        if (HopVfs.fileExists(configFilename)) {
            configFile.readFromFile();
        }
        for (String variableName : collectedVariables.getVariableNames()) {
            String value = collectedVariables.getVariable(variableName);
            DescribedVariable describedVariable = configFile.findDescribedVariable(variableName);
            if (describedVariable == null) {
                describedVariable = new DescribedVariable();
                describedVariable.setDescription("Imported from Kettle");
            }
            describedVariable.setName(variableName);
            describedVariable.setValue(value);
            configFile.setDescribedVariable(describedVariable);
        }
        // Save the file...
        // 
        configFile.saveToFile();
    } catch (Exception e) {
        throw new HopException("Error importing variables to environment config file '" + configFilename, e);
    }
}
Also used : DescribedVariable(org.apache.hop.core.config.DescribedVariable) HopException(org.apache.hop.core.exception.HopException) IVariables(org.apache.hop.core.variables.IVariables) DescribedVariablesConfigFile(org.apache.hop.core.config.DescribedVariablesConfigFile) HopException(org.apache.hop.core.exception.HopException)

Aggregations

DescribedVariablesConfigFile (org.apache.hop.core.config.DescribedVariablesConfigFile)5 HopException (org.apache.hop.core.exception.HopException)4 DescribedVariable (org.apache.hop.core.config.DescribedVariable)3 FileObject (org.apache.commons.vfs2.FileObject)2 IConfigFile (org.apache.hop.core.config.IConfigFile)1 ConfigFile (org.apache.hop.core.config.plugin.ConfigFile)1 IVariables (org.apache.hop.core.variables.IVariables)1 ErrorDialog (org.apache.hop.ui.core.dialog.ErrorDialog)1