properties.putAll(instructions);
properties.put("Generated-By-FAB-From", jarInfo);
properties.put(Analyzer.PLUGIN, ANALYZER_PLUGIN_NAMES);
final Analyzer analyzer = new Analyzer();
analyzer.setJar(jar);
analyzer.setProperties(properties);
// now lets add all the new embedded jars
for (Map.Entry<String, Object> entry : embeddedResources.entrySet()) {
String path = entry.getKey();
Object value = entry.getValue();
Resource resource = toResource(value);
if (resource != null) {
jar.putResource(path, resource);
try {
File file = toFile(value);
analyzer.addClasspath(file);
} catch (IOException e) {
LOG.warn("Failed to get File for " + value + ". " + e, e);
}
}
}
if (manifest != null && OverwriteMode.MERGE == overwriteMode) {
analyzer.mergeManifest(manifest);
}
checkMandatoryProperties(analyzer, jar, jarInfo);
Manifest newManifest = analyzer.calcManifest();
jar.setManifest( newManifest );
Attributes main = jar.getManifest().getMainAttributes();
// Hack to add back META-INF imports
String importPackage = main.getValue(Analyzer.IMPORT_PACKAGE);
for (Descriptors.PackageRef key : analyzer.getReferred().keySet()) {
if (key.getFQN().startsWith("META-INF.")) {
if (importPackage == null) {
importPackage = key.getFQN();
} else {
importPackage += "," + key.getFQN();
}
}
}
if (importPackage != null) {
main.putValue(Analyzer.IMPORT_PACKAGE, importPackage);
}
String importPackages = emptyIfNull(main.getValue(Analyzer.IMPORT_PACKAGE));
Parameters values = new Analyzer().parseHeader(importPackages);
// add any missing version clauses
if (versionResolver != null) {
for (Map.Entry<String, Attrs> entry : values.entrySet()) {
String packageName = entry.getKey();
Map<String, String> packageValues = entry.getValue();
if (!packageValues.containsKey("version")) {
String version = versionResolver.resolvePackageVersion(packageName);
if (version != null) {
packageValues.put("version", version);
}
}
}
}
// Merge in the extra imports - lets not add versions to extra imports as they come from exports which might not have versions.
for (Map.Entry<String, Map<String, String>> entry : extraImportPackages.entrySet()) {
Map<String, String> original = values.get(entry.getKey());
if (original == null) {
original = entry.getValue();
} else {
original.putAll(entry.getValue());
}
Attrs newAttrs = new Attrs();
newAttrs.putAll(original);
values.put(entry.getKey(), newAttrs);
}
// lets remove any excluded import packages
String excludedPackagesText = main.getValue(ServiceConstants.INSTR_FAB_EXCLUDE_IMPORTS_PACKAGE);
if (notEmpty(excludedPackagesText)) {
StringTokenizer e = new StringTokenizer(excludedPackagesText);
while (e.hasMoreTokens()) {
String expression = e.nextToken();
String ignore = expression;
if (ignore.endsWith("*")) {
do {
ignore = ignore.substring(0, ignore.length() - 1);
} while (ignore.endsWith("*"));
if (ignore.length() == 0) {
LOG.debug("Ignoring all imports due to %s value of %s", ServiceConstants.INSTR_FAB_EXCLUDE_IMPORTS_PACKAGE, expression);
values.clear();
} else {
List<String> packageNames = new ArrayList<String>(values.keySet());
for (String packageName : packageNames) {
if (packageName.equals(ignore) || packageName.startsWith(ignore)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Ignoring package " + packageName + " due to " + ServiceConstants.INSTR_FAB_EXCLUDE_IMPORTS_PACKAGE + " value of " + expression);
}
values.remove(packageName);
}
}
}
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Ignoring package " + ignore + " due to " + ServiceConstants.INSTR_FAB_EXCLUDE_IMPORTS_PACKAGE + " header");
}
values.remove(ignore);
}
}
}
// lets remove optional dependency if they are exported from a non-optional dependency...
for (Map.Entry<String, Attrs> entry : values.entrySet()) {
String packageName = entry.getKey();
Map<String, String> map = entry.getValue();
String res = map.get("resolution:");
if ("optional".equals(res)) {
if (!versionResolver.isPackageOptional(packageName)) {
map.remove("resolution:");
res = null;
}
}
if (!"optional".equals(res)) {
// add all the non-optional deps..
actualImports.add(packageName);
}
}
// TODO do we really need to filter out any of the attribute values?
// we were filtering out everything bar resolution:
//importPackages = Processor.printClauses(values, "resolution:");
importPackages = Processor.printClauses(values /*, ALLOWED_PACKAGE_CLAUSES */);
if (notEmpty(importPackages)) {
main.putValue(Analyzer.IMPORT_PACKAGE, importPackages);
}
String exportPackages = emptyIfNull(main.getValue(Analyzer.EXPORT_PACKAGE));
Parameters exports = new Analyzer().parseHeader(exportPackages);
for (Map.Entry<String, Attrs> entry : exports.entrySet()) {
String packageName = entry.getKey();
Map<String, String> map = entry.getValue();
String version = map.get("version");
if (version == null) {