Search in sources :

Example 11 with ExportedPackage

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

the class BundlesServlet method listImportExport.

private void listImportExport(List props, Bundle bundle, final String pluginRoot) {
    PackageAdmin packageAdmin = getPackageAdmin();
    if (packageAdmin == null) {
        return;
    }
    Map usingBundles = new TreeMap();
    ExportedPackage[] exports = packageAdmin.getExportedPackages(bundle);
    if (exports != null && exports.length > 0) {
        // do alphabetical sort
        Arrays.sort(exports, new Comparator() {

            public int compare(ExportedPackage p1, ExportedPackage p2) {
                return p1.getName().compareTo(p2.getName());
            }

            @Override
            public int compare(Object o1, Object o2) {
                return compare((ExportedPackage) o1, (ExportedPackage) o2);
            }
        });
        Object[] val = new Object[exports.length];
        for (int j = 0; j < exports.length; j++) {
            ExportedPackage export = exports[j];
            val[j] = collectExport(export.getName(), export.getVersion());
            Bundle[] ubList = export.getImportingBundles();
            if (ubList != null) {
                for (int i = 0; i < ubList.length; i++) {
                    Bundle ub = ubList[i];
                    String name = ub.getSymbolicName();
                    if (name == null)
                        name = ub.getLocation();
                    usingBundles.put(name, ub);
                }
            }
        }
        keyVal(props, "Exported Packages", val);
    } else {
        keyVal(props, "Exported Packages", "---");
    }
    exports = packageAdmin.getExportedPackages((Bundle) null);
    if (exports != null && exports.length > 0) {
        // collect import packages first
        final List imports = new ArrayList();
        for (int i = 0; i < exports.length; i++) {
            final ExportedPackage ep = exports[i];
            final Bundle[] importers = ep.getImportingBundles();
            for (int j = 0; importers != null && j < importers.length; j++) {
                if (importers[j].getBundleId() == bundle.getBundleId()) {
                    imports.add(ep);
                    break;
                }
            }
        }
        // now sort
        Object[] val;
        if (imports.size() > 0) {
            final ExportedPackage[] packages = (ExportedPackage[]) imports.toArray(new ExportedPackage[imports.size()]);
            Arrays.sort(packages, new Comparator() {

                public int compare(ExportedPackage p1, ExportedPackage p2) {
                    return p1.getName().compareTo(p2.getName());
                }

                @Override
                public int compare(Object o1, Object o2) {
                    return compare((ExportedPackage) o1, (ExportedPackage) o2);
                }
            });
            // and finally print out
            val = new Object[packages.length];
            for (int i = 0; i < packages.length; i++) {
                ExportedPackage ep = packages[i];
                val[i] = collectImport(ep.getName(), ep.getVersion(), false, ep, pluginRoot);
            }
        } else {
            // add description if there are no imports
            val = new Object[1];
            val[0] = "None";
        }
        keyVal(props, "Imported Packages", val);
    }
    if (!usingBundles.isEmpty()) {
        Object[] val = new Object[usingBundles.size()];
        int index = 0;
        for (Iterator ui = usingBundles.values().iterator(); ui.hasNext(); ) {
            Bundle usingBundle = (Bundle) ui.next();
            val[index] = getBundleDescriptor(usingBundle, pluginRoot);
            index++;
        }
        keyVal(props, "Importing Bundles", val);
    }
}
Also used : Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) Comparator(java.util.Comparator) PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Example 12 with ExportedPackage

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

the class WebConsolePlugin method collectExportedPackages.

@SuppressWarnings("deprecation")
static final Map<String, Set<ExportedPackage>> collectExportedPackages(final PackageAdmin pa, final BundleContext bundleContext) {
    Map<String, Set<ExportedPackage>> exports = new TreeMap<String, Set<ExportedPackage>>();
    Bundle[] bundles = bundleContext.getBundles();
    for (int i = 0; bundles != null && i < bundles.length; i++) {
        final Bundle bundle = bundles[i];
        final ExportedPackage[] bundleExports = pa.getExportedPackages(bundle);
        for (int j = 0; bundleExports != null && j < bundleExports.length; j++) {
            final ExportedPackage exportedPackage = bundleExports[j];
            Set<ExportedPackage> exportSet = exports.get(exportedPackage.getName());
            if (exportSet == null) {
                exportSet = new TreeSet<ExportedPackage>(EXPORT_PACKAGE_COMPARATOR);
                exports.put(exportedPackage.getName(), exportSet);
            }
            exportSet.add(exportedPackage);
        }
    }
    return exports;
}
Also used : SortedSet(java.util.SortedSet) TreeSet(java.util.TreeSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) Bundle(org.osgi.framework.Bundle) TreeMap(java.util.TreeMap)

