Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 package org.apache.felix.gogo.command;
 
 import java.util.List;
 
 
 public class Inspect
 {
     public static final String PACKAGE_TYPE = "package";
     public static final String BUNDLE_TYPE = "bundle";
     public static final String FRAGMENT_TYPE = "fragment";
     public static final String SERVICE_TYPE = "service";
 
     public static final String CAPABILITY = "capability";
     public static final String REQUIREMENT = "requirement";
 
     public static void inspect(
         BundleContext bcString typeString directionBundle[] bundles)
     {
         // Verify arguments.
         if (isValidType(type) && isValidDirection(direction))
         {
             // Now determine what needs to be printed.
             if (.startsWith(type))
             {
                 if (.startsWith(direction))
                 {
                     printExportedPackages(bcbundles);
                 }
                 else
                 {
                     printImportedPackages(bcbundles);
                 }
             }
             else if (.startsWith(type))
             {
                 if (.startsWith(direction))
                 {
                     printRequiringBundles(bcbundles);
                 }
                 else
                 {
                     printRequiredBundles(bcbundles);
                 }
             }
             else if (.startsWith(type))
             {
                 if (.startsWith(direction))
                 {
                     printFragmentHosts(bcbundles);
                 }
                 else
                 {
                     printHostedFragments(bcbundles);
                 }
             }
             else
             {
                 if (.startsWith(direction))
                 {
                     printExportedServices(bcbundles);
                 }
                 else
                 {
                     printImportedServices(bcbundles);
                 }
             }
         }
         else
         {
             if (!isValidType(type))
             {
                 ..println("Invalid argument: " + type);
             }
            if (!isValidDirection(direction))
            {
                ..println("Invalid argument: " + direction);
            }
        }
    }
    public static void printExportedPackages(BundleContext bcBundle[] bundles)
    {
        // Keep track of service references.
        List<ServiceReferencerefs = new ArrayList();
        // Get package admin service.
        PackageAdmin pa = Util.getService(bcPackageAdmin.classrefs);
        if (pa == null)
        {
            ..println("PackageAdmin service is unavailable.");
        }
        else
        {
            boolean separatorNeeded = false;
            if ((bundles == null) || (bundles.length == 0))
            {
                bundles = bc.getBundles();
            }
            for (int bundleIdx = 0; bundleIdx < bundles.lengthbundleIdx++)
            {
                try
                {
                    if (bundles[bundleIdx] != null)
                    {
                        ExportedPackage[] exports = pa.getExportedPackages(bundles[bundleIdx]);
                        if (separatorNeeded)
                        {
                            ..println("");
                        }
                        String title = bundles[bundleIdx] + " exports packages:";
                        ..println(title);
                        ..println(Util.getUnderlineString(title.length()));
                        if ((exports != null) && (exports.length > 0))
                        {
                            for (int expIdx = 0; expIdx < exports.lengthexpIdx++)
                            {
                                ..print(exports[expIdx]);
                                Bundle[] importers = exports[expIdx].getImportingBundles();
                                if ((importers == null) || (importers.length == 0))
                                {
                                    ..println(" UNUSED");
                                }
                                else
                                {
                                    ..println(" imported by:");
                                    for (int impIdx = 0; impIdx < importers.lengthimpIdx++)
                                    {
                                        ..println("   " + importers[impIdx]);
                                    }
                                }
                            }
                        }
                        else
                        {
                            ..println("Nothing");
                        }
                        separatorNeeded = true;
                    }
                }
                catch (Exception ex)
                {
                    ..println(ex.toString());
                }
            }
            Util.ungetServices(bcrefs);
        }
    }
    public static void printImportedPackages(BundleContext bcBundle[] bundles)
    {
        boolean separatorNeeded = false;
        if ((bundles == null) || (bundles.length == 0))
        {
            bundles = bc.getBundles();
        }
        for (int bundleIdx = 0; bundleIdx < bundles.lengthbundleIdx++)
        {
            try
            {
                if (bundles[bundleIdx] != null)
                {
                    if (separatorNeeded)
                    {
                        ..println("");
                    }
                    _printImportedPackages(bcbundles[bundleIdx]);
                    separatorNeeded = true;
                }
            }
            catch (Exception ex)
            {
                ..println(ex.toString());
            }
        }
    }
    private static void _printImportedPackages(BundleContext bcBundle bundle)
    {
        // Keep track of service references.
        List<ServiceReferencerefs = new ArrayList();
        // Get package admin service.
        PackageAdmin pa = Util.getService(bcPackageAdmin.classrefs);
        if (pa == null)
        {
            ..println("PackageAdmin service is unavailable.");
        }
        else
        {
            ExportedPackage[] exports = pa.getExportedPackages((Bundlenull);
            String title = bundle + " imports packages:";
            ..println(title);
            ..println(Util.getUnderlineString(title.length()));
            boolean found = false;
            for (int expIdx = 0; expIdx < exports.lengthexpIdx++)
            {
                Bundle[] importers = exports[expIdx].getImportingBundles();
                for (int impIdx = 0; (importers != null) && (impIdx < importers.length); impIdx++)
                {
                    if (importers[impIdx] == bundle)
                    {
                        ..println(exports[expIdx]
                            + " -> " + exports[expIdx].getExportingBundle());
                        found = true;
                    }
                }
            }
            if (!found)
            {
                ..println("Nothing");
            }
            Util.ungetServices(bcrefs);
        }
    }
    public static void printRequiringBundles(BundleContext bcBundle[] bundles)
    {
        // Keep track of service references.
        List<ServiceReferencerefs = new ArrayList();
        // Get package admin service.
        PackageAdmin pa = Util.getService(bcPackageAdmin.classrefs);
        if (pa == null)
        {
            ..println("PackageAdmin service is unavailable.");
        }
        else
        {
            boolean separatorNeeded = false;
            if ((bundles == null) || (bundles.length == 0))
            {
                bundles = bc.getBundles();
            }
            for (int bundleIdx = 0; bundleIdx < bundles.lengthbundleIdx++)
            {
                try
                {
                    if (bundles[bundleIdx] != null)
                    {
                        RequiredBundle[] rbs = pa.getRequiredBundles(
                            bundles[bundleIdx].getSymbolicName());
                        for (int rbIdx = 0; (rbs != null) && (rbIdx < rbs.length); rbIdx++)
                        {
                            if (rbs[rbIdx].getBundle() == bundles[bundleIdx])
                            {
                                if (separatorNeeded)
                                {
                                    ..println("");
                                }
                                String title = bundles[bundleIdx] + " is required by:";
                                ..println(title);
                                ..println(Util.getUnderlineString(title.length()));
                                if ((rbs[rbIdx].getRequiringBundles() != null)
                                    && (rbs[rbIdx].getRequiringBundles().length > 0))
                                {
                                    for (int reqIdx = 0;
                                        reqIdx < rbs[rbIdx].getRequiringBundles().length;
                                        reqIdx++)
                                    {
                                        ..println(rbs[rbIdx].getRequiringBundles()[reqIdx]);
                                    }
                                }
                                else
                                {
                                    ..println("Nothing");
                                }
                                separatorNeeded = true;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    ..println(ex.toString());
                }
            }
            Util.ungetServices(bcrefs);
        }
    }
    public static void printRequiredBundles(BundleContext bcBundle[] bundles)
    {
        boolean separatorNeeded = false;
        if ((bundles == null) || (bundles.length == 0))
        {
            bundles = bc.getBundles();
        }
        for (int bundleIdx = 0; bundleIdx < bundles.lengthbundleIdx++)
        {
            try
            {
                if (bundles[bundleIdx] != null)
                {
                    if (separatorNeeded)
                    {
                        ..println("");
                    }
                    _printRequiredBundles(bcbundles[bundleIdx]);
                    separatorNeeded = true;
                }
            }
            catch (Exception ex)
            {
                ..println(ex.toString());
            }
        }
    }
    private static void _printRequiredBundles(BundleContext bcBundle bundle)
    {
        // Keep track of service references.
        List<ServiceReferencerefs = new ArrayList();
        // Get package admin service.
        PackageAdmin pa = Util.getService(bcPackageAdmin.classrefs);
        if (pa == null)
        {
            ..println("PackageAdmin service is unavailable.");
        }
        else
        {
            RequiredBundle[] rbs = pa.getRequiredBundles(null);
            String title = bundle + " requires bundles:";
            ..println(title);
            ..println(Util.getUnderlineString(title.length()));
            boolean found = false;
            for (int rbIdx = 0; rbIdx < rbs.lengthrbIdx++)
            {
                Bundle[] requirers = rbs[rbIdx].getRequiringBundles();
                for (int reqIdx = 0; (requirers != null) && (reqIdx < requirers.length); reqIdx++)
                {
                    if (requirers[reqIdx] == bundle)
                    {
                        ..println(rbs[reqIdx]);
                        found = true;
                    }
                }
            }
            if (!found)
            {
                ..println("Nothing");
            }
            Util.ungetServices(bcrefs);
        }
    }
    public static void printFragmentHosts(BundleContext bcBundle[] bundles)
    {
        // Keep track of service references.
        List<ServiceReferencerefs = new ArrayList();
        // Get package admin service.
        PackageAdmin pa = Util.getService(bcPackageAdmin.classrefs);
        if (pa == null)
        {
            ..println("PackageAdmin service is unavailable.");
        }
        else
        {
            if ((bundles == null) || (bundles.length == 0))
            {
                bundles = bc.getBundles();
            }
            for (int bundleIdx = 0; bundleIdx < bundles.lengthbundleIdx++)
            {
                // Print a separator for some whitespace.
                if (bundleIdx > 0)
                {
                    ..println("");
                }
                try
                {
                    if ((bundles[bundleIdx] != null) && isFragment(bundles[bundleIdx]))
                    {
                        String title = bundles[bundleIdx] + " is attached to:";
                        ..println(title);
                        ..println(Util.getUnderlineString(title.length()));
                        Bundle[] hosts = pa.getHosts(bundles[bundleIdx]);
                        for (int hostIdx = 0;
                            (hosts != null) && (hostIdx < hosts.length);
                            hostIdx++)
                        {
                            ..println(hosts[hostIdx]);
                        }
                        if ((hosts == null) || (hosts.length == 0))
                        {
                            ..println("Nothing");
                        }
                    }
                    else if ((bundles[bundleIdx] != null) && !isFragment(bundles[bundleIdx]))
                    {
                        ..println("Bundle " + bundles[bundleIdx] + " is not a fragment.");
                    }
                }
                catch (Exception ex)
                {
                    ..println(ex.toString());
                }
            }
            Util.ungetServices(bcrefs);
        }
    }
    public static void printHostedFragments(BundleContext bcBundle[] bundles)
    {
        // Keep track of service references.
        List<ServiceReferencerefs = new ArrayList();
        // Get package admin service.
        PackageAdmin pa = Util.getService(bcPackageAdmin.classrefs);
        if (pa == null)
        {
            ..println("PackageAdmin service is unavailable.");
        }
        else
        {
            if ((bundles == null) || (bundles.length == 0))
            {
                bundles = bc.getBundles();
            }
            for (int bundleIdx = 0; bundleIdx < bundles.lengthbundleIdx++)
            {
                // Print a separator for some whitespace.
                if (bundleIdx > 0)
                {
                    ..println("");
                }
                try
                {
                    if ((bundles[bundleIdx] != null) && !isFragment(bundles[bundleIdx]))
                    {
                        String title = bundles[bundleIdx] + " hosts:";
                        ..println(title);
                        ..println(Util.getUnderlineString(title.length()));
                        Bundle[] fragments = pa.getFragments(bundles[bundleIdx]);
                        for (int fragIdx = 0;
                            (fragments != null) && (fragIdx < fragments.length);
                            fragIdx++)
                        {
                            ..println(fragments[fragIdx]);
                        }
                        if ((fragments == null) || (fragments.length == 0))
                        {
                            ..println("Nothing");
                        }
                    }
                    else if ((bundles[bundleIdx] != null) && isFragment(bundles[bundleIdx]))
                    {
                        ..println("Bundle " + bundles[bundleIdx] + " is a fragment.");
                    }
                }
                catch (Exception ex)
                {
                    ..println(ex.toString());
                }
            }
            Util.ungetServices(bcrefs);
        }
    }
    public static void printExportedServices(BundleContext bcBundle[] bundles)
    {
        if ((bundles == null) || (bundles.length == 0))
        {
            bundles = bc.getBundles();
        }
        for (int bundleIdx = 0; bundleIdx < bundles.lengthbundleIdx++)
        {
            // Print a separator for some whitespace.
            if (bundleIdx > 0)
            {
                ..println("");
            }
            try
            {
                if (bundles[bundleIdx] != null)
                {
                    ServiceReference[] refs = bundles[bundleIdx].getRegisteredServices();
                    // Print header if we have not already done so.
                    String title = Util.getBundleName(bundles[bundleIdx]) + " provides services:";
                    ..println(title);
                    ..println(Util.getUnderlineString(title.length()));
                    if ((refs == null) || (refs.length == 0))
                    {
                        ..println("Nothing");
                    }
                    // Print properties for each service.
                    for (int refIdx = 0;
                        (refs != null) && (refIdx < refs.length);
                        refIdx++)
                    {
                        // 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]);
                            ..println(
                                keys[keyIdx] + " = " + Util.getValueString(v));
                        }
                        // Print service separator if necessary.
                        if ((refIdx + 1) < refs.length)
                        {
                            ..println("----");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ..println(ex.toString());
            }
        }
    }
    public static void printImportedServices(BundleContext bcBundle[] bundles)
    {
        if ((bundles == null) || (bundles.length == 0))
        {
            bundles = bc.getBundles();
        }
        for (int bundleIdx = 0; bundleIdx < bundles.lengthbundleIdx++)
        {
            // Print a separator for some whitespace.
            if (bundleIdx > 0)
            {
                ..println("");
            }
            try
            {
                if (bundles[bundleIdx] != null)
                {
                    ServiceReference[] refs = bundles[bundleIdx].getServicesInUse();
                    // Print header if we have not already done so.
                    String title = Util.getBundleName(bundles[bundleIdx]) + " requires services:";
                    ..println(title);
                    ..println(Util.getUnderlineString(title.length()));
                    if ((refs == null) || (refs.length == 0))
                    {
                        ..println("Nothing");
                    }
                    // Print properties for each service.
                    for (int refIdx = 0;
                        (refs != null) && (refIdx < refs.length);
                        refIdx++)
                    {
                        // Print the registering bundle.
                        ..println("Registering bundle = " + refs[refIdx].getBundle());
                        // 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]);
                            ..println(
                                keys[keyIdx] + " = " + Util.getValueString(v));
                        }
                        // Print service separator if necessary.
                        if ((refIdx + 1) < refs.length)
                        {
                            ..println("----");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ..println(ex.toString());
            }
        }
    }
    private static boolean isValidType(String type)
    {
        return (.startsWith(type) || .startsWith(type)
            || .startsWith(type) || .startsWith(type));
    }
    private static boolean isValidDirection(String direction)
    {
        return (.startsWith(direction) || .startsWith(direction));
    }
    private static boolean isFragment(Bundle bundle)
    {
        return bundle.getHeaders().get(.) != null;
    }
New to GrepCode? Check out our FAQ X