private OSGi safeRegisterEndpoint()

in jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Whiteboard.java [831:891]


    private <T> OSGi<?> safeRegisterEndpoint(
        CachingServiceReference<T> serviceReference,
        CxfJaxrsServiceRegistrator registrator) {

        Map<?, ?> properties = registrator.getProperties();

        Bundle originalBundle = _bundleContext.getBundle(
            Long.class.cast(properties.get("service.bundleid")).longValue());

        return
            changeContext(
                originalBundle.getBundleContext(),
                onlyGettables(
                    just(serviceReference),
                    _runtime::addNotGettableEndpoint,
                    _runtime::removeNotGettableEndpoint,
                    _log
                )
            ).recoverWith((t, e) ->
                just(serviceReference).
                effects(
                    _runtime::addErroredEndpoint,
                    _runtime::removeErroredEndpoint).
                effects(
                    ifErrorEnabled(
                        _log,
                        () -> "ServiceReference {} for endpoint produced " +
                            "error: {}",
                        e),
                    ifErrorEnabled(
                        _log,
                        () -> "Errored ServiceReference {} for endpoint left")
                ).
                then(nothing())
            ).flatMap(
                Whiteboard::getResourceProvider
            ).effects(
                rp -> _runtime.addApplicationEndpoint(
                    properties::get, serviceReference,
                    registrator.getBus(), rp.getResourceClass()),
                rp -> _runtime.removeApplicationEndpoint(
                    properties::get, serviceReference)
            ).effects(
                registrator::add,
                registrator::remove
            ).effects(
                ifDebugEnabled(
                    _log,
                    () -> "Registered endpoint " +
                        serviceReference.getServiceReference() + " into application " +
                            getServiceName(properties::get)
                ),
                ifDebugEnabled(
                    _log,
                    () -> "Unregistered endpoint " +
                        serviceReference.getServiceReference() + " from application " +
                            getServiceName(properties::get)
                )

            );
    }