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

guicsvcw  于 2023-10-18  发布在  Eclipse
关注(0)|答案(1)|浏览(177)

org.osgi.framework.ServiceReference中所述:
在Framework中注册的每个服务都有一个唯一的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

oknwwptz

oknwwptz1#

这取决于执行。有些将为每个ServiceRegistration使用单个共享ServiceReference对象。其他人可以使用多个。规范中需要注意的一点是,代码不依赖于标识来确定两个ServiceReferences是否引用同一个ServiceRegistration。

相关问题