Search in sources :

Example 1 with PersistentSession

use of org.apache.nifi.toolkit.cli.impl.session.PersistentSession 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)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 NiFiRegistryClient (org.apache.nifi.registry.client.NiFiRegistryClient)1 Session (org.apache.nifi.toolkit.cli.api.Session)1 NiFiClientFactory (org.apache.nifi.toolkit.cli.impl.client.NiFiClientFactory)1 NiFiRegistryClientFactory (org.apache.nifi.toolkit.cli.impl.client.NiFiRegistryClientFactory)1 NiFiClient (org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient)1 InMemorySession (org.apache.nifi.toolkit.cli.impl.session.InMemorySession)1 PersistentSession (org.apache.nifi.toolkit.cli.impl.session.PersistentSession)1 EndOfFileException (org.jline.reader.EndOfFileException)1 LineReaderBuilder (org.jline.reader.LineReaderBuilder)1 UserInterruptException (org.jline.reader.UserInterruptException)1 TerminalBuilder (org.jline.terminal.TerminalBuilder)1