Search in sources :

Example 1 with Session

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();
}
Also used : NiFiClientFactory(org.apache.nifi.toolkit.cli.impl.client.NiFiClientFactory) NiFiClient(org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient) NiFiRegistryClientFactory(org.apache.nifi.toolkit.cli.impl.client.NiFiRegistryClientFactory) LineReaderBuilder(org.jline.reader.LineReaderBuilder) TerminalBuilder(org.jline.terminal.TerminalBuilder) PersistentSession(org.apache.nifi.toolkit.cli.impl.session.PersistentSession) IOException(java.io.IOException) EndOfFileException(org.jline.reader.EndOfFileException) UserInterruptException(org.jline.reader.UserInterruptException) NiFiRegistryClient(org.apache.nifi.registry.client.NiFiRegistryClient) InMemorySession(org.apache.nifi.toolkit.cli.impl.session.InMemorySession) File(java.io.File) PersistentSession(org.apache.nifi.toolkit.cli.impl.session.PersistentSession) Session(org.apache.nifi.toolkit.cli.api.Session) InMemorySession(org.apache.nifi.toolkit.cli.impl.session.InMemorySession)

Example 2 with Session

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);
    }
}
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 3 with Session

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);
    }
}
Also used : SessionException(org.apache.nifi.toolkit.cli.api.SessionException) CommandException(org.apache.nifi.toolkit.cli.api.CommandException) StringResult(org.apache.nifi.toolkit.cli.impl.result.StringResult) Session(org.apache.nifi.toolkit.cli.api.Session)

Example 4 with Session

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);
    }
}
Also used : PrintStream(java.io.PrintStream) SessionException(org.apache.nifi.toolkit.cli.api.SessionException) CommandException(org.apache.nifi.toolkit.cli.api.CommandException) Session(org.apache.nifi.toolkit.cli.api.Session)

Example 5 with Session

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);
}
Also used : StandardContext(org.apache.nifi.toolkit.cli.impl.context.StandardContext) Context(org.apache.nifi.toolkit.cli.api.Context) NiFiClientFactory(org.apache.nifi.toolkit.cli.impl.client.NiFiClientFactory) NiFiClient(org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient) NiFiRegistryClientFactory(org.apache.nifi.toolkit.cli.impl.client.NiFiRegistryClientFactory) CommandGroup(org.apache.nifi.toolkit.cli.api.CommandGroup) NiFiRegistryClient(org.apache.nifi.registry.client.NiFiRegistryClient) Command(org.apache.nifi.toolkit.cli.api.Command) InMemorySession(org.apache.nifi.toolkit.cli.impl.session.InMemorySession) StandardContext(org.apache.nifi.toolkit.cli.impl.context.StandardContext) CommandProcessor(org.apache.nifi.toolkit.cli.impl.command.CommandProcessor) Session(org.apache.nifi.toolkit.cli.api.Session) InMemorySession(org.apache.nifi.toolkit.cli.impl.session.InMemorySession)

Aggregations

Session (org.apache.nifi.toolkit.cli.api.Session)7 NiFiRegistryClient (org.apache.nifi.registry.client.NiFiRegistryClient)3 CommandException (org.apache.nifi.toolkit.cli.api.CommandException)3 NiFiClientFactory (org.apache.nifi.toolkit.cli.impl.client.NiFiClientFactory)3 NiFiRegistryClientFactory (org.apache.nifi.toolkit.cli.impl.client.NiFiRegistryClientFactory)3 NiFiClient (org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient)3 InMemorySession (org.apache.nifi.toolkit.cli.impl.session.InMemorySession)3 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 Properties (java.util.Properties)2 Command (org.apache.nifi.toolkit.cli.api.Command)2 CommandGroup (org.apache.nifi.toolkit.cli.api.CommandGroup)2 Context (org.apache.nifi.toolkit.cli.api.Context)2 SessionException (org.apache.nifi.toolkit.cli.api.SessionException)2 StandardContext (org.apache.nifi.toolkit.cli.impl.context.StandardContext)2 File (java.io.File)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 Option (org.apache.commons.cli.Option)1 CommandProcessor (org.apache.nifi.toolkit.cli.impl.command.CommandProcessor)1