protected Object doExecute()

in gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/ListServices.java [39:172]


    protected Object doExecute() throws Exception {
        if (ids != null && !ids.isEmpty()) {
            for (long id : ids) {
                Bundle bundle = getBundleContext().getBundle(id);
                if (bundle != null) {
                    boolean headerPrinted = false;
                    boolean needSeparator = false;
                    ServiceReference[] refs = null;

                    // Get registered or in-use services.
                    if (inUse) {
                        refs = bundle.getServicesInUse();
                    } else {
                        refs = bundle.getRegisteredServices();
                    }

                    // Print properties for each service.
                    for (int refIdx = 0;
                         (refs != null) && (refIdx < refs.length);
                         refIdx++) {
                        String[] objectClass = (String[])
                                refs[refIdx].getProperty("objectClass");

                        // Determine if we need to print the service, depending
                        // on whether it is a command service or not.
                        boolean print = true;
                        for (int ocIdx = 0;
                             !showAll && (ocIdx < objectClass.length);
                             ocIdx++) {
                            if (objectClass[ocIdx].equals(Command.class.getName())) {
                                print = false;
                            }
                        }

                        // Print header if we have not already done so.
                        if (!headerPrinted) {
                            headerPrinted = true;
                            String title = Util.getBundleName(bundle);
                            title = (inUse)
                                    ? title + " uses:"
                                    : title + " provides:";
                            io.out.println("");
                            io.out.println(title);
                            io.out.println(Util.getUnderlineString(title));
                        }

                        if (showAll || print) {
                            // Print service separator if necessary.
                            if (needSeparator) {
                                io.out.println("----");
                            }

                            // Print service properties.
                            String[] keys = refs[refIdx].getPropertyKeys();
                            for (int keyIdx = 0;
                                 (keys != null) && (keyIdx < keys.length);
                                 keyIdx++) {
                                Object v = refs[refIdx].getProperty(keys[keyIdx]);
                                io.out.println(
                                        keys[keyIdx] + " = " + Util.getValueString(v));
                            }

                            needSeparator = true;
                        }
                    }
                } else {
                    io.err.println("Bundle ID " + id + " is invalid.");
                }
            }
        }
        else
        {
            Bundle[] bundles = getBundleContext().getBundles();
            if (bundles != null)
            {
                // TODO: Sort list.
                for (int bundleIdx = 0; bundleIdx < bundles.length; bundleIdx++)
                {
                    boolean headerPrinted = false;
                    ServiceReference[] refs = null;

                    // Get registered or in-use services.
                    if (inUse)
                    {
                        refs = bundles[bundleIdx].getServicesInUse();
                    }
                    else
                    {
                        refs = bundles[bundleIdx].getRegisteredServices();
                    }

                    for (int refIdx = 0; (refs != null) && (refIdx < refs.length); refIdx++)
                    {
                        String[] objectClass = (String[])
                            refs[refIdx].getProperty("objectClass");

                        // Determine if we need to print the service, depending
                        // on whether it is a command service or not.
                        boolean print = true;
                        for (int ocIdx = 0;
                            !showAll && (ocIdx < objectClass.length);
                            ocIdx++)
                        {
                            if (objectClass[ocIdx].equals(Command.class.getName()))
                            {
                                print = false;
                            }
                        }

                        // Print the service if necessary.
                        if (showAll || print)
                        {
                            if (!headerPrinted)
                            {
                                headerPrinted = true;
                                String title = Util.getBundleName(bundles[bundleIdx]);
                                title = (inUse)
                                    ? title + " uses:"
                                    : title + " provides:";
                                io.out.println("\n" + title);
                                io.out.println(Util.getUnderlineString(title));
                            }
                            io.out.println(Util.getValueString(objectClass));
                        }
                    }
                }
            }
            else
            {
                io.out.println("There are no registered services.");
            }
        }
        return Result.SUCCESS;
    }