use of org.apache.nifi.toolkit.cli.api.CommandException 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);
}
}
use of org.apache.nifi.toolkit.cli.api.CommandException in project nifi by apache.
the class AbstractCompositeCommand method execute.
@Override
public final R execute(final CommandLine cli) throws CommandException {
try {
final Properties nifiProperties = createProperties(cli, CommandOption.NIFI_PROPS, SessionVariable.NIFI_CLIENT_PROPS);
if (nifiProperties == null) {
throw new CommandException("Unable to find NiFi config, must specify --" + CommandOption.NIFI_PROPS.getLongName() + ", or setup session config");
}
final ClientFactory<NiFiClient> nifiClientFactory = getContext().getNiFiClientFactory();
final NiFiClient nifiClient = nifiClientFactory.createClient(nifiProperties);
final Properties registryProperties = createProperties(cli, CommandOption.NIFI_REG_PROPS, SessionVariable.NIFI_REGISTRY_CLIENT_PROPS);
if (registryProperties == null) {
throw new CommandException("Unable to find NiFi Registry config, must specify --" + CommandOption.NIFI_REG_PROPS.getLongName() + ", or setup session config");
}
final ClientFactory<NiFiRegistryClient> registryClientFactory = getContext().getNiFiRegistryClientFactory();
final NiFiRegistryClient registryClient = registryClientFactory.createClient(registryProperties);
return doExecute(cli, nifiClient, nifiProperties, registryClient, registryProperties);
} catch (CommandException ce) {
throw ce;
} catch (Exception e) {
throw new CommandException("Error executing command '" + getName() + "' : " + e.getMessage(), e);
}
}
use of org.apache.nifi.toolkit.cli.api.CommandException in project nifi by apache.
the class GetVariable method execute.
@Override
public StringResult execute(final CommandLine commandLine) throws CommandException {
final String[] args = commandLine.getArgs();
if (args == null || args.length != 1 || StringUtils.isBlank(args[0])) {
throw new CommandException("Incorrect number of arguments, should be: <var>");
}
final Session session = getContext().getSession();
try {
final String value = session.get(args[0]);
if (value == null) {
return new StringResult("", getContext().isInteractive());
} else {
return new StringResult(value, getContext().isInteractive());
}
} catch (SessionException se) {
throw new CommandException(se.getMessage(), se);
}
}
use of org.apache.nifi.toolkit.cli.api.CommandException in project nifi by apache.
the class ShowSession method execute.
@Override
public VoidResult execute(final CommandLine cli) throws CommandException {
try {
final Session session = getContext().getSession();
final PrintStream printStream = getContext().getOutput();
session.printVariables(printStream);
return VoidResult.getInstance();
} catch (SessionException se) {
throw new CommandException(se.getMessage(), se);
}
}
use of org.apache.nifi.toolkit.cli.api.CommandException in project nifi by apache.
the class GetRegistryClientId method doExecute.
@Override
public RegistryClientIDResult doExecute(final NiFiClient client, final Properties properties) throws NiFiClientException, IOException, CommandException {
final String regClientName = getArg(properties, CommandOption.REGISTRY_CLIENT_NAME);
final String regClientUrl = getArg(properties, CommandOption.REGISTRY_CLIENT_URL);
if (!StringUtils.isBlank(regClientName) && !StringUtils.isBlank(regClientUrl)) {
throw new CommandException("Name and URL cannot be specified at the same time");
}
if (StringUtils.isBlank(regClientName) && StringUtils.isBlank(regClientUrl)) {
throw new CommandException("Name or URL must be specified");
}
final RegistryClientsEntity registries = client.getControllerClient().getRegistryClients();
RegistryDTO registry;
if (!StringUtils.isBlank(regClientName)) {
registry = registries.getRegistries().stream().map(r -> r.getComponent()).filter(r -> r.getName().equalsIgnoreCase(regClientName.trim())).findFirst().orElse(null);
} else {
registry = registries.getRegistries().stream().map(r -> r.getComponent()).filter(r -> r.getUri().equalsIgnoreCase(regClientUrl.trim())).findFirst().orElse(null);
}
if (registry == null) {
throw new NiFiClientException("No registry client exists with the name '" + regClientName + "'");
} else {
return new RegistryClientIDResult(getResultType(properties), registry);
}
}
Aggregations