@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();
} else {
return getName();
}
}
@Override
public Completer getCompleter(final boolean scoped) {
final ArgumentCompleter completer = new ArgumentCompleter(oldCommand, getScope(), getName(), scoped);
return new Completer() {
@Override
public int complete(Session session, CommandLine commandLine, List<String> candidates) {
return completer.complete(session, commandLine, candidates);
}
};
}
@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();
} else {
return getName();
}
}
@Override
public Completer getCompleter(final boolean scoped) {
final OldArgumentCompleter completer = new OldArgumentCompleter(oldCommand, getScope(), getName(), scoped);
return new Completer() {
@Override
public int complete(Session session, CommandLine commandLine, List<String> candidates) {
return completer.complete(session, commandLine, candidates);
}
};
}
@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;
}