Example 13 with ExportedPackage

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

the class WebConsolePlugin method doPost.

/**
 * @see HttpServlet#doPost(HttpServletRequest, HttpServletResponse)
 */
@Override
protected final void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    final Object json;
    // $NON-NLS-1$
    String action = req.getParameter("action");
    if ("deps".equals(action)) {
        // $NON-NLS-1$
        json = doFindDependencies(req, pa);
    } else if ("dups".equals(action)) {
        // $NON-NLS-1$
        @SuppressWarnings("deprecation") Map<String, Set<ExportedPackage>> packages = collectExportedPackages(pa, bc);
        json = doFindDuplicates(packages);
    } else {
        throw new ServletException("Invalid action: " + action);
    }
    WebConsoleUtil.setNoCache(resp);
    // $NON-NLS-1$
    resp.setContentType("application/json; utf-8");
    JSONWriter writer = new JSONWriter(resp.getWriter());
    writer.value(json);
    writer.flush();
}
Also used : ServletException(javax.servlet.ServletException) JSONWriter(org.apache.felix.utils.json.JSONWriter) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 14 with ExportedPackage

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

the class RefreshDependenciesUtil method dependsOn.

/** True if target depends on source via package imports */
private boolean dependsOn(List<Long> idChecked, Bundle target, Bundle source) {
    if (idChecked.contains(source.getBundleId())) {
        return false;
    }
    idChecked.add(source.getBundleId());
    final ExportedPackage[] eps = pckAdmin.getExportedPackages(source);
    if (eps == null) {
        return false;
    }
    for (ExportedPackage ep : eps) {
        final Bundle[] importers = ep.getImportingBundles();
        if (importers == null) {
            continue;
        }
        for (Bundle b : importers) {
            if (b.getBundleId() == target.getBundleId()) {
                log.debug("{} depends on {} via package {}", new Object[] { target, source, ep.getName() });
                return true;
            }
            if (dependsOn(idChecked, target, b)) {
                log.debug("{} depends on {} which depends on {}, returning true", new Object[] { target, b, source });
                return true;
            }
        }
    }
    return false;
}
Also used : ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) Bundle(org.osgi.framework.Bundle)

Example 15 with ExportedPackage

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

the class RefreshDependenciesUtilTest method setupBundle.

private Bundle setupBundle(String mockName, String importPackages, final String exportsPackages) {
    final Bundle result = jmock.mock(Bundle.class, mockName);
    jmock.checking(new Expectations() {

        {
            allowing(result).getBundleId();
            will(returnValue(counter++));
        }
    });
    if (importPackages != null) {
        for (String pack : importPackages.split(";")) {
            List<Bundle> list = importingBundles.get(pack);
            if (list == null) {
                list = new ArrayList<Bundle>();
                importingBundles.put(pack, list);
            }
            list.add(result);
        }
    }
    final List<ExportedPackage> eps = new ArrayList<ExportedPackage>();
    if (exportsPackages != null) {
        for (final String pack : exportsPackages.split(";")) {
            final ExportedPackage ep = jmock.mock(ExportedPackage.class, "ExportedPackage." + pack + "." + mockName);
            eps.add(ep);
            jmock.checking(new Expectations() {

                {
                    allowing(ep).getImportingBundles();
                    will(returnValue(getImportingBundles(pack)));
                    allowing(ep).getName();
                    will(returnValue(pack));
                }
            });
        }
    }
    jmock.checking(new Expectations() {

        {
            allowing(pa).getExportedPackages(result);
            will(returnValue(eps.toArray(new ExportedPackage[] {})));
        }
    });
    return result;
}
Also used : Expectations(org.jmock.Expectations) Bundle(org.osgi.framework.Bundle) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) ArrayList(java.util.ArrayList)

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