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);
}
}
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;
}
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();
}
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;
}
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;
}
Aggregations