use of aQute.bnd.osgi.Descriptors.PackageRef in project bnd by bndtools.
the class JavaElement method getLocalAPI.
private Element getLocalAPI() throws Exception {
Set<Element> result = new HashSet<Element>();
for (Map.Entry<PackageRef, List<Element>> entry : packages.entrySet()) {
List<Element> set = entry.getValue();
for (Iterator<Element> i = set.iterator(); i.hasNext(); ) {
if (notAccessible.contains(analyzer.getTypeRefFromFQN(i.next().getName())))
i.remove();
}
String version = exports.get(entry.getKey()).get(Constants.VERSION_ATTRIBUTE);
if (version != null) {
Version v = new Version(version);
set.add(new Element(VERSION, v.getWithoutQualifier().toString(), null, IGNORED, IGNORED, null));
}
Element pd = new Element(PACKAGE, entry.getKey().getFQN(), set, MINOR, MAJOR, null);
result.add(pd);
}
for (JAVA java : javas) {
result.add(new Element(CLASS_VERSION, java.toString(), null, CHANGED, CHANGED, null));
}
return new Element(API, "<api>", result, CHANGED, CHANGED, null);
}
use of aQute.bnd.osgi.Descriptors.PackageRef in project bnd by bndtools.
the class MavenCommand method view.
void view(String[] args, int i) throws Exception {
Maven maven = new Maven(executor);
OutputStream out = System.err;
List<URI> urls = new ArrayList<URI>();
while (i < args.length && args[i].startsWith("-")) {
if ("-r".equals(args[i])) {
URI uri = new URI(args[++i]);
urls.add(uri);
System.err.println("URI for repo " + uri);
} else if ("-o".equals(args[i])) {
out = IO.outputStream(Paths.get(args[++i]));
} else
throw new IllegalArgumentException("Unknown option: " + args[i]);
i++;
}
URI[] urls2 = urls.toArray(new URI[0]);
PrintWriter pw = IO.writer(out);
while (i < args.length) {
String ref = args[i++];
pw.println("Ref " + ref);
Matcher matcher = GROUP_ARTIFACT_VERSION.matcher(ref);
if (matcher.matches()) {
String group = matcher.group(1);
String artifact = matcher.group(2);
String version = matcher.group(3);
CachedPom pom = maven.getPom(group, artifact, version, urls2);
Builder a = new Builder();
a.setProperty(Constants.PRIVATEPACKAGE, "*");
Set<Pom> dependencies = pom.getDependencies(Scope.compile, urls2);
for (Pom dep : dependencies) {
System.err.printf("%20s %-20s %10s%n", dep.getGroupId(), dep.getArtifactId(), dep.getVersion());
a.addClasspath(dep.getArtifact());
}
pw.println(a.getClasspath());
a.build();
TreeSet<PackageRef> sorted = new TreeSet<PackageRef>(a.getImports().keySet());
for (PackageRef p : sorted) {
pw.printf("%-40s\n", p);
}
// for ( Map.Entry<String, Set<String>> entry :
// a.getUses().entrySet()) {
// String from = entry.getKey();
// for ( String uses : entry.getValue()) {
// System.err.printf("%40s %s\n", from, uses);
// from = "";
// }
// }
a.close();
} else
System.err.println("Wrong, must look like group+artifact+version, is " + ref);
}
}
use of aQute.bnd.osgi.Descriptors.PackageRef in project bnd by bndtools.
the class Analyzer method doExportsToImports.
/**
* We will add all exports to the imports unless there is a -noimport
* directive specified on an export. This directive is skipped for the
* manifest. We also remove any version parameter so that augmentImports can
* do the version policy. The following method is really tricky and evolved
* over time. Coming from the original background of OSGi, it was a weird
* idea for me to have a public package that should not be substitutable. I
* was so much convinced that this was the right rule that I rücksichtlos
* imported them all. Alas, the real world was more subtle than that. It
* turns out that it is not a good idea to always import. First, there must
* be a need to import, i.e. there must be a contained package that refers
* to the exported package for it to make use importing that package.
* Second, if an exported package refers to an internal package than it
* should not be imported. Additionally, it is necessary to treat the
* exports in groups. If an exported package refers to another exported
* packages than it must be in the same group. A framework can only
* substitute exports for imports for the whole of such a group. WHY?????
* Not clear anymore ...
*/
Packages doExportsToImports(Packages exports) {
// private packages = contained - exported.
Set<PackageRef> privatePackages = new HashSet<PackageRef>(contained.keySet());
privatePackages.removeAll(exports.keySet());
// private references = ∀ p : contained packages | uses(p)
Set<PackageRef> containedReferences = newSet();
for (PackageRef p : contained.keySet()) {
Collection<PackageRef> uses = this.uses.get(p);
if (uses != null)
containedReferences.addAll(uses);
}
// Assume we are going to import all exported packages
Set<PackageRef> toBeImported = new HashSet<PackageRef>(exports.keySet());
// Remove packages that are not referenced locally
toBeImported.retainAll(containedReferences);
for (Iterator<PackageRef> i = toBeImported.iterator(); i.hasNext(); ) {
PackageRef next = i.next();
Collection<PackageRef> usedByExportedPackage = this.uses.get(next);
// imports that do not match reality ...
if (usedByExportedPackage == null || usedByExportedPackage.isEmpty()) {
continue;
}
for (PackageRef privatePackage : privatePackages) {
if (usedByExportedPackage.contains(privatePackage)) {
i.remove();
break;
}
}
}
// Clean up attributes and generate result map
Packages result = new Packages();
for (Iterator<PackageRef> i = toBeImported.iterator(); i.hasNext(); ) {
PackageRef ep = i.next();
Attrs parameters = exports.get(ep);
String noimport = parameters == null ? null : parameters.get(NO_IMPORT_DIRECTIVE);
if (noimport != null && noimport.equalsIgnoreCase("true"))
continue;
// // we can't substitute when there is no version
// String version = parameters.get(VERSION_ATTRIBUTE);
// if (version == null) {
// if (isPedantic())
// warning(
// "Cannot automatically import exported package %s because it has
// no version defined",
// ep);
// continue;
// }
parameters = new Attrs();
parameters.remove(VERSION_ATTRIBUTE);
result.put(ep, parameters);
}
return result;
}
use of aQute.bnd.osgi.Descriptors.PackageRef in project bnd by bndtools.
the class Analyzer method doUses.
/**
* @param packageRef
* @param exports
* @param uses
* @param imports
*/
protected void doUses(PackageRef packageRef, Packages exports, Map<PackageRef, List<PackageRef>> uses, Packages imports) {
Attrs clause = exports.get(packageRef);
// Check if someone already set the uses: directive
String override = clause.get(USES_DIRECTIVE);
if (override == null)
override = USES_USES;
// Get the used packages
Collection<PackageRef> usedPackages = uses.get(packageRef);
if (usedPackages != null) {
// Only do a uses on exported or imported packages
// and uses should also not contain our own package
// name
Set<PackageRef> sharedPackages = new TreeSet<PackageRef>();
sharedPackages.addAll(imports.keySet());
sharedPackages.addAll(exports.keySet());
sharedPackages.retainAll(usedPackages);
sharedPackages.remove(packageRef);
StringBuilder sb = new StringBuilder();
String del = "";
for (Iterator<PackageRef> u = sharedPackages.iterator(); u.hasNext(); ) {
PackageRef usedPackage = u.next();
if (!usedPackage.isJava()) {
sb.append(del);
sb.append(usedPackage.getFQN());
del = ",";
}
}
if (override.indexOf('$') >= 0) {
setProperty(CURRENT_USES, sb.toString());
override = getReplacer().process(override);
unsetProperty(CURRENT_USES);
} else
// This is for backward compatibility 0.0.287
// can be deprecated over time
override = override.replaceAll(USES_USES, Matcher.quoteReplacement(sb.toString())).trim();
if (override.endsWith(","))
override = override.substring(0, override.length() - 1);
if (override.startsWith(","))
override = override.substring(1);
if (override.length() > 0) {
clause.put(USES_DIRECTIVE, override);
}
}
}
use of aQute.bnd.osgi.Descriptors.PackageRef in project bnd by bndtools.
the class Analyzer method augmentExports.
/**
* Provide any macro substitutions and versions for exported packages.
*
* @throws IOException
*/
void augmentExports(Packages exports) throws IOException {
for (PackageRef packageRef : exports.keySet()) {
String packageName = packageRef.getFQN();
setProperty(CURRENT_PACKAGE, packageName);
Attrs attributes = exports.get(packageRef);
try {
Attrs exporterAttributes = classpathExports.get(packageRef);
if (exporterAttributes == null) {
if (check(Check.EXPORTS)) {
Map<String, Resource> map = dot.getDirectories().get(packageRef.getBinary());
if ((map == null || map.isEmpty())) {
error("Exporting an empty package '%s'", packageRef.getFQN());
}
}
} else {
for (Map.Entry<String, String> entry : exporterAttributes.entrySet()) {
String key = entry.getKey();
// dont overwrite and no directives
if (!key.endsWith(":")) {
if (!attributes.containsKey(key))
attributes.put(key, entry.getValue());
else {
if (since(About._2_4)) {
// and we have set it.
if (key.equals(Constants.VERSION_ATTRIBUTE)) {
try {
Version fromExport = new Version(cleanupVersion(exporterAttributes.getVersion()));
Version fromSet = new Version(cleanupVersion(attributes.getVersion()));
if (!fromExport.equals(fromSet)) {
SetLocation location = warning("Version for package %s is set to different values in the source (%s) and in the manifest (%s). The version in the manifest is not " + "picked up by an other sibling bundles in this project or projects that directly depend on this project", packageName, attributes.get(key), exporterAttributes.get(key));
if (getPropertiesFile() != null)
location.file(getPropertiesFile().getAbsolutePath());
location.header(EXPORT_PACKAGE);
location.context(packageName);
}
} catch (Exception e) {
// Ignored here, is picked up in
// other places
}
}
}
}
}
}
}
} finally {
unsetProperty(CURRENT_PACKAGE);
}
fixupAttributes(packageRef, attributes);
removeAttributes(attributes);
}
}
Aggregations