Search in sources :

Example 1 with SessionException

use of org.apache.nifi.toolkit.cli.api.SessionException 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 2 with SessionException

use of org.apache.nifi.toolkit.cli.api.SessionException 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 3 with SessionException

use of org.apache.nifi.toolkit.cli.api.SessionException in project nifi by apache.

the class PersistentSession method loadSession.

public synchronized void loadSession() throws SessionException {
    wrappedSession.clear();
    try (final InputStream in = new FileInputStream(persistenceFile)) {
        final Properties properties = new Properties();
        properties.load(in);
        for (final String propName : properties.stringPropertyNames()) {
            final String propValue = properties.getProperty(propName);
            wrappedSession.set(propName, propValue);
        }
    } catch (Exception e) {
        throw new SessionException("Error loading session: " + e.getMessage(), e);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SessionException(org.apache.nifi.toolkit.cli.api.SessionException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) SessionException(org.apache.nifi.toolkit.cli.api.SessionException)

Example 4 with SessionException

use of org.apache.nifi.toolkit.cli.api.SessionException in project nifi by apache.

the class PersistentSession method saveSession.

private void saveSession() throws SessionException {
    try (final OutputStream out = new FileOutputStream(persistenceFile)) {
        final Properties properties = new Properties();
        for (String variable : wrappedSession.keys()) {
            String value = wrappedSession.get(variable);
            properties.setProperty(variable, value);
        }
        properties.store(out, null);
        out.flush();
    } catch (Exception e) {
        throw new SessionException("Error saving session: " + e.getMessage(), e);
    }
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) SessionException(org.apache.nifi.toolkit.cli.api.SessionException) Properties(java.util.Properties) SessionException(org.apache.nifi.toolkit.cli.api.SessionException)

Aggregations

SessionException (org.apache.nifi.toolkit.cli.api.SessionException)4 Properties (java.util.Properties)2 CommandException (org.apache.nifi.toolkit.cli.api.CommandException)2 Session (org.apache.nifi.toolkit.cli.api.Session)2 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1 StringResult (org.apache.nifi.toolkit.cli.impl.result.StringResult)1