use of org.apache.nifi.toolkit.cli.api.Session in project nifi by apache.
the class CLIMain method createContext.
private static Context createContext(final PrintStream output, final boolean isInteractive) {
Session session;
final String userHomeValue = System.getProperty("user.home");
final File userHome = Paths.get(userHomeValue).toFile();
if (!userHome.exists() || !userHome.canRead() || !userHome.canWrite()) {
session = new InMemorySession();
if (isInteractive) {
output.println();
output.println("Unable to create session from " + userHomeValue + ", falling back to in-memory session");
output.println();
}
} else {
final InMemorySession inMemorySession = new InMemorySession();
final File sessionState = new File(userHome.getAbsolutePath(), SESSION_PERSISTENCE_FILE);
try {
if (!sessionState.exists()) {
sessionState.createNewFile();
}
final PersistentSession persistentSession = new PersistentSession(sessionState, inMemorySession);
persistentSession.loadSession();
session = persistentSession;
if (isInteractive) {
output.println();
output.println("Session loaded from " + sessionState.getAbsolutePath());
output.println();
}
} catch (Exception e) {
session = inMemorySession;
if (isInteractive) {
output.println();
output.println("Unable to load session from " + sessionState.getAbsolutePath() + ", falling back to in-memory session");
output.println();
}
}
}
final ClientFactory<NiFiClient> niFiClientFactory = new NiFiClientFactory();
final ClientFactory<NiFiRegistryClient> nifiRegClientFactory = new NiFiRegistryClientFactory();
return new StandardContext.Builder().output(output).session(session).nifiClientFactory(niFiClientFactory).nifiRegistryClientFactory(nifiRegClientFactory).interactive(isInteractive).build();
}
use of org.apache.nifi.toolkit.cli.api.Session 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.Session 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.Session 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.Session in project nifi by apache.
the class NiFiCLIMainRunner method main.
public static void main(String[] args) {
final String[] cmdArgs = ("registry list-buckets help " + "").split("[ ]");
final Session session = new InMemorySession();
final ClientFactory<NiFiClient> niFiClientFactory = new NiFiClientFactory();
final ClientFactory<NiFiRegistryClient> nifiRegClientFactory = new NiFiRegistryClientFactory();
final Context context = new StandardContext.Builder().output(System.out).session(session).nifiClientFactory(niFiClientFactory).nifiRegistryClientFactory(nifiRegClientFactory).build();
final Map<String, Command> commands = CommandFactory.createTopLevelCommands(context);
final Map<String, CommandGroup> commandGroups = CommandFactory.createCommandGroups(context);
final CommandProcessor processor = new CommandProcessor(commands, commandGroups, context);
processor.process(cmdArgs);
}
Aggregations