@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { resp.setStatus(HttpServletResponse.SC_OK); PrintWriter writer = resp.getWriter(); Map<String, CommandProvider> cmds = getCommands(context); for (CommandProvider commandProvider : cmds.values()) { writer.append(commandProvider.getHelp()); } }
@Override public Object execute(String cmd) { String methodName = commandKey(cmd); Map<String, CommandProvider> commands = getCommands(context); CommandProvider commandProvider = commands.get(methodName); Class<?> providerClass = commandProvider.getClass(); Object toReturn = null; try { Method method = providerClass.getMethod(methodName, CommandInterpreter.class); toReturn = method.invoke(commandProvider, this); } catch (Exception ex) { print(ex); } return toReturn; }
private static Map<String, CommandProvider> getCommands(BundleContext context) { Map<String, CommandProvider> data = new HashMap<String, CommandProvider>(); ServiceTracker<CommandProvider, CommandProvider> tracker = new ServiceTracker<CommandProvider, CommandProvider>(context, CommandProvider.class, null); tracker.open(true); try { Object[] services = tracker.getServices(); for (Object service : services) { CommandProvider commandProvider = (CommandProvider) service; for (Method method : commandProvider.getClass().getMethods()) { String methodName = method.getName(); if (methodName.startsWith("_")) { data.put(methodName, commandProvider); } } } } finally { OsgiUtil.close(tracker); } return data; }