use of org.apache.karaf.shell.api.console.Command in project karaf by apache.
the class ShellHelpProvider method getHelp.
public String getHelp(Session session, String path) {
if (path.indexOf('|') > 0) {
if (path.startsWith("shell|")) {
path = path.substring("shell|".length());
} else {
return null;
}
}
// Retrieve matching commands
Set<Command> commands = getCommands(session, path);
// Compute the scopes and matching bundles
Set<Bundle> bundles = new HashSet<>();
Set<String> scopes = new HashSet<>();
for (Command command : commands) {
if (command instanceof ActionCommand) {
Class<? extends Action> action = ((ActionCommand) command).getActionClass();
bundles.add(FrameworkUtil.getBundle(action));
}
scopes.add(command.getScope());
}
// use that one instead
if (scopes.size() == 1 && bundles.size() == 1 && path.equals(scopes.iterator().next())) {
Bundle bundle = bundles.iterator().next();
URL resource = bundle.getResource("OSGI-INF/shell-" + path + ".info");
if (resource != null) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource.openStream()))) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
int maxSize = 80;
Terminal terminal = session.getTerminal();
if (terminal != null) {
maxSize = terminal.getWidth();
}
WikiVisitor visitor = new AnsiPrintingWikiVisitor(ps, maxSize);
WikiParser parser = new WikiParser(visitor);
parser.parse(reader);
return baos.toString();
} catch (IOException e) {
// ignore
}
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
printShellHelp(session, new PrintStream(baos), path);
return baos.toString();
}
return null;
}
use of org.apache.karaf.shell.api.console.Command 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);
}
use of org.apache.karaf.shell.api.console.Command in project karaf by apache.
the class CommandsCompleter method checkData.
@SuppressWarnings("unchecked")
protected Map<String, Completer>[] checkData() {
// Copy the set to avoid concurrent modification exceptions
// TODO: fix that in gogo instead
Collection<Command> commands;
boolean update;
synchronized (this) {
commands = factory.getRegistry().getCommands();
update = !commands.equals(this.commands);
}
if (update) {
// get command aliases
Map<String, Completer> global = new HashMap<>();
Map<String, Completer> local = new HashMap<>();
// add argument completers for each command
for (Command command : commands) {
String key = command.getScope() + ":" + command.getName();
Completer cg = command.getCompleter(false);
Completer cl = command.getCompleter(true);
if (cg == null) {
if (Session.SCOPE_GLOBAL.equals(command.getScope())) {
cg = new FixedSimpleCommandCompleter(Collections.singletonList(command.getName()));
} else {
cg = new FixedSimpleCommandCompleter(Arrays.asList(key, command.getName()));
}
}
if (cl == null) {
cl = new FixedSimpleCommandCompleter(Collections.singletonList(command.getName()));
}
global.put(key, cg);
local.put(key, cl);
}
synchronized (this) {
this.commands.clear();
this.globalCompleters.clear();
this.localCompleters.clear();
this.commands.addAll(commands);
this.globalCompleters.putAll(global);
this.localCompleters.putAll(local);
}
}
synchronized (this) {
return new Map[] { new HashMap<>(this.globalCompleters), new HashMap<>(this.localCompleters) };
}
}
use of org.apache.karaf.shell.api.console.Command in project karaf by apache.
the class RegistryImpl method register.
@Override
public void register(Object service) {
synchronized (services) {
services.put(service, service);
if (service instanceof Command) {
Command cmd = (Command) service;
String key = cmd.getScope() + ":" + cmd.getName();
commands.computeIfAbsent(key, k -> new ArrayList<>()).add(cmd);
}
}
}
use of org.apache.karaf.shell.api.console.Command in project karaf by apache.
the class RegistryImpl method unregister.
@Override
public void unregister(Object service) {
synchronized (services) {
services.remove(service);
if (service instanceof Command) {
Command cmd = (Command) service;
String key = cmd.getScope() + ":" + cmd.getName();
List<Command> cmds = commands.get(key);
if (cmds != null) {
cmds.remove(cmd);
if (cmds.isEmpty()) {
commands.remove(key);
}
}
}
}
}
Aggregations