Search in sources :

Example 1 with SessionVariable

use of org.apache.nifi.toolkit.cli.impl.session.SessionVariable in project nifi by apache.

the class AbstractPropertyCommand method execute.

@Override
public final R execute(final CommandLine commandLine) throws CommandException {
    try {
        final Properties properties = new Properties();
        // start by loading the properties file if it was specified
        if (commandLine.hasOption(CommandOption.PROPERTIES.getLongName())) {
            final String propertiesFile = commandLine.getOptionValue(CommandOption.PROPERTIES.getLongName());
            if (!StringUtils.isBlank(propertiesFile)) {
                try (final InputStream in = new FileInputStream(propertiesFile)) {
                    properties.load(in);
                }
            }
        } else {
            // no properties file was specified so see if there is anything in the session
            final SessionVariable sessionVariable = getPropertiesSessionVariable();
            if (sessionVariable != null) {
                final Session session = getContext().getSession();
                final String sessionPropsFiles = session.get(sessionVariable.getVariableName());
                if (!StringUtils.isBlank(sessionPropsFiles)) {
                    try (final InputStream in = new FileInputStream(sessionPropsFiles)) {
                        properties.load(in);
                    }
                }
            }
        }
        // add in anything specified on command line, and override anything that was already there
        for (final Option option : commandLine.getOptions()) {
            final String optValue = option.getValue() == null ? "" : option.getValue();
            properties.setProperty(option.getLongOpt(), optValue);
        }
        // delegate to sub-classes
        return doExecute(properties);
    } catch (CommandException ce) {
        throw ce;
    } catch (Exception e) {
        throw new CommandException("Error executing command '" + getName() + "' : " + e.getMessage(), e);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Option(org.apache.commons.cli.Option) SessionVariable(org.apache.nifi.toolkit.cli.impl.session.SessionVariable) CommandException(org.apache.nifi.toolkit.cli.api.CommandException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) CommandException(org.apache.nifi.toolkit.cli.api.CommandException) Session(org.apache.nifi.toolkit.cli.api.Session)

Example 2 with SessionVariable

use of org.apache.nifi.toolkit.cli.impl.session.SessionVariable in project nifi by apache.

the class ShowKeys method execute.

@Override
public VoidResult execute(CommandLine cli) throws CommandException {
    println();
    for (final SessionVariable variable : SessionVariable.values()) {
        println("\t" + variable.getVariableName());
    }
    println();
    return VoidResult.getInstance();
}
Also used : SessionVariable(org.apache.nifi.toolkit.cli.impl.session.SessionVariable)

Aggregations

SessionVariable (org.apache.nifi.toolkit.cli.impl.session.SessionVariable)2 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 Properties (java.util.Properties)1 Option (org.apache.commons.cli.Option)1 CommandException (org.apache.nifi.toolkit.cli.api.CommandException)1 Session (org.apache.nifi.toolkit.cli.api.Session)1