public List addingBundle()

in spi-fly/spi-fly-core/src/main/java/org/apache/aries/spifly/ProviderBundleTrackerCustomizer.java [80:182]


    public List<ServiceRegistration> addingBundle(final Bundle bundle, BundleEvent event) {
        BundleRevision bundleRevision = bundle.adapt(BundleRevision.class);
        if (bundle.equals(spiBundle) || ((bundleRevision != null) && ((bundleRevision.getTypes() & TYPE_FRAGMENT) == TYPE_FRAGMENT)))
            return null; // don't process the SPI bundle itself

        log(Level.FINE, "Bundle Considered for SPI providers: "
            + bundle.getSymbolicName());

        DiscoveryMode discoveryMode = DiscoveryMode.SERVICELOADER_CAPABILITIES;
        List<String> providedServices = null;
        Map<String, Object> customAttributes = new HashMap<String, Object>();
        if (bundle.getHeaders().get(SpiFlyConstants.REQUIRE_CAPABILITY) != null) {
            try {
                providedServices = readServiceLoaderMediatorCapabilityMetadata(bundle, customAttributes);
            } catch (InvalidSyntaxException e) {
                log(Level.FINE, "Unable to read capabilities from bundle " + bundle, e);
            }
        }

        String spiProviderHeader = getHeaderFromBundleOrFragment(bundle, SpiFlyConstants.SPI_PROVIDER_HEADER);
        if (providedServices == null && spiProviderHeader != null) {
            String header = spiProviderHeader.trim();
            if ("*".equals(header)) {
                providedServices = new ArrayList<String>();
            } else {
                providedServices = Stream.of(header.split(",")).map(String::trim).collect(toList());
            }
            discoveryMode = DiscoveryMode.SPI_PROVIDER_HEADER;
        }

        List<URL> serviceFileURLs = null;
        if (providedServices == null) {
            Entry<List<String>, List<URL>> autoServices = getFromAutoProviderProperty(bundle, customAttributes);

            providedServices = autoServices.getKey();
            serviceFileURLs = autoServices.getValue();
            discoveryMode = DiscoveryMode.AUTO_PROVIDERS_PROPERTY;
        }

        if (providedServices == null) {
            log(Level.FINE, "No provided SPI services. Skipping bundle: "
                    + bundle.getSymbolicName());
            return null;
        } else {
            log(Level.FINE, "Examining bundle for SPI provider: "
                    + bundle.getSymbolicName());
        }

        for (String serviceType : providedServices) {
            // Eagerly register any services that are explicitly listed, as they may not be found in META-INF/services
            activator.registerProviderBundle(serviceType, bundle, customAttributes);
        }

        if (serviceFileURLs == null) {
            serviceFileURLs = getServiceFileUrls(bundle);
        }

        final List<ServiceRegistration> registrations = new ArrayList<ServiceRegistration>();
        for (ServiceDetails details : collectServiceDetails(bundle, serviceFileURLs, discoveryMode)) {
            if (providedServices.size() > 0 && !providedServices.contains(details.serviceType))
                continue;

            try {
                final Class<?> cls = bundle.loadClass(details.instanceType);
                log(Level.FINE, "Loaded SPI provider: " + cls);

                if (details.properties != null) {
                    ServiceRegistration reg = null;
                    Object instance =
                        (details.properties.containsKey("service.scope") &&
                        "prototype".equalsIgnoreCase(String.valueOf(details.properties.get("service.scope")))) ?
                            new ProviderPrototypeServiceFactory(cls) :
                            new ProviderServiceFactory(cls);

                    SecurityManager sm = System.getSecurityManager();
                    if (sm != null) {
                        if (bundle.hasPermission(new ServicePermission(details.serviceType, ServicePermission.REGISTER))) {
                            reg = bundle.getBundleContext().registerService(
                                    details.serviceType, instance, details.properties);
                        } else {
                            log(Level.FINE, "Bundle " + bundle + " does not have the permission to register services of type: " + details.serviceType);
                        }
                    } else {
                        reg = bundle.getBundleContext().registerService(
                            details.serviceType, instance, details.properties);
                    }

                    if (reg != null) {
                        registrations.add(reg);
                        log(Level.FINE, "Registered service: " + reg);
                    }
                }

                activator.registerProviderBundle(details.serviceType, bundle, details.properties);
                log(Level.INFO, "Registered provider " + details.instanceType + " of service " + details.serviceType + " in bundle " + bundle.getSymbolicName());
            } catch (Exception | NoClassDefFoundError e) {
                log(Level.FINE,
                    "Could not load provider " + details.instanceType + " of service " + details.serviceType, e);
            }
        }

        return registrations;
    }