void writeBundleSourceJson()

in src/main/java/org/apache/sling/tooling/support/source/impl/SourceReferencesServlet.java [94:153]


    void writeBundleSourceJson(Writer writer, Bundle... bundles) throws IOException {
        final JSONWriter w = new JSONWriter(writer);
        w.array();

        for (Bundle bundle : bundles) {

            // skip bundle if it is a fragment (http://stackoverflow.com/questions/11655295/using-the-osgi-api-how-do-i-find-out-if-a-given-bundle-is-a-fragment)
            if ((bundle.adapt(BundleRevision.class).getTypes() & BundleRevision.TYPE_FRAGMENT) != 0) {
                log.debug("Skip bundle '{}' because it is a fragment", bundle);
                // source references should only be listed with the host bundle
                continue;
            }
            String bundleVersion = bundle.getVersion().toString();

            w.object();
            w.key(Constants.BUNDLE_SYMBOLICNAME);
            w.value(bundle.getSymbolicName());
            w.key(Constants.BUNDLE_VERSION);
            w.value(bundleVersion);

            w.key("sourceReferences");
            w.array();

            // the system bundle is embedded by the launchpad jar so we need special handling
            // since the pom.properties file is not located in the bundle
            if ( bundle.getBundleId() == Constants.SYSTEM_BUNDLE_ID && FELIX_FW_ARTIFACT_ID.equals(bundle.getSymbolicName()) ) {
                writeMavenGav(w, FELIX_FW_GROUP_ID, FELIX_FW_ARTIFACT_ID, (String) bundleVersion);
            }

            // look for pom.properties in the bundle ( the original bundle, fragments )
            collectMavenSourceReferences(w, bundle);

            // look for pom.properties in jars embedded in the bundle
            for ( String jar : getEmbeddedJars(bundle)) {
                URL entry = bundle.getEntry(jar);
                // incorrect or inaccessible entry
                if ( entry == null ) {
                    continue;
                }
                collectMavenSourceReferences(w, entry);
            }

            // query custom finders for source references
            for ( SourceReferenceFinder finder : finders ) {
                try {
                    for ( SourceReference reference : finder.findSourceReferences(bundle)) {
                        log.debug("{} found reference {}:{}:{} in {}", new Object[] { finder, reference.getGroupId(), reference.getArtifactId(), reference.getVersion(), bundle});
                        writeMavenGav(w, reference.getGroupId(), reference.getArtifactId(), reference.getVersion());
                    }
                } catch (SourceReferenceException e) {
                    log.warn(finder + " execution did not complete normally for " + bundle, e);
                }
            }

            w.endArray();
            w.endObject();
        }

        w.endArray();
    }