use of org.apache.felix.service.command.Function in project karaf by apache.
the class CommandTracker method addingService.
@Override
public List<Command> addingService(final ServiceReference<Object> reference) {
final String scope = reference.getProperty(CommandProcessor.COMMAND_SCOPE).toString();
final Object function = reference.getProperty(CommandProcessor.COMMAND_FUNCTION);
final List<String> names = new ArrayList<>();
if (function.getClass().isArray()) {
for (final Object f : ((Object[]) function)) {
names.add(f.toString());
}
} else {
names.add(function.toString());
}
List<Command> commands = new ArrayList<>();
for (String name : names) {
final Function target = new CommandProxy(context, reference, name);
Command command = new Command() {
@Override
public String getScope() {
return scope;
}
@Override
public String getName() {
return name;
}
@Override
public String getDescription() {
Object property = reference.getProperty("osgi.command.description");
if (property != null) {
return property.toString();
} else {
return getName();
}
}
@Override
public Completer getCompleter(final boolean scoped) {
return null;
}
@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 target.execute(commandSession, arguments);
}
};
sessionFactory.getRegistry().register(command);
commands.add(command);
}
return commands;
}
use of org.apache.felix.service.command.Function in project karaf by apache.
the class ArgumentCompleter method complete.
public int complete(final String buffer, final int cursor, final List<String> candidates) {
ArgumentList list = delimit(buffer, cursor);
int argpos = list.getArgumentPosition();
int argIndex = list.getCursorArgumentIndex();
//Store the argument list so that it can be used by completers.
CommandSession commandSession = CommandSessionHolder.getSession();
if (commandSession != null) {
commandSession.put(ARGUMENTS_LIST, list);
}
Completer comp = null;
String[] args = list.getArguments();
int index = 0;
// First argument is command name
if (index < argIndex) {
// Verify command name
if (!verifyCompleter(commandCompleter, args[index])) {
return -1;
}
// - we have a session
if (!args[index].contains(":") && commandSession != null) {
Function f1 = unProxy((Function) commandSession.get("*:" + args[index]));
Function f2 = unProxy(this.function);
if (f1 != null && f1 != f2) {
return -1;
}
}
index++;
} else {
comp = commandCompleter;
}
// Now, check options
if (comp == null) {
while (index < argIndex && args[index].startsWith("-")) {
if (!verifyCompleter(optionsCompleter, args[index])) {
return -1;
}
Option option = options.get(args[index]);
if (option == null) {
return -1;
}
Field field = fields.get(option);
if (field != null && field.getType() != boolean.class && field.getType() != Boolean.class) {
if (++index == argIndex) {
comp = NullCompleter.INSTANCE;
}
}
index++;
}
if (comp == null && index >= argIndex && index < args.length && args[index].startsWith("-")) {
comp = optionsCompleter;
}
}
//Now check for if last Option has a completer
int lastAgurmentIndex = argIndex - 1;
if (lastAgurmentIndex >= 1) {
Option lastOption = options.get(args[lastAgurmentIndex]);
if (lastOption != null) {
Field lastField = fields.get(lastOption);
if (lastField != null && lastField.getType() != boolean.class && lastField.getType() != Boolean.class) {
Option option = lastField.getAnnotation(Option.class);
if (option != null) {
Completer optionValueCompleter = null;
String name = option.name();
if (optionalCompleters != null && name != null) {
optionValueCompleter = optionalCompleters.get(name);
if (optionValueCompleter == null) {
String[] aliases = option.aliases();
if (aliases.length > 0) {
for (int i = 0; i < aliases.length && optionValueCompleter == null; i++) {
optionValueCompleter = optionalCompleters.get(option.aliases()[i]);
}
}
}
}
if (optionValueCompleter != null) {
comp = optionValueCompleter;
}
}
}
}
}
// Check arguments
if (comp == null) {
int indexArg = 0;
while (index < argIndex) {
Completer sub = argsCompleters.get(indexArg >= argsCompleters.size() ? argsCompleters.size() - 1 : indexArg);
if (!verifyCompleter(sub, args[index])) {
return -1;
}
index++;
indexArg++;
}
comp = argsCompleters.get(indexArg >= argsCompleters.size() ? argsCompleters.size() - 1 : indexArg);
}
int ret = comp.complete(list.getCursorArgument(), argpos, candidates);
if (ret == -1) {
return -1;
}
int pos = ret + (list.getBufferPosition() - argpos);
if ((buffer != null) && (cursor != buffer.length()) && isDelimiter(buffer, cursor)) {
for (int i = 0; i < candidates.size(); i++) {
String val = candidates.get(i);
while ((val.length() > 0) && isDelimiter(val, val.length() - 1)) {
val = val.substring(0, val.length() - 1);
}
candidates.set(i, val);
}
}
return pos;
}
use of org.apache.felix.service.command.Function in project karaf by apache.
the class SecuredCommandProcessorImpl method trackCommands.
private ServiceTracker<Object, Object> trackCommands(final BundleContext context, String roleClause) throws InvalidSyntaxException {
Filter filter = context.createFilter(String.format("(&(%s=*)(%s=*)%s)", CommandProcessor.COMMAND_SCOPE, CommandProcessor.COMMAND_FUNCTION, roleClause));
return new ServiceTracker<Object, Object>(context, filter, null) {
@Override
public Object addingService(ServiceReference<Object> reference) {
Object scope = reference.getProperty(CommandProcessor.COMMAND_SCOPE);
Object function = reference.getProperty(CommandProcessor.COMMAND_FUNCTION);
List<Object> commands = new ArrayList<>();
if (scope != null && function != null) {
if (function.getClass().isArray()) {
for (Object f : ((Object[]) function)) {
Function target = new CommandProxy(context, reference, f.toString());
addCommand(scope.toString(), target, f.toString());
commands.add(target);
}
} else {
Function target = new CommandProxy(context, reference, function.toString());
addCommand(scope.toString(), target, function.toString());
commands.add(target);
}
return commands;
}
return null;
}
@Override
public void removedService(ServiceReference<Object> reference, Object service) {
Object scope = reference.getProperty(CommandProcessor.COMMAND_SCOPE);
Object function = reference.getProperty(CommandProcessor.COMMAND_FUNCTION);
if (scope != null && function != null) {
if (!function.getClass().isArray()) {
removeCommand(scope.toString(), function.toString());
} else {
for (Object func : (Object[]) function) {
removeCommand(scope.toString(), func.toString());
}
}
}
super.removedService(reference, service);
}
};
}
use of org.apache.felix.service.command.Function in project karaf by apache.
the class CommandsCompleter method checkData.
@SuppressWarnings({ "unchecked", "deprecation" })
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) };
}
}
use of org.apache.felix.service.command.Function in project karaf by apache.
the class CommandsCompleter method unProxy.
public static Function unProxy(Function function) {
if (function == null || function.getClass() != CommandProxy.class) {
return function;
}
CommandProxy proxy = (CommandProxy) function;
Object target = proxy.getTarget();
try {
return target instanceof Function ? (Function) target : function;
} finally {
proxy.ungetTarget();
}
}
Aggregations