/*
* Copyright (c) 2003-2008, KNOPFLERFISH project All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* - Neither the name of the KNOPFLERFISH project nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.knopflerfish.bundle.desktop.swing;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import javax.swing.JComponent;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Version;
import org.osgi.service.packageadmin.ExportedPackage;
import org.osgi.service.packageadmin.PackageAdmin;
import org.osgi.service.packageadmin.RequiredBundle;
import org.osgi.util.tracker.ServiceTracker;
public class PackageHTMLDisplayer extends DefaultSwingBundleDisplayer {
ServiceTracker pkgTracker;
public PackageHTMLDisplayer(BundleContext bc) {
super(bc, "Packages", "Shows bundle packages", true);
pkgTracker = new ServiceTracker(bc, PackageAdmin.class.getName(), null);
pkgTracker.open();
}
public JComponent newJComponent() {
return new JHTML(this);
}
public void valueChanged(long bid) {
Bundle[] bl = Activator.desktop.getSelectedBundles();
for(Iterator it = components.iterator(); it.hasNext(); ) {
JHTML comp = (JHTML)it.next();
comp.valueChanged(bl);
}
}
class JHTML extends JHTMLBundle {
JHTML(DefaultSwingBundleDisplayer displayer) {
super(displayer);
}
public StringBuffer bundleInfo(Bundle b) {
StringBuffer sb = new StringBuffer();
startFont(sb);
PackageAdmin pkgAdmin = (PackageAdmin)pkgTracker.getService();
if(pkgAdmin == null) {
sb.append("No PackageAdmin service found");
} else {
ExportedPackage[] pkgs = pkgAdmin.getExportedPackages(b);
if(pkgs != null && pkgs.length > 0) {
sb.append("Exported packages");
List exportDescr = new ArrayList();
for (int i = 0; i < pkgs.length; i++) {
StringBuffer sb1 = new StringBuffer();
sb1.append("
");
sb1.append("" + pkgs[i].getName() + "");
Version version = pkgs[i].getVersion();
if (version != null) {
sb1.append(" " + version);
}
if (pkgs[i].isRemovalPending()) {
sb1.append(" ").append("pending removal on refresh");
}
Bundle[] bl = pkgs[i].getImportingBundles();
for(int j = 0; bl != null && j < bl.length; j++) {
sb1.append("
");
sb1.append(" ");
Util.bundleLink(sb1, bl[j]);
}
exportDescr.add(sb1.toString());
}
Collections.sort(exportDescr);
for (Iterator it = exportDescr.iterator(); it.hasNext(); ) {
sb.append(it.next());
}
} else {
sb.append("No exported packages");
}
// Array with all bundles that can be required.
RequiredBundle[] rbl = pkgAdmin.getRequiredBundles(null);
List importedPkgs = new ArrayList();
List requiredPkgs = new ArrayList();
ExportedPackage[] allEpkgs = pkgAdmin.getExportedPackages((Bundle)null);
for(int i = 0; allEpkgs != null && i < allEpkgs.length; i++) {
Bundle[] bl2 = allEpkgs[i].getImportingBundles();
for(int k = 0; bl2 != null && k < bl2.length; k++) {
if(bl2[k].getBundleId() == b.getBundleId()) {
Bundle exporter = allEpkgs[i].getExportingBundle();
if (isBundleRequiredBy(rbl, exporter, b)) {
requiredPkgs.add(allEpkgs[i]);
} else {
importedPkgs.add(allEpkgs[i]);
}
}
}
}
sb.append("
"); if(importedPkgs.size() > 0) { sb.append("Imported packages"); Collections.sort(importedPkgs, new ExportedPackageComparator()); for (Iterator it = importedPkgs.iterator(); it.hasNext(); ) { sb.append(formatPackage( (ExportedPackage) it.next(), false )); } } else { sb.append("No imported packages"); } sb.append("
"); if(requiredPkgs.size() > 0) { sb.append(""); sb.append("Packages available from required bundles"); Collections.sort(requiredPkgs, new ExportedPackageComparator()); for (Iterator it = requiredPkgs.iterator(); it.hasNext(); ) { ExportedPackage epkg = (ExportedPackage) it.next(); sb.append(formatPackage( epkg, isPkgInList(epkg, importedPkgs))); } sb.append("
"); } RequiredBundle rb = getRequiredBundle(rbl, b); Bundle[] requiringBundles = rb!=null ? rb.getRequiringBundles() : null; if (requiringBundles!=null && requiringBundles.length>0) { sb.append("");
sb.append("Required by");
if (rb.isRemovalPending()) {
sb.append(" (pending removal on refresh)");
}
for (int j=0; requiringBundles!=null && j