use of org.apache.karaf.shell.api.console.Session in project karaf by apache.
the class ShellCommand method run.
public void run() {
int exitStatus = 0;
try {
final Session session = sessionFactory.create(in, new PrintStream(out), new PrintStream(err));
for (Map.Entry<String, String> e : env.getEnv().entrySet()) {
session.put(e.getKey(), e.getValue());
}
try {
Subject subject = this.session != null ? this.session.getAttribute(KarafJaasAuthenticator.SUBJECT_ATTRIBUTE_KEY) : null;
Object result;
if (subject != null) {
try {
result = JaasHelper.doAs(subject, (PrivilegedExceptionAction<Object>) () -> {
String scriptFileName = System.getProperty(EXEC_INIT_SCRIPT);
if (scriptFileName == null) {
scriptFileName = System.getProperty(SHELL_INIT_SCRIPT);
}
executeScript(scriptFileName, session);
return session.execute(command);
});
} catch (PrivilegedActionException e) {
throw e.getException();
}
} else {
String scriptFileName = System.getProperty(EXEC_INIT_SCRIPT);
if (scriptFileName == null) {
scriptFileName = System.getProperty(SHELL_INIT_SCRIPT);
}
executeScript(scriptFileName, session);
result = session.execute(command);
}
if (result != null) {
// TODO: print the result of the command ?
// session.getConsole().println(session.format(result, Converter.INSPECT));
}
} catch (Throwable t) {
exitStatus = 1;
ShellUtil.logException(session, t);
}
} catch (Exception e) {
exitStatus = 1;
LOGGER.error("Unable to start shell", e);
} finally {
StreamUtils.close(in, out, err);
callback.onExit(exitStatus);
}
}
use of org.apache.karaf.shell.api.console.Session in project karaf by apache.
the class SuCommand method execute.
@Override
public Object execute() throws Exception {
Subject subject = new Subject();
LoginContext loginContext = new LoginContext(realm, subject, callbacks -> {
for (Callback callback : callbacks) {
if (callback instanceof NameCallback) {
((NameCallback) callback).setName(user);
} else if (callback instanceof PasswordCallback) {
String password = SuCommand.this.session.readLine("Password: ", '*');
((PasswordCallback) callback).setPassword(password.toCharArray());
} else {
throw new UnsupportedCallbackException(callback);
}
}
});
loginContext.login();
JaasHelper.doAs(subject, (PrivilegedExceptionAction<Object>) () -> {
final Session newSession = session.getFactory().create(System.in, System.out, System.err, SuCommand.this.session.getTerminal(), null, null);
Object oldIgnoreInterrupts = session.get(Session.IGNORE_INTERRUPTS);
try {
session.put(Session.IGNORE_INTERRUPTS, Boolean.TRUE);
String name = "Karaf local console user " + ShellUtil.getCurrentUserName();
Thread thread = new Thread(newSession, name);
thread.start();
thread.join();
} finally {
session.put(Session.IGNORE_INTERRUPTS, oldIgnoreInterrupts);
}
return null;
});
loginContext.logout();
return null;
}
use of org.apache.karaf.shell.api.console.Session in project karaf by apache.
the class HelpCommand method getCompleter.
@Override
public Completer getCompleter(final boolean scoped) {
return new Completer() {
@Override
public int complete(Session session, CommandLine commandLine, List<String> candidates) {
String[] args = commandLine.getArguments();
int argIndex = commandLine.getCursorArgumentIndex();
StringsCompleter completer = new StringsCompleter(Collections.singletonList(getName()));
if (argIndex == 0) {
return completer.complete(session, new ArgumentCommandLine(args[argIndex], commandLine.getArgumentPosition()), candidates);
} else if (!verifyCompleter(session, completer, args[0])) {
return -1;
}
// TODO: use CommandNamesCompleter and better completion wrt parsing etc...
completer = new StringsCompleter();
for (Command command : session.getRegistry().getCommands()) {
if (!Session.SCOPE_GLOBAL.equals(command.getScope())) {
completer.getStrings().add(command.getScope() + ":" + command.getName());
}
completer.getStrings().add(command.getName());
}
completer.getStrings().add("--help");
if (argIndex == 1) {
int res;
if (argIndex < args.length) {
res = completer.complete(session, new ArgumentCommandLine(args[argIndex], commandLine.getArgumentPosition()), candidates);
} else {
res = completer.complete(session, new ArgumentCommandLine("", 0), candidates);
}
return res + (commandLine.getBufferPosition() - commandLine.getArgumentPosition());
} else if (!verifyCompleter(session, completer, args[1])) {
return -1;
}
return -1;
}
protected boolean verifyCompleter(Session session, Completer completer, String argument) {
List<String> candidates = new ArrayList<>();
return completer.complete(session, new ArgumentCommandLine(argument, argument.length()), candidates) != -1 && !candidates.isEmpty();
}
};
}
use of org.apache.karaf.shell.api.console.Session in project karaf by apache.
the class SingleCommandHelpProvider method getHelp.
public String getHelp(Session session, String path) {
if (path.indexOf('|') > 0) {
if (path.startsWith("command|")) {
path = path.substring("command|".length());
} else {
return null;
}
}
ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos, true);
Session s = session.getFactory().create(bais, ps, ps, session);
s.put(Session.SCOPE, session.get(Session.SCOPE));
s.put(Session.SUBSHELL, session.get(Session.SUBSHELL));
try {
s.execute(path + " --help");
} catch (Throwable t) {
return null;
} finally {
s.close();
}
return baos.toString();
}
use of org.apache.karaf.shell.api.console.Session in project karaf by apache.
the class CommandWrapper method execute.
@Override
public Object execute(final CommandSession commandSession, List<Object> arguments) throws Exception {
// TODO: remove the hack for .session
Session session = (Session) commandSession.get(".session");
// When need to translate closures to a compatible type for the command
for (int i = 0; i < arguments.size(); i++) {
Object v = arguments.get(i);
if (v instanceof Closure) {
final Closure closure = (Closure) v;
arguments.set(i, (org.apache.karaf.shell.api.console.Function) (s, a) -> closure.execute(commandSession, a));
}
}
return command.execute(session, arguments);
}
Aggregations