use of org.apache.karaf.shell.commands.CommandWithAction in project karaf by apache.
the class ArgumentCompleter method getCompleterValues.
private Map<Integer, Object> getCompleterValues(CommandWithAction function) {
final Map<Integer, Object> values = new HashMap<>();
Action action = null;
try {
for (Class<?> type = function.getActionClass(); type != null; type = type.getSuperclass()) {
for (Method method : type.getDeclaredMethods()) {
CompleterValues completerMethod = method.getAnnotation(CompleterValues.class);
if (completerMethod != null) {
int index = completerMethod.index();
Integer key = index;
if (index >= arguments.size() || index < 0) {
LOGGER.warn("Index out of range on @CompleterValues on class " + type.getName() + " for index: " + key + " see: " + method);
} else if (values.containsKey(key)) {
LOGGER.warn("Duplicate @CompleterMethod annotations on class " + type.getName() + " for index: " + key + " see: " + method);
} else {
try {
Object value;
if (Modifier.isStatic(method.getModifiers())) {
value = method.invoke(null);
} else {
if (action == null) {
action = function.createNewAction();
}
value = method.invoke(action);
}
values.put(key, value);
} catch (IllegalAccessException e) {
LOGGER.warn("Could not invoke @CompleterMethod on " + function + ". " + e, e);
} catch (InvocationTargetException e) {
Throwable target = e.getTargetException();
if (target == null) {
target = e;
}
LOGGER.warn("Could not invoke @CompleterMethod on " + function + ". " + target, target);
}
}
}
}
}
} finally {
if (action != null) {
try {
function.releaseAction(action);
} catch (Exception e) {
LOGGER.warn("Failed to release action: " + action + ". " + e, e);
}
}
}
return values;
}
use of org.apache.karaf.shell.commands.CommandWithAction in project karaf by apache.
the class ArgumentCompleter method getCompleterValues.
private Map<Integer, Object> getCompleterValues(CommandWithAction function) {
final Map<Integer, Object> values = new HashMap<>();
Action action = null;
try {
for (Class<?> type = function.getActionClass(); type != null; type = type.getSuperclass()) {
for (Method method : type.getDeclaredMethods()) {
CompleterValues completerMethod = method.getAnnotation(CompleterValues.class);
if (completerMethod != null) {
int index = completerMethod.index();
Integer key = index;
if (index >= arguments.size() || index < 0) {
LOGGER.warn("Index out of range on @CompleterValues on class " + type.getName() + " for index: " + key + " see: " + method);
} else if (values.containsKey(key)) {
LOGGER.warn("Duplicate @CompleterMethod annotations on class " + type.getName() + " for index: " + key + " see: " + method);
} else {
try {
Object value;
if (Modifier.isStatic(method.getModifiers())) {
value = method.invoke(null);
} else {
if (action == null) {
action = function.createNewAction();
}
value = method.invoke(action);
}
values.put(key, value);
} catch (IllegalAccessException e) {
LOGGER.warn("Could not invoke @CompleterMethod on " + function + ". " + e, e);
} catch (InvocationTargetException e) {
Throwable target = e.getTargetException();
if (target == null) {
target = e;
}
LOGGER.warn("Could not invoke @CompleterMethod on " + function + ". " + target, target);
}
}
}
}
}
} finally {
if (action != null) {
try {
function.releaseAction(action);
} catch (Exception e) {
LOGGER.warn("Failed to release action: " + action + ". " + e, e);
}
}
}
return values;
}
use of org.apache.karaf.shell.commands.CommandWithAction in project karaf by apache.
the class CommandTracker method addingService.
@Override
public Object addingService(final ServiceReference reference) {
Object service = context.getService(reference);
if (service instanceof CommandWithAction) {
final CommandWithAction oldCommand = (CommandWithAction) service;
final org.apache.karaf.shell.api.console.Command command = new org.apache.karaf.shell.api.console.Command() {
@Override
public String getScope() {
return reference.getProperty(CommandProcessor.COMMAND_SCOPE).toString();
}
@Override
public String getName() {
return reference.getProperty(CommandProcessor.COMMAND_FUNCTION).toString();
}
@Override
public String getDescription() {
final Command cmd = oldCommand.getActionClass().getAnnotation(Command.class);
if (cmd != null) {
return cmd.description();
}
try {
Method method = oldCommand.getActionClass().getMethod("description");
method.setAccessible(true);
return (String) method.invoke(oldCommand.createNewAction());
} catch (Throwable ignore) {
}
return getName();
}
@Override
public Completer getCompleter(final boolean scoped) {
final ArgumentCompleter completer = new ArgumentCompleter(oldCommand, getScope(), getName(), scoped);
return completer::complete;
}
@Override
public Parser getParser() {
return null;
}
@Override
public Object execute(Session session, List<Object> arguments) throws Exception {
// TODO: remove not really nice cast
CommandSession commandSession = (CommandSession) session.get(".commandSession");
return oldCommand.execute(commandSession, arguments);
}
};
sessionFactory.getRegistry().register(command);
return command;
} else if (service instanceof org.apache.felix.gogo.commands.CommandWithAction) {
final org.apache.felix.gogo.commands.CommandWithAction oldCommand = (org.apache.felix.gogo.commands.CommandWithAction) service;
final org.apache.karaf.shell.api.console.Command command = new org.apache.karaf.shell.api.console.Command() {
@Override
public String getScope() {
return reference.getProperty(CommandProcessor.COMMAND_SCOPE).toString();
}
@Override
public String getName() {
return reference.getProperty(CommandProcessor.COMMAND_FUNCTION).toString();
}
@Override
public String getDescription() {
final org.apache.felix.gogo.commands.Command cmd = oldCommand.getActionClass().getAnnotation(org.apache.felix.gogo.commands.Command.class);
if (cmd != null) {
return cmd.description();
}
try {
Method method = oldCommand.getActionClass().getMethod("description");
method.setAccessible(true);
return (String) method.invoke(oldCommand.createNewAction());
} catch (Throwable ignore) {
}
return getName();
}
@Override
public Completer getCompleter(final boolean scoped) {
final OldArgumentCompleter completer = new OldArgumentCompleter(oldCommand, getScope(), getName(), scoped);
return completer::complete;
}
@Override
public Parser getParser() {
return null;
}
@Override
public Object execute(Session session, List<Object> arguments) throws Exception {
// TODO: remove not really nice cast
CommandSession commandSession = (CommandSession) session.get(".commandSession");
return oldCommand.execute(commandSession, arguments);
}
};
sessionFactory.getRegistry().register(command);
return command;
} else {
return null;
}
}
use of org.apache.karaf.shell.commands.CommandWithAction 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
Set<String> names;
boolean update;
synchronized (this) {
names = new HashSet<>((Set<String>) session.get(COMMANDS));
update = !names.equals(commands);
}
if (update) {
// get command aliases
Set<String> commands = new HashSet<>();
Map<String, Completer> global = new HashMap<>();
Map<String, Completer> local = new HashMap<>();
// add argument completers for each command
for (String command : names) {
String rawCommand = stripScope(command);
Function function = (Function) session.get(command);
function = unProxy(function);
if (function instanceof CommandWithAction) {
try {
global.put(command, new ArgumentCompleter(session, (CommandWithAction) function, command));
local.put(command, new ArgumentCompleter(session, (CommandWithAction) function, rawCommand));
} catch (Throwable t) {
LOGGER.debug("Unable to create completers for command '" + command + "'", t);
}
} else if (function instanceof org.apache.felix.gogo.commands.CommandWithAction) {
try {
global.put(command, new OldArgumentCompleter(session, (org.apache.felix.gogo.commands.CommandWithAction) function, command));
local.put(command, new OldArgumentCompleter(session, (org.apache.felix.gogo.commands.CommandWithAction) function, rawCommand));
} catch (Throwable t) {
LOGGER.debug("Unable to create completers for command '" + command + "'", t);
}
}
commands.add(command);
}
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) };
}
}
Aggregations