protected Object doExecute()

in gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/ListBundles.java [48:171]


    protected Object doExecute() throws Exception {
        ServiceReference ref = getBundleContext().getServiceReference(StartLevel.class.getName());
        StartLevel sl = null;
        if (ref != null) {
            sl = (StartLevel) getBundleContext().getService(ref);
        }
        if (sl == null) {
            io.out.println("StartLevel service is unavailable.");
        }

        ServiceReference pkgref = getBundleContext().getServiceReference(PackageAdmin.class.getName());
        PackageAdmin admin = null;
        if (pkgref != null) {
            admin = (PackageAdmin) getBundleContext().getService(pkgref);
            if (admin == null) {
                io.out.println("PackageAdmin service is unavailable.");
            }
        }

        Bundle[] bundles = getBundleContext().getBundles();
        if (bundles != null) {
            // Display active start level.
            if (sl != null) {
                io.out.println("START LEVEL " + sl.getStartLevel());
            }

            // Print column headers.
            String msg = " Name";
            if (showLoc) {
               msg = " Location";
            }
            else if (showSymbolic) {
               msg = " Symbolic name";
            }
            else if (showUpdate) {
               msg = " Update location";
            }
            String level = (sl == null) ? "" : "  Level ";
            io.out.println("   ID   State         Spring   " + level + msg);
            for (int i = 0; i < bundles.length; i++) {
                // Get the bundle name or location.
                String name = (String) bundles[i].getHeaders().get(Constants.BUNDLE_NAME);
                // If there is no name, then default to symbolic name.
                name = (name == null) ? bundles[i].getSymbolicName() : name;
                // If there is no symbolic name, resort to location.
                name = (name == null) ? bundles[i].getLocation() : name;

                // Overwrite the default value is the user specifically
                // requested to display one or the other.
                if (showLoc) {
                    name = bundles[i].getLocation();
                }
                else if (showSymbolic) {
                    name = bundles[i].getSymbolicName();
                    name = (name == null) ? "<no symbolic name>" : name;
                }
                else if (showUpdate) {
                    name = (String) bundles[i].getHeaders().get(Constants.BUNDLE_UPDATELOCATION);
                    name = (name == null) ? bundles[i].getLocation() : name;
                }
                // Show bundle version if not showing location.
                String version = (String) bundles[i].getHeaders().get(Constants.BUNDLE_VERSION);
                name = (!showLoc && !showUpdate && (version != null)) ? name + " (" + version + ")" : name;
                long l = bundles[i].getBundleId();
                String id = String.valueOf(l);
                if (sl == null) {
                    level = "1";
                }
                else {
                    level = String.valueOf(sl.getBundleStartLevel(bundles[i]));
                }
                while (level.length() < 5) {
                    level = " " + level;
                }
                while (id.length() < 4) {
                    id = " " + id;
                }
                io.out.println("[" + id + "] ["
                    + getStateString(bundles[i])
                    + "] [" + getSpringStateString(bundles[i])
                    + "] [" + level + "] " + name);

                if (admin != null) {
                    Bundle[] fragments = admin.getFragments(bundles[i]);
                    Bundle[] hosts = admin.getHosts(bundles[i]);

                    if (fragments != null) {
                        io.out.print("                                       Fragments: ");
                        int ii = 0;
                        for (Bundle fragment : fragments) {
                            ii++;
                            io.out.print(fragment.getBundleId());
                            if ((fragments.length > 1) && ii < (fragments.length)) {
                                io.out.print(",");
                            }
                        }
                        io.out.println();
                    }

                    if (hosts != null) {
                        io.out.print("                                       Hosts: ");
                        int ii = 0;
                        for (Bundle host : hosts) {
                            ii++;
                            io.out.print(host.getBundleId());
                            if ((hosts.length > 1) && ii < (hosts.length)) {
                                io.out.print(",");
                            }
                        }
                        io.out.println();
                    }

                }
            }
        }
        else {
            io.out.println("There are no installed bundles.");
        }

        getBundleContext().ungetService(ref);
        getBundleContext().ungetService(pkgref);

        return Result.SUCCESS;
    }