Search in sources :

Example 1 with CommandProxy

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;
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) Function(org.apache.felix.service.command.Function) CommandProxy(org.apache.felix.gogo.runtime.CommandProxy) CommandSession(org.apache.felix.service.command.CommandSession) Descriptor(org.apache.felix.service.command.Descriptor) List(java.util.List) ArrayList(java.util.ArrayList) CommandSession(org.apache.felix.service.command.CommandSession)

Example 2 with CommandProxy

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;
}
Also used : Function(org.apache.felix.service.command.Function) CommandProxy(org.apache.felix.gogo.runtime.CommandProxy) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) TreeMap(java.util.TreeMap)

Example 3 with CommandProxy

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);
        }
    };
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) ArrayList(java.util.ArrayList) ServiceReference(org.osgi.framework.ServiceReference) CommandProxy(org.apache.felix.gogo.runtime.CommandProxy) Filter(org.osgi.framework.Filter) ArrayList(java.util.ArrayList) List(java.util.List) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Example 4 with CommandProxy

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);
        }
    };
}
Also used : Function(org.apache.felix.service.command.Function) CommandProxy(org.apache.felix.gogo.runtime.CommandProxy) Filter(org.osgi.framework.Filter) ServiceTracker(org.osgi.util.tracker.ServiceTracker) ArrayList(java.util.ArrayList) ServiceReference(org.osgi.framework.ServiceReference)

Example 5 with CommandProxy

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();
    }
}
Also used : Function(org.apache.felix.service.command.Function) CommandProxy(org.apache.felix.gogo.runtime.CommandProxy)

Aggregations

CommandProxy (org.apache.felix.gogo.runtime.CommandProxy)5 ArrayList (java.util.ArrayList)4 Function (org.apache.felix.service.command.Function)4 List (java.util.List)3 Method (java.lang.reflect.Method)2 Filter (org.osgi.framework.Filter)2 ServiceReference (org.osgi.framework.ServiceReference)2 ServiceTracker (org.osgi.util.tracker.ServiceTracker)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 CommandSession (org.apache.felix.service.command.CommandSession)1 Descriptor (org.apache.felix.service.command.Descriptor)1