use of org.apache.felix.gogo.runtime.CommandProxy 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() {
for (Method method : getMethods()) {
Descriptor d = method.getAnnotation(Descriptor.class);
if (d != null) {
return d.value();
}
}
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);
}
private List<Method> getMethods() {
Object target = context.getService(reference);
List<Method> methods = new ArrayList<>();
String func = name.substring(name.indexOf(':') + 1).toLowerCase();
List<String> funcs = new ArrayList<>();
funcs.add("is" + func);
funcs.add("get" + func);
funcs.add("set" + func);
if (Reflective.KEYWORDS.contains(func)) {
funcs.add("_" + func);
} else {
funcs.add(func);
}
for (Method method : target.getClass().getMethods()) {
if (funcs.contains(method.getName().toLowerCase())) {
methods.add(method);
}
}
context.ungetService(reference);
return methods;
}
};
sessionFactory.getRegistry().register(command);
commands.add(command);
}
return commands;
}
use of org.apache.felix.gogo.runtime.CommandProxy in project felix by apache.
the class Shell method getReflectionCommands.
private Map<String, List<Method>> getReflectionCommands(CommandSession session) {
Map<String, List<Method>> commands = new TreeMap<>();
Set<String> names = getCommands(session);
for (String name : names) {
Function function = (Function) session.get(name);
if (function instanceof CommandProxy) {
Object target = ((CommandProxy) function).getTarget();
List<Method> methods = new ArrayList<>();
String func = name.substring(name.indexOf(':') + 1).toLowerCase();
List<String> funcs = new ArrayList<>();
funcs.add("is" + func);
funcs.add("get" + func);
funcs.add("set" + func);
if (Reflective.KEYWORDS.contains(func)) {
funcs.add("_" + func);
} else {
funcs.add(func);
}
for (Method method : target.getClass().getMethods()) {
if (funcs.contains(method.getName().toLowerCase())) {
methods.add(method);
}
}
commands.put(name, methods);
((CommandProxy) function).ungetTarget();
}
}
return commands;
}
use of org.apache.felix.gogo.runtime.CommandProxy in project felix by apache.
the class Activator method trackOSGiCommands.
private ServiceTracker trackOSGiCommands(final BundleContext context) throws InvalidSyntaxException {
Filter filter = context.createFilter(String.format("(&(%s=*)(%s=*))", CommandProcessor.COMMAND_SCOPE, CommandProcessor.COMMAND_FUNCTION));
return new ServiceTracker<Object, List<Object>>(context, filter, null) {
private final ConcurrentMap<ServiceReference<Object>, Map<String, CommandProxy>> proxies = new ConcurrentHashMap<>();
@Override
public List<Object> addingService(ServiceReference<Object> reference) {
Object scope = reference.getProperty(CommandProcessor.COMMAND_SCOPE);
Object function = reference.getProperty(CommandProcessor.COMMAND_FUNCTION);
Object ranking = reference.getProperty(Constants.SERVICE_RANKING);
List<Object> commands = new ArrayList<>();
int rank = 0;
if (ranking != null) {
try {
rank = Integer.parseInt(ranking.toString());
} catch (NumberFormatException e) {
// Ignore
}
}
if (scope != null && function != null) {
Map<String, CommandProxy> proxyMap = new HashMap<>();
if (function.getClass().isArray()) {
for (Object f : ((Object[]) function)) {
CommandProxy target = new CommandProxy(context, reference, f.toString());
proxyMap.put(f.toString(), target);
processor.addCommand(scope.toString(), target, f.toString(), rank);
commands.add(target);
}
} else {
CommandProxy target = new CommandProxy(context, reference, function.toString());
proxyMap.put(function.toString(), target);
processor.addCommand(scope.toString(), target, function.toString(), rank);
commands.add(target);
}
proxies.put(reference, proxyMap);
return commands;
}
return null;
}
@Override
public void removedService(ServiceReference<Object> reference, List<Object> service) {
Object scope = reference.getProperty(CommandProcessor.COMMAND_SCOPE);
Object function = reference.getProperty(CommandProcessor.COMMAND_FUNCTION);
if (scope != null && function != null) {
Map<String, CommandProxy> proxyMap = proxies.remove(reference);
for (Map.Entry<String, CommandProxy> entry : proxyMap.entrySet()) {
processor.removeCommand(scope.toString(), entry.getKey(), entry.getValue());
}
}
super.removedService(reference, service);
}
};
}
use of org.apache.felix.gogo.runtime.CommandProxy 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.gogo.runtime.CommandProxy 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