Search in sources :

Example 1 with PackageVersion

use of org.apache.karaf.packages.core.PackageVersion in project karaf by apache.

the class Exports method checkDuplicateExports.

private void checkDuplicateExports() {
    Bundle[] bundles = bundleContext.getBundles();
    SortedMap<String, PackageVersion> packageVersionMap = getDuplicatePackages(bundles);
    ShellTable table = new ShellTable();
    table.column(new Col("Package Name"));
    table.column(new Col("Version"));
    table.column(new Col("Exporting bundles (ID)"));
    for (String key : packageVersionMap.keySet()) {
        PackageVersion pVer = packageVersionMap.get(key);
        if (pVer.getBundles().size() > 1) {
            String pBundles = getBundlesSt(pVer.getBundles());
            table.addRow().addContent(pVer.getPackageName(), pVer.getVersion().toString(), pBundles);
        }
    }
    table.print(System.out, !noFormat);
}
Also used : Col(org.apache.karaf.shell.support.table.Col) ShellTable(org.apache.karaf.shell.support.table.ShellTable) Bundle(org.osgi.framework.Bundle) PackageVersion(org.apache.karaf.packages.core.PackageVersion)

Example 2 with PackageVersion

use of org.apache.karaf.packages.core.PackageVersion in project karaf by apache.

the class PackageTest method duplicatePackageTest.

@Test
@Ignore
public void duplicatePackageTest() throws Exception {
    // Leaving out version to make test easier to manage
    // We currently expect no duplicate package exports
    Map<String, Integer> expectedDups = new HashMap<>();
    List<PackageVersion> packageVersionMap = packageService.getExports();
    for (PackageVersion pVer : packageVersionMap) {
        if (pVer.getBundles().size() > 1) {
            String packageName = pVer.getPackageName();
            int expectedNum = expectedDups.getOrDefault(packageName, 0);
            Assert.assertEquals("Expecting number of duplicates for package " + packageName, expectedNum, pVer.getBundles().size());
        }
    }
}
Also used : HashMap(java.util.HashMap) PackageVersion(org.apache.karaf.packages.core.PackageVersion) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with PackageVersion

use of org.apache.karaf.packages.core.PackageVersion in project karaf by apache.

the class PackageServiceImpl method getExports.

public List<PackageVersion> getExports() {
    Bundle[] bundles = bundleContext.getBundles();
    SortedMap<String, PackageVersion> packageVersionMap = new TreeMap<>();
    for (Bundle bundle : bundles) {
        BundleRevision rev = bundle.adapt(BundleRevision.class);
        if (rev != null) {
            List<BundleCapability> caps = rev.getDeclaredCapabilities(BundleRevision.PACKAGE_NAMESPACE);
            for (BundleCapability cap : caps) {
                Map<String, Object> attr = cap.getAttributes();
                String packageName = (String) attr.get(BundleRevision.PACKAGE_NAMESPACE);
                Version version = (Version) attr.get("version");
                String key = packageName + ":" + version.toString();
                PackageVersion pVer = packageVersionMap.computeIfAbsent(key, k -> new PackageVersion(packageName, version));
                pVer.addBundle(bundle);
            }
        }
    }
    return new ArrayList<>(packageVersionMap.values());
}
Also used : Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) PackageVersion(org.apache.karaf.packages.core.PackageVersion) PackageVersion(org.apache.karaf.packages.core.PackageVersion) Version(org.osgi.framework.Version) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Example 4 with PackageVersion

use of org.apache.karaf.packages.core.PackageVersion in project karaf by apache.

the class PackagesMBeanImpl method getExports.

public TabularData getExports() {
    try {
        String[] names = new String[] { "Name", "Version", "ID", "Bundle Name" };
        CompositeType bundleType = new CompositeType("PackageExport", "Exported packages", names, new String[] { "Package name", "Version of the Package", "ID of the Bundle", "Bundle symbolic name" }, new OpenType[] { SimpleType.STRING, SimpleType.STRING, SimpleType.LONG, SimpleType.STRING });
        TabularType tableType = new TabularType("PackageExports", "Exported packages", bundleType, new String[] { "Name", "Version", "ID" });
        TabularData table = new TabularDataSupport(tableType);
        List<PackageVersion> exports = packageService.getExports();
        for (PackageVersion export : exports) {
            for (Bundle bundle : export.getBundles()) {
                Object[] data = new Object[] { export.getPackageName(), export.getVersion().toString(), bundle.getBundleId(), bundle.getSymbolicName() };
                CompositeData comp = new CompositeDataSupport(bundleType, names, data);
                LOGGER.debug("Adding CompositeDataSupport {}", comp);
                table.put(comp);
            }
        }
        return table;
    } catch (RuntimeException e) {
        // To avoid the exception gets swallowed by jmx
        LOGGER.error(e.getMessage(), e);
        throw e;
    } catch (OpenDataException e) {
        LOGGER.error(e.getMessage(), e);
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : Bundle(org.osgi.framework.Bundle) TabularType(javax.management.openmbean.TabularType) CompositeData(javax.management.openmbean.CompositeData) CompositeDataSupport(javax.management.openmbean.CompositeDataSupport) PackageVersion(org.apache.karaf.packages.core.PackageVersion) TabularData(javax.management.openmbean.TabularData) OpenDataException(javax.management.openmbean.OpenDataException) TabularDataSupport(javax.management.openmbean.TabularDataSupport) CompositeType(javax.management.openmbean.CompositeType)

Example 5 with PackageVersion

use of org.apache.karaf.packages.core.PackageVersion in project karaf by apache.

the class Exports method showExports.

private void showExports() {
    List<PackageVersion> exports = packageService.getExports();
    ShellTable table = new ShellTable();
    table.column("Package Name");
    if (!showOnlyName) {
        table.column("Version");
        table.column("ID");
        table.column("Bundle Name");
    }
    for (PackageVersion pVer : exports) {
        for (Bundle bundle : pVer.getBundles()) {
            if (matchesFilter(pVer, bundle)) {
                if (!showOnlyName) {
                    table.addRow().addContent(pVer.getPackageName(), pVer.getVersion().toString(), bundle.getBundleId(), bundle.getSymbolicName());
                } else {
                    table.addRow().addContent(pVer.getPackageName());
                }
            }
        }
    }
    table.print(System.out, !noFormat);
}
Also used : ShellTable(org.apache.karaf.shell.support.table.ShellTable) Bundle(org.osgi.framework.Bundle) PackageVersion(org.apache.karaf.packages.core.PackageVersion)

Aggregations

PackageVersion (org.apache.karaf.packages.core.PackageVersion)6 Bundle (org.osgi.framework.Bundle)5 TreeMap (java.util.TreeMap)2 ShellTable (org.apache.karaf.shell.support.table.ShellTable)2 Version (org.osgi.framework.Version)2 BundleCapability (org.osgi.framework.wiring.BundleCapability)2 BundleRevision (org.osgi.framework.wiring.BundleRevision)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 CompositeData (javax.management.openmbean.CompositeData)1 CompositeDataSupport (javax.management.openmbean.CompositeDataSupport)1 CompositeType (javax.management.openmbean.CompositeType)1 OpenDataException (javax.management.openmbean.OpenDataException)1 TabularData (javax.management.openmbean.TabularData)1 TabularDataSupport (javax.management.openmbean.TabularDataSupport)1 TabularType (javax.management.openmbean.TabularType)1 Col (org.apache.karaf.shell.support.table.Col)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1