return bundles;
}
// Second pass: for each bundle, check if there is any unresolved optional package that could be resolved
Map<Bundle, List<Clause>> imports = new HashMap<Bundle, List<Clause>>();
for (Iterator<Bundle> it = bundles.iterator(); it.hasNext();) {
Bundle b = it.next();
String importsStr = (String) b.getHeaders().get(Constants.IMPORT_PACKAGE);
List<Clause> importsList = getOptionalImports(importsStr);
if (importsList.isEmpty()) {
it.remove();
} else {
imports.put(b, importsList);
}
}
if (bundles.isEmpty()) {
return bundles;
}
// Third pass: compute a list of packages that are exported by our bundles and see if
// some exported packages can be wired to the optional imports
List<Clause> exports = new ArrayList<Clause>();
for (Bundle b : state.installed) {
String exportsStr = (String) b.getHeaders().get(Constants.EXPORT_PACKAGE);
if (exportsStr != null) {
Clause[] exportsList = Parser.parseHeader(exportsStr);
exports.addAll(Arrays.asList(exportsList));
}
}
for (Iterator<Bundle> it = bundles.iterator(); it.hasNext();) {
Bundle b = it.next();
List<Clause> importsList = imports.get(b);
for (Iterator<Clause> itpi = importsList.iterator(); itpi.hasNext();) {
Clause pi = itpi.next();
boolean matching = false;
for (Clause pe : exports) {
if (pi.getName().equals(pe.getName())) {
String evStr = pe.getAttribute(Constants.VERSION_ATTRIBUTE);
String ivStr = pi.getAttribute(Constants.VERSION_ATTRIBUTE);
Version exported = evStr != null ? Version.parseVersion(evStr) : Version.emptyVersion;
VersionRange imported = ivStr != null ? VersionRange.parseVersionRange(ivStr) : VersionRange.ANY_VERSION;
if (imported.contains(exported)) {
matching = true;
break;
}
}
}
if (!matching) {
itpi.remove();
}
}
if (importsList.isEmpty()) {
it.remove();
} else {
LOGGER.debug("Refeshing bundle {} ({}) to solve the following optional imports", b.getSymbolicName(), b.getBundleId());
for (Clause p : importsList) {
LOGGER.debug(" {}", p);
}
}