多个不同的ServiceReferences何时引用同一个ServiceRegistration?

问题描述 投票:0回答:1

org.osgi.framework.ServiceReference
中所述:

框架中注册的每个服务都有一个唯一的 ServiceRegistration 对象,并且可能有多个不同的 ServiceReference 对象引用它。

但是 Eclipse 的实现

org.eclipse.osgi.internal.serviceregistry.ServiceReferenceImpl
:

public class ServiceReferenceImpl<S> implements ServiceReference<S> {
    /** Registered Service object. */
    private final ServiceRegistrationImpl<S> registration;

    /**
     * Construct a reference.
     *
     */
    ServiceReferenceImpl(ServiceRegistrationImpl<S> registration) {
        this.registration = registration;
        /* We must not dereference registration in the constructor
         * since it is "leaked" to us in the ServiceRegistrationImpl
         * constructor.
         */
    }

    ...
}

它的构造函数具有默认访问权限,并且仅在

org.eclipse.osgi.internal.serviceregistry.ServiceRegistrationImpl<S>
的构造函数中调用:

public class ServiceRegistrationImpl<S> implements ServiceRegistration<S>, Comparable<ServiceRegistrationImpl<?>> {

    /** Reference to this registration. */
    /* @GuardedBy("registrationLock") */
    private ServiceReferenceImpl<S> reference;

    /**
     * Construct a ServiceRegistration and register the service
     * in the framework's service registry.
     *
     */
    ServiceRegistrationImpl(ServiceRegistry registry, BundleContextImpl context, String[] clazzes, S service) {
        this.registry = registry;
        this.context = context;
        this.bundle = context.getBundleImpl();
        this.clazzes = clazzes; /* must be set before calling createProperties. */
        this.service = service; /* must be set before calling createProperties. */
        this.serviceid = registry.getNextServiceId(); /* must be set before calling createProperties. */
        this.contextsUsing = new ArrayList<>(10);

        synchronized (registrationLock) {
            this.state = REGISTERED;
            /* We leak this from the constructor here, but it is ok
             * because the ServiceReferenceImpl constructor only
             * stores the value in a final field without
             * otherwise using it.
             */
            this.reference = new ServiceReferenceImpl<>(this);
        }
    }
    ...
}

另外,

org.osgi.framework.ServiceRegistration
接口只有
getReference()
方法,而不是像
getReferences()
这样的方法。

所以我很困惑什么时候有多个不同的

ServiceReference
引用相同的
ServiceRegistration

eclipse service osgi
1个回答
0
投票

这取决于实施。有些会为每个 ServiceRegistration 使用单个共享 ServiceReference 对象。其他人可以使用多个。规范中的注意事项是代码不要依赖身份来确定两个 ServiceReference 是否引用相同的 ServiceRegistration。

© www.soinside.com 2019 - 2024. All rights reserved.