// destroy bundles
Bundle[] bundles = new Bundle[managedContexts.size()];
int i = 0;
for (Iterator it = managedContexts.values().iterator(); it.hasNext();) {
ConfigurableOsgiBundleApplicationContext context = (ConfigurableOsgiBundleApplicationContext) it.next();
bundles[i++] = context.getBundle();
}
bundles = shutdownDependencySorter.computeServiceDependencyGraph(bundles);
boolean debug = log.isDebugEnabled();
StringBuffer buffer = new StringBuffer();
if (debug) {
buffer.append("Shutdown order is: {");
for (i = 0; i < bundles.length; i++) {
buffer.append("\nBundle [" + bundles[i].getSymbolicName() + "]");
ServiceReference[] services = bundles[i].getServicesInUse();
HashSet usedBundles = new HashSet();
if (services != null) {
for (int j = 0; j < services.length; j++) {
if (BundleDependencyComparator.isSpringManagedService(services[j])) {
Bundle used = services[j].getBundle();
if (!used.equals(bundleContext.getBundle()) && !usedBundles.contains(used)) {
usedBundles.add(used);
buffer.append("\n Using [" + used.getSymbolicName() + "]");
}
}
}
}
}
buffer.append("\n}");
log.debug(buffer);
}
final List taskList = new ArrayList(managedContexts.size());
final List closedContexts = Collections.synchronizedList(new ArrayList());
final Object[] contextClosingDown = new Object[1];
for (i = 0; i < bundles.length; i++) {
Long id = new Long(bundles[i].getBundleId());
final ConfigurableOsgiBundleApplicationContext context = (ConfigurableOsgiBundleApplicationContext) managedContexts.get(id);
if (context != null) {
closedContexts.add(context);
// add a new runnable
taskList.add(new Runnable() {
private final String toString = "Closing runnable for context " + context.getDisplayName();
public void run() {
contextClosingDown[0] = context;
// eliminate context
closedContexts.remove(context);
if (log.isDebugEnabled())
log.debug("Closing appCtx " + context.getDisplayName());
context.close();
}
public String toString() {
return toString;
}