Search in sources :

Example 41 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project felix by apache.

the class WebConsolePrinter method dumpDuplicatesAsTxt.

@SuppressWarnings("deprecation")
private void dumpDuplicatesAsTxt(final PrintWriter pw, final Map<String, Set<ExportedPackage>> exports) {
    pw.println("Duplicate Exported Packages");
    pw.println("---------------------------");
    final List<String[]> lines = new ArrayList<String[]>();
    lines.add(new String[] { "Package", "Exports", "Imports" });
    for (Iterator<Entry<String, Set<ExportedPackage>>> entriesIter = exports.entrySet().iterator(); entriesIter.hasNext(); ) {
        Entry<String, Set<ExportedPackage>> exportEntry = entriesIter.next();
        final Set<ExportedPackage> exportSet = exportEntry.getValue();
        if (exportSet.size() > 1) {
            String firstCol = exportEntry.getKey();
            for (Iterator<ExportedPackage> packageIter = exportSet.iterator(); packageIter.hasNext(); ) {
                ExportedPackage exportedPackage = packageIter.next();
                final Bundle[] importers = exportedPackage.getImportingBundles();
                final String secondCol = "version=" + exportedPackage.getVersion() + ", Bundle " + exportedPackage.getExportingBundle();
                if (importers != null && importers.length > 0) {
                    boolean first = true;
                    for (int j = 0; j < importers.length; j++) {
                        final Bundle bundle = importers[j];
                        if (first) {
                            lines.add(new String[] { firstCol, secondCol, bundle.toString() });
                            first = false;
                        } else {
                            lines.add(new String[] { "", "", bundle.toString() });
                        }
                    }
                } else {
                    lines.add(new String[] { firstCol, secondCol, "" });
                }
                firstCol = "";
            }
        }
    }
    int maxFirst = 0, maxSecond = 0;
    for (int i = 0; i < lines.size(); i++) {
        final String[] entry = lines.get(i);
        if (entry[0].length() > maxFirst) {
            maxFirst = entry[0].length();
        }
        if (entry[1].length() > maxSecond) {
            maxSecond = entry[1].length();
        }
    }
    maxFirst += 2;
    maxSecond += 2;
    for (int i = 0; i < lines.size(); i++) {
        final String[] entry = lines.get(i);
        padText(pw, entry[0], maxFirst);
        padText(pw, entry[1], maxSecond);
        pw.println(entry[2]);
    }
}
Also used : Set(java.util.Set) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) Entry(java.util.Map.Entry) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage)

Example 42 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project felix by apache.

the class Inspect42 method printImportedPackages.

private static boolean printImportedPackages(BundleContext bc, Bundle b) {
    boolean matches = false;
    // Keep track of service references.
    List<ServiceReference<?>> refs = new ArrayList<ServiceReference<?>>();
    // Fragments cannot import packages.
    if (!isFragment(b)) {
        // Get package admin service.
        PackageAdmin pa = Util.getService(bc, PackageAdmin.class, refs);
        if (pa == null) {
            System.out.println("PackageAdmin service is unavailable.");
        } else {
            ExportedPackage[] exports = pa.getExportedPackages((Bundle) null);
            if (exports != null) {
                for (ExportedPackage ep : exports) {
                    Bundle[] importers = ep.getImportingBundles();
                    if (importers != null) {
                        for (Bundle importer : importers) {
                            if (importer == b) {
                                matches = true;
                                System.out.println(LEGACY_PACKAGE_NAMESPACE + "; " + ep.getName() + " resolved by:");
                                System.out.println("   " + ep.getName() + "; " + ep.getVersion().toString() + " from " + ep.getExportingBundle());
                            }
                        }
                    }
                }
            }
        }
    }
    Util.ungetServices(bc, refs);
    return matches;
}
Also used : PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) Bundle(org.osgi.framework.Bundle) RequiredBundle(org.osgi.service.packageadmin.RequiredBundle) ArrayList(java.util.ArrayList) ServiceReference(org.osgi.framework.ServiceReference)

Example 43 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project felix by apache.

the class Inspect42 method printExportedPackages.

public static boolean printExportedPackages(BundleContext bc, Bundle b) {
    boolean matches = false;
    // Keep track of service references.
    List<ServiceReference<?>> refs = new ArrayList<ServiceReference<?>>();
    // Fragments cannot export packages.
    if (!isFragment(b)) {
        // Get package admin service.
        PackageAdmin pa = Util.getService(bc, PackageAdmin.class, refs);
        if (pa == null) {
            System.out.println("PackageAdmin service is unavailable.");
        } else {
            try {
                ExportedPackage[] exports = pa.getExportedPackages(b);
                if (exports != null) {
                    for (ExportedPackage ep : exports) {
                        matches = true;
                        Bundle[] importers = ep.getImportingBundles();
                        if ((importers != null) && (importers.length > 0)) {
                            String msg = LEGACY_PACKAGE_NAMESPACE + "; " + ep.getName() + "; " + ep.getVersion().toString() + " required by:";
                            System.out.println(msg);
                            for (Bundle importer : importers) {
                                System.out.println("   " + importer);
                            }
                        } else {
                            System.out.println(LEGACY_PACKAGE_NAMESPACE + "; " + ep.getName() + "; " + ep.getVersion().toString() + " " + UNUSED_MESSAGE);
                        }
                    }
                }
            } catch (Exception ex) {
                System.err.println(ex.toString());
            }
        }
    }
    Util.ungetServices(bc, refs);
    return matches;
}
Also used : PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) Bundle(org.osgi.framework.Bundle) RequiredBundle(org.osgi.service.packageadmin.RequiredBundle) ArrayList(java.util.ArrayList) ServiceReference(org.osgi.framework.ServiceReference)

Example 44 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project felix by apache.

the class Permissions method createPermission.

private Permission createPermission(final PermissionInfo permissionInfo, final Class target) {
    return (Permission) AccessController.doPrivileged(new PrivilegedAction() {

        public Object run() {
            Permission cached = getFromCache(permissionInfo.getEncoded(), target);
            if (cached != null) {
                return cached;
            }
            try {
                if (m_classLoader.loadClass(target.getName()) == target) {
                    return addToCache(permissionInfo.getEncoded(), createPermission(permissionInfo.getName(), permissionInfo.getActions(), target));
                }
            } catch (ClassNotFoundException e1) {
            }
            ServiceReference[] refs = null;
            try {
                refs = m_context.getServiceReferences(PackageAdmin.class.getName(), null);
            } catch (InvalidSyntaxException e) {
            }
            if (refs != null) {
                for (int i = 0; i < refs.length; i++) {
                    PackageAdmin admin = (PackageAdmin) m_context.getService(refs[i]);
                    if (admin != null) {
                        Permission result = null;
                        Bundle bundle = admin.getBundle(target);
                        if (bundle != null) {
                            ExportedPackage[] exports = admin.getExportedPackages(bundle);
                            if (exports != null) {
                                String name = target.getName();
                                name = name.substring(0, name.lastIndexOf('.'));
                                for (int j = 0; j < exports.length; j++) {
                                    if (exports[j].getName().equals(name)) {
                                        result = createPermission(permissionInfo.getName(), permissionInfo.getActions(), target);
                                        break;
                                    }
                                }
                            }
                        }
                        m_context.ungetService(refs[i]);
                        return addToCache(permissionInfo.getEncoded(), result);
                    }
                }
            }
            return null;
        }
    });
}
Also used : PrivilegedAction(java.security.PrivilegedAction) PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) Bundle(org.osgi.framework.Bundle) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) AllPermission(java.security.AllPermission) FilePermission(java.io.FilePermission) AdminPermission(org.osgi.framework.AdminPermission) Permission(java.security.Permission) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Example 45 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project rt.equinox.framework by eclipse.

the class PackageAdminImpl method getExportedPackages.

public ExportedPackage[] getExportedPackages(Bundle bundle) {
    if (bundle == null) {
        return getExportedPackages((String) null);
    }
    Module module = StartLevelImpl.getModule(bundle);
    Collection<ModuleRevision> revisions = module == null ? Collections.<ModuleRevision>emptyList() : module.getRevisions().getModuleRevisions();
    Collection<ExportedPackage> allExports = new ArrayList<>();
    for (ModuleRevision revision : revisions) {
        ModuleWiring wiring = revision.getWiring();
        if (wiring != null) {
            List<ModuleCapability> providedPackages = wiring.getModuleCapabilities(PackageNamespace.PACKAGE_NAMESPACE);
            if (providedPackages != null) {
                for (ModuleCapability providedPackage : providedPackages) {
                    allExports.add(new ExportedPackageImpl(providedPackage, wiring));
                }
            }
        }
    }
    return allExports.isEmpty() ? null : allExports.toArray(new ExportedPackage[allExports.size()]);
}
Also used : ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) ArrayList(java.util.ArrayList) ModuleWiring(org.eclipse.osgi.container.ModuleWiring) ModuleCapability(org.eclipse.osgi.container.ModuleCapability) Module(org.eclipse.osgi.container.Module) ModuleRevision(org.eclipse.osgi.container.ModuleRevision)

Aggregations

ExportedPackage (org.osgi.service.packageadmin.ExportedPackage)54 Bundle (org.osgi.framework.Bundle)40 PackageAdmin (org.osgi.service.packageadmin.PackageAdmin)21 ArrayList (java.util.ArrayList)19 RequiredBundle (org.osgi.service.packageadmin.RequiredBundle)14 Test (org.junit.Test)9 HashMap (java.util.HashMap)7 List (java.util.List)7 Version (org.osgi.framework.Version)6 LinkedHashSet (java.util.LinkedHashSet)4 TreeMap (java.util.TreeMap)4 Expectations (org.jmock.Expectations)4 BundleContext (org.osgi.framework.BundleContext)4 ServiceReference (org.osgi.framework.ServiceReference)4 File (java.io.File)3 HashSet (java.util.HashSet)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 Set (java.util.Set)3 BundleCapability (org.osgi.framework.wiring.BundleCapability)3