5.1 Introduction
The OSGi Service Layer defines a dynamic collaborative model that is
highly integrated with the Life Cycle Layer. The service model is a
publish, find and bind model. A service is a normal Java object that is
registered under one or more Java interfaces with the service registry.
Bundles can register services, search for them, or receive
notifications when their registration state changes.
OSGi Service Layer定義了一個(gè)與Life Cycle
Layer緊密結(jié)合的動(dòng)態(tài)協(xié)作模型,服務(wù)模型是發(fā)布,查找和綁定模型。一個(gè)服務(wù)是一個(gè)普通的java對(duì)象,在服務(wù)注冊(cè)器中注冊(cè)于一個(gè)或多個(gè)java接口之
下。bundles可以注冊(cè)服務(wù)(through its BundleContext),查找服務(wù),或當(dāng)服務(wù)的注冊(cè)狀態(tài)改變時(shí)得到通知。
5.1.1 Essentials
• Collaborative – The service layer must
provide a mechanism for bundles to publish, find, and bind to each
other’s services without having a priori knowledge of those bundles.
• Collaborative – 服務(wù)層必須給bundles提供一個(gè)機(jī)制用于發(fā)布,查找和綁定其他各自bundles的服務(wù),而不需要有這些bundles的先期知識(shí)。
• Dynamic – The service mechanism must be able to handle changes in the outside world and underlying structures directly.
• Dynamic – 服務(wù)機(jī)制必須能直接操作外部的和底層的改變。
• Secure – It must be possible to restrict access to services.
• Secure – 必須能限制服務(wù)的訪問。
• Reflective – Provide full access to the Service Layer’s internal state.
• Reflective – 提供對(duì)服務(wù)層內(nèi)部狀態(tài)的完全訪問。
• Versioning – Provide mechanisms that make it possible to handle the fact that bundles and their services evolve over time.
• Versioning – 提供機(jī)制,使其能夠操作bundle和它們的服務(wù)的變化
• Persistent Identifier – Provide a means for bundles to track services across Framework restarts.
• Persistent Identifier –提供一個(gè)方法時(shí)bundles能跟蹤服務(wù)不受框架重啟的影響。
5.1.2 Entities
• Service – An object registered with the service registry under one or
more interfaces together with properties. This object can be discovered
and used by bundles.
• Service – 一個(gè)對(duì)象,在服務(wù)注冊(cè)器中注冊(cè)于一個(gè)或多個(gè)接口的名下,服務(wù)可以有屬性,這個(gè)對(duì)象可以被bundles發(fā)現(xiàn)并使用。
• Service Registry – Holds the service registrations.
• Service Registry – 服務(wù)注冊(cè)器,保存服務(wù)暫住證
• Service Reference – A reference to a service. Provides access to the
service’s properties but not the actual service object. The service
object must be acquired through a bundle’s Bundle Context.
• Service Reference – 到一個(gè)服務(wù)的引用。提供了對(duì)這個(gè)服務(wù)的屬性的訪問,但是并不是真正的服務(wù)對(duì)象,真正的服務(wù)對(duì)象必須通過bundle的BundleContext對(duì)象獲得。
• Service Registration – The receipt provided when a service is
registered. The service registration allows the update of the service
properties and the unregistration of the service.
• Service Registration – 一個(gè)暫住證(意思是收到一個(gè)服務(wù)的注冊(cè),而且就注冊(cè)在這個(gè)證里),服務(wù)暫住證允許服務(wù)屬性的更新和注銷服務(wù)。
• Service Permission – The permission to use an interface name when registering or using a service.
• Service Permission – 一個(gè)權(quán)限用于當(dāng)注冊(cè)或使用一個(gè)服務(wù)時(shí)使用一個(gè)接口名字。
• Service Factory – A facility to let the registering bundle customize the service object for each using bundle.
• Service Factory – 一個(gè)靈活的工具,使注冊(cè)服務(wù)的bundle為每一個(gè)要使用這個(gè)服務(wù)的bundle定制服務(wù)對(duì)象。
• Service Listener – A listener to Service Events.
• Service Event – An event holding information about the registration, modification, or unregistration of a service object.
• Filter – An object that implements a simple but powerful filter language. It can select on properties.
• Invalid Syntax Exception – The exception thrown when a filter expression contains an error.
5.2 Services
In the OSGi Service Platform, bundles are built around a set of
cooperating services available from a shared service registry. Such an
OSGi service is defined semantically by its service interface and
implemented as a service object.
在OSGi服務(wù)平臺(tái)中,bundles從一個(gè)共享服務(wù)注冊(cè)器中建造一組可相互協(xié)作的服務(wù),這樣一個(gè)OSGi服務(wù)通過它的服務(wù)接口來定義的,并被實(shí)現(xiàn)為一個(gè)服務(wù)對(duì)象。
The service interface should be specified with as few implementation
details as possible. OSGi has specified many service interfaces for
common needs and will specify more in the future.
服務(wù)接口應(yīng)該盡可能少的指定實(shí)現(xiàn)細(xì)節(jié),OSGi已經(jīng)為通用需求指定了很多服務(wù)接口,以后會(huì)更多。
The service object is owned by, and runs within, a bundle. This bundle
must register the service object with the Framework service registry so
that the service’s functionality is available to other bundles under
control of the Framework.
服務(wù)對(duì)象屬于一個(gè)bundle,并在bundle中運(yùn)行,bundle必須將這個(gè)服務(wù)對(duì)象注冊(cè)到框架服務(wù)注冊(cè)器中,以便在框架的控制下,這個(gè)服務(wù)的功能可以被其他bundles使用。
Dependencies between the bundle owning the service and the bundles
using it are managed by the Framework. For example, when a bundle is
stopped, all the services registered with the Framework by that bundle
must be automatically unregistered.
提供服務(wù)的bundle和使用服務(wù)的bundle之間的依賴關(guān)系由框架來管理。例如,當(dāng)一個(gè)bundle停止后,所有這個(gè)bundle注冊(cè)到框架的服務(wù)必須自動(dòng)注銷(由框架來注銷)。
The Framework maps services to their underlying service objects, and
provides a simple but powerful query mechanism that enables a bundle to
request the services it needs. The Framework also provides an event
mechanism so that bundles can receive events of services that are
registered, modified, or unregistered.
框架將服務(wù)映射到它們的服務(wù)對(duì)象,并提供一個(gè)簡(jiǎn)單而強(qiáng)大的查詢機(jī)制,使一個(gè)bundle請(qǐng)求一個(gè)它需要的服務(wù)。框架頁提供了一個(gè)事件機(jī)制以便bundles能接收服務(wù)注冊(cè),修改或注銷的事件。
5.2.1 Service References
In general, registered services are referenced through ServiceReference
objects. This avoids creating unnecessary dynamic service dependencies
between bundles when a bundle needs to know about a service but does
not require the service object itself.
通常,注冊(cè)的服務(wù)通過ServiceReference對(duì)象去引用它,這就避免了當(dāng)一個(gè)bundle需要知道一個(gè)服務(wù)但是不需要這個(gè)服務(wù)對(duì)象本身時(shí),創(chuàng)建一個(gè)不必要的bundles之間的動(dòng)態(tài)服務(wù)依賴關(guān)系。
A ServiceReference object can be stored and passed on to other bundles
without the implications of dependencies. When a bundle wishes to use
the service, it can be obtained by passing the ServiceReference object
to BundleContext.getService(ServiceReference). See Locating Services on
page 114.
一個(gè)ServiceReference
對(duì)象可以被保存并傳到別的bundles,而不需要實(shí)現(xiàn)依賴關(guān)系。當(dāng)一個(gè)bundle想使用這個(gè)服務(wù)時(shí),它能通過將ServiceReference
對(duì)象傳到BundleContext.getService(ServiceReference)來獲得
A ServiceReference object encapsulates the properties and other
metainformation about the service object it represents. This
meta-information can be queried by a bundle to assist in the selection
of a service that best suits its needs.
When a bundle queries the Framework service registry for services, the
Framework must provide the requesting bundle with the ServiceReference
objects of the requested services, rather than with the services
themselves.
當(dāng)一個(gè)bundle在框架服務(wù)注冊(cè)器中查詢服務(wù)時(shí),框架必須給這個(gè)查詢bundle提供它請(qǐng)求的服務(wù)的ServiceReference對(duì)象,而不是這些服務(wù)本身。
A ServiceReference object may also be obtained from a ServiceRegistration object.
一個(gè)ServiceReference對(duì)象也可以從ServiceRegistration對(duì)象那獲得。
A ServiceReference object is valid only as long as the service object
is registered. However, its properties must remain available as long as
the ServiceReference object exists.
ServiceReference對(duì)象僅僅只在服務(wù)對(duì)象被注冊(cè)時(shí)可以使用,但是它的屬性會(huì)一直可用只要ServiceReferenc對(duì)象存在。
5.2.2 Service Interfaces
A service interface is the specification of the service’s public
methods. In practice, a bundle developer creates a service object by
implementing its service interface and registers the service with the
Framework service registry. Once a bundle has registered a service
object under an interface name, the associated service can be acquired
by bundles under that interface name,
and its methods can be accessed by way of its service interface. The
Framework also supports registering service objects under a class name,
so references to service interface in this specification can be
interpreted to be an interface or class.
一個(gè)服務(wù)接口是服務(wù)的公共方法的規(guī)范,在實(shí)際應(yīng)用中,一個(gè)bundle開發(fā)者通過實(shí)現(xiàn)服務(wù)接口來創(chuàng)建一個(gè)服務(wù)對(duì)象,并將這個(gè)服務(wù)注冊(cè)到框架服務(wù)注冊(cè)器中。
一旦一個(gè)bundle以一個(gè)接口名注冊(cè)了一個(gè)服務(wù)對(duì)象,這個(gè)關(guān)聯(lián)的服務(wù)就能在bundles中通過接口名獲得,它的方法能以它的服務(wù)接口的方式訪問。框架
頁支持以類名的方式注冊(cè)服務(wù),所以規(guī)范中的服務(wù)接口可以被理解為一個(gè)接口或一個(gè)類(當(dāng)然一般情況下都是用接口)。
When requesting a service object from the Framework, a bundle can
specify the name of the service interface that the requested service
object must implement. In the request, the bundle may also specify a
filter string to narrow the search.
當(dāng)一個(gè)bundle從框架中請(qǐng)求一個(gè)服務(wù)對(duì)象時(shí),它能指定那個(gè)被請(qǐng)求的服務(wù)對(duì)象必須實(shí)現(xiàn)的服務(wù)接口的名字,請(qǐng)求過程中,bundle也可以指定一個(gè)過濾器字符串來縮小查找范圍。
Many service interfaces are defined and specified by organizations such
as the OSGi Alliance. A service interface that has been accepted as a
standard can be implemented and used by any number of bundle developers.
很多服務(wù)接口被OSGi聯(lián)盟這樣的組織定義和指定了,一個(gè)被公認(rèn)為標(biāo)準(zhǔn)的服務(wù)接口可以被bundle開發(fā)者實(shí)現(xiàn)并使用。
例如:(參見org.eclipse.osgi.framework.internal.core.FrameworkCommandProvider)
ServiceReference packageAdminRef =
bundleContext.getServiceReference("org.osgi.service.packageadmin.PackageAdmin");
5.2.3 Registering Services
A bundle publishes a service by registering a service object with the
Framework service registry. A service object registered with the
Framework is exposed to other bundles installed in the OSGi environment.
一個(gè)bundle通過將一個(gè)服務(wù)對(duì)象注冊(cè)到框架服務(wù)注冊(cè)器來發(fā)布一個(gè)服務(wù),一個(gè)被注冊(cè)的服務(wù)對(duì)象被暴露給其他安裝了的bundles。
Every registered service object has a unique ServiceRegistration
object, and has one or more ServiceReference objects that refer to it.
These ServiceReference objects expose the registration properties of
the service object, including the set of service interfaces they
implement. The ServiceReference object can then be used to acquire a
service object that
implements the desired service interface. The Framework permits bundles
to register and unregister service objects dynamically. Therefore, a
bundle is permitted to register service objects at any time during the
STARTING, ACTIVE or STOPPING states.
每個(gè)注冊(cè)的服務(wù)對(duì)象都有一個(gè)唯一的ServiceRegistration對(duì)象,并且有一個(gè)或多個(gè)ServiceReference對(duì)象引用它,這些
ServiceReference對(duì)象暴露服務(wù)對(duì)象的注冊(cè)屬性,包括它們實(shí)現(xiàn)的服務(wù)接口,然后可以使用ServiceReference對(duì)象來獲取實(shí)現(xiàn)了
服務(wù)接口的服務(wù)對(duì)象。框架允許bundle動(dòng)態(tài)地注冊(cè)和注銷服務(wù)對(duì)象,因此一個(gè)bundle可以在STARTING,ACTIVE或STOPPING狀態(tài)
注冊(cè)服務(wù)對(duì)象。
A bundle registers a service object with the Framework by calling one
of the BundleContext.registerService methods on its BundleContext
object:
• registerService(String,Object,Dictionary) – For a service object registered under a single service interface.
• registerService(String[],Object,Dictionary) – For a service object registered under multiple service interfaces.
一個(gè)bundle可以通過調(diào)用以下方法注冊(cè)服務(wù)對(duì)象:
• registerService(String,Object,Dictionary) – 將一個(gè)服務(wù)對(duì)象注冊(cè)到一個(gè)服務(wù)接口名下
• registerService(String[],Object,Dictionary) –將一個(gè)服務(wù)對(duì)象注冊(cè)到多個(gè)服務(wù)接口名下
The names of the service interfaces under which a bundle wants to
register its service are provided as arguments to the registerService
methods. The Framework must ensure that the service object actually is
an instance of each specified service interfaces, unless the object is
a Service Factory. See Service Factory on page 117.
一個(gè)bundle注冊(cè)它的服務(wù)(一個(gè)對(duì)象)到一個(gè)(些)服務(wù)接口,這個(gè)(些)服務(wù)接口的名字做為參數(shù)提供給registerService方法。框架必須確保服務(wù)對(duì)象是這些服務(wù)接口的實(shí)例,除非這個(gè)服務(wù)對(duì)象是一個(gè)ServiceFactory
To perform this check, the Framework must load the Class object for
each specified service interface from either the bundle or a shared
package. For each Class object, Class.isInstance must be called and
return true on the Class object with the service object as the argument.
對(duì)于執(zhí)行這個(gè)檢查,框架必須為這些來自bundle或共享包的服務(wù)接口加載Class對(duì)象,調(diào)用Class.isInstance方法,服務(wù)對(duì)象作為參數(shù),必須返回true
The service object being registered may be further described by a
Dictionary object, which contains the properties of the service as a
collection of key/value pairs.
被注冊(cè)的服務(wù)對(duì)象后面的一個(gè)被Dictionary對(duì)象描述的參數(shù),可能包含了將服務(wù)的屬性做為一個(gè)鍵值對(duì)集合。
The service interface names under which a service object has been
successfully registered are automatically added to the service object’s
properties under the key objectClass. This value must be set
automatically by the Framework and any value provided by the bundle
must be overridden.
一個(gè)服務(wù)對(duì)象被成功注冊(cè)于服務(wù)接口的名下,這個(gè)(些)接口的名字會(huì)被自動(dòng)添加到服務(wù)對(duì)象的屬性中,鍵名為objectClass(值為字符串?dāng)?shù)組類型)。這些值必須由框架自動(dòng)添加,bundle提供的值必須被覆蓋。
If the service object is successfully registered, the Framework must
return a ServiceRegistration object to the caller. A service object can
be unregistered only by the holder of its ServiceRegistration object
(see the unregister() method). Every successful service object
registration must yield a unique ServiceRegistration object even if the
same service object is registered multiple times.
如果服務(wù)對(duì)象被成功注冊(cè),框架必須返回一個(gè)ServiceRegistration對(duì)象給調(diào)用者。一個(gè)服務(wù)對(duì)象僅僅只能被持有它的
ServiceRegistration對(duì)象注銷。每一次服務(wù)對(duì)象的成功注冊(cè)都要伴隨生成一個(gè)ServiceRegistration對(duì)象,即使一個(gè)相同
的服務(wù)對(duì)象已經(jīng)被注冊(cè)過了。
5.2.4 Early Need for ServiceRegistration Object
The registration of a service object will cause all registered
ServiceListener objects to be notified. This is a synchronous
notification. This means that such a listener can get access to the
service and call its methods before the registerService method has
returned the ServiceRegistration object. In certain cases, access to
the ServiceRegistration object is necessary in such a callback.
However, the registering bundle has not yet received the
ServiceRegistration object. Figure 5.30 on page 111 shows such a sequence.
一個(gè)服務(wù)對(duì)象的注冊(cè)會(huì)使所有注冊(cè)的ServiceListener對(duì)象得到通知(有服務(wù)被注冊(cè)了),這是一個(gè)同步通知,這就意味著一個(gè)監(jiān)聽器能在
registerService方法返回ServiceRegistration對(duì)象之前訪問服務(wù)并調(diào)用服務(wù)的方法。在某些情況下,在這樣的回調(diào)方法中訪
問ServiceRegistration對(duì)象是有必要的,但是注冊(cè)服務(wù)的bundle此時(shí)并沒有收到ServiceRegistration對(duì)象。
In a case as described previously, access to the registration object
can be obtained via a ServiceFactory object. If a ServiceFactory object
is registered, the Framework must call-back the registering bundle with
the ServiceFactory method getService(Bundle,ServiceRegistration). The
required ServiceRegistration object is passed as a parameter to this
method.
在前面描述的例子中,可以通過ServiceFactory對(duì)象來訪問注冊(cè)對(duì)象,如果一個(gè)ServiceFactory對(duì)象被注冊(cè),框架必須以
ServiceFactory.getService(Bundle,ServiceRegistration)方法回調(diào)注冊(cè)bundle。
ServiceRegistration對(duì)象做為參數(shù)傳入這個(gè)方法中。
5.2.5 Service Properties
Properties hold information as
key/value pairs. The key must be a String object and the value should
be a type recognized by Filter objects (see Filters on page 116 for a
list). Multiple values for the same key are supported with arrays ([])
and Collection objects.
The values of properties should be limited to primitive or standard
Java types to prevent unwanted inter bundle dependencies. The Framework
cannot detect dependencies that are created by the exchange of objects
between bundles via the service properties.
The key of a property is not case sensitive. ObjectClass, OBJECTCLASS
and objectclass all are the same property key. A Framework must return
the key in ServiceReference.getPropertyKeys in exactly the same case as
it was last set. When a Dictionary object that contains keys that only
differ in case is passed, the Framework must raise an exception.
The service properties are intended to provide information about the
service object. The properties should not be used to participate in the
actual function of the service. Modifying the properties for the
service registration is a potentially expensive operation. For example,
a Framework may pre-process the properties into an index during
registration to speed up later lookups.
The Filter interface supports complex filtering; it can be used to find
matching service objects. Therefore, all properties share a single name
space in the Framework service registry. As a result, it is important
to use descriptive names or formal definitions of shorter names to
prevent conflicts. Several OSGi specifications reserve parts of this
name space. All properties starting with the prefix service. and the
property objectClass are reserved for use by
OSGi specifications.
Table 5.9 Standard Service Properties contains a list of pre-defined properties.
Table 5.9 Standard Service Properties
Property Key |
Type |
Constants |
Property Description |
service.id |
Long |
SERVICE_ID |
Every registered service object is
assigned a unique service.id by the
Framework. This number is added
to the service object’s properties.
The Framework assigns a unique
value to every registered service
object that is larger than values provided
to all previously registered
service objects. |
service.pid |
String |
SERVICE_PID |
The service.pid property optionally
identifies a persistent, unique
identifier for the service object. See
Persistent Identifier (PID) on page
113. |
service.ranking |
Integer |
SERVICE_RANKING |
When registering a service object, a
bundle may optionally specify a
service.ranking number as one of
the service object’s properties. If
multiple qualifying service interfaces
exist, a service with the highest
SERVICE_RANKING number, or
when equal to the lowest
SERVICE_ID, determines which service
object is returned by the Framework. |
service.vendor |
String |
SERVICE_VENDOR |
This optional property can be used
by the bundle registering the service
object to indicate the vendor. |
5.2.6 Persistent Identifier (PID)
The purpose of a Persistent Identifier (PID) is to identify a service
across Framework restarts. Services that can reference the same
underlying entity every time they are registered should therefore use a
service property that contains a PID. The name of the service property
for PID is defined as service.pid. The PID is a unique identifier for a
service that persists over
multiple invocations of the Framework. For a given service, the same
PID should always be used. If the bundle is stopped and later started,
the same PID must always be used.
The format of the PID should be:
pid ::= symbolic-name // See 1.3.2
A PID must be unique for each service. A bundle must not register
multiple services with the same PID, nor should other bundles use the
same PID. If this happens, it is an error condition.
Persistent Identifier (PID)的目的是為了在框架重啟后識(shí)別一個(gè)服務(wù),每次服務(wù)注冊(cè)時(shí),都引用同一個(gè)實(shí)體,因此應(yīng)當(dāng)使用一個(gè)包含了PID的服務(wù)屬性。服務(wù)屬性的名字被定義為service.pid。
5.2.7 Locating Services
In order to use a service object and call its methods, a bundle must
first obtain a ServiceReference object. The BundleContext interface
defines two methods a bundle can call to obtain ServiceReference
objects from the Framework:
為了使用一個(gè)服務(wù)對(duì)象并調(diào)用它的方法,一個(gè)bundle必須首先獲得一個(gè)ServiceReference對(duì)象。接口BundleContext定義了2個(gè)方法來從框架中獲取ServiceReference對(duì)象:
• getServiceReference(String) – This method returns a ServiceReference
object to a service object that implements, and was registered under,
the name of the service interface specified as String. If multiple such
service objects exist, the service object with the highest
SERVICE_RANKING is returned. If there is a tie in ranking, the service
object with the lowest
SERVICE_ID (the service object that was registered first) is returned.
• getServiceReference(String) –
這個(gè)方法返回一個(gè)到一個(gè)服務(wù)對(duì)象的ServiceReference對(duì)象,這個(gè)服務(wù)對(duì)象實(shí)現(xiàn)并注冊(cè)于參數(shù)中String指定的服務(wù)接口,如果有多個(gè)這樣的
服務(wù)對(duì)象存在,具有最高SERVICE_RANKING的服務(wù)對(duì)象被返回。如果有2個(gè)相同的SERVICE_RANKING,那么具有最低
SERVICE_ID(先被安裝的)的服務(wù)對(duì)象被返回。
• getServiceReferences(String,String) – This method returns an array of ServiceReference objects that:
• Implement and were registered under the given service interface.
• Satisfy the search filter specified. The filter syntax is further explained in Filters on page 116.
Both methods must return null if no matching service objects are
returned. Otherwise, the caller receives one or more ServiceReference
objects. These objects can be used to retrieve properties of the
underlying service object, or they can be used to obtain the actual
service object via the BundleContext object.
如果沒有匹配的服務(wù)對(duì)象,這兩個(gè)方法必須返回null,否則,調(diào)用者將收到1個(gè)或多個(gè)ServiceReference對(duì)象。這些對(duì)象能被用來獲取服務(wù)對(duì)象屬性,或是通過BundleContext來獲取真正的服務(wù)對(duì)象。
Both methods require that the caller has the required
ServicePermission[<name>, GET] to get the service object for the
specified service interface names. If the caller lacks the required
permission, these methods must return null.
這兩個(gè)方法需要調(diào)用者有ServicePermission[<name>, GET]權(quán)限通過制定的服務(wù)接口名去獲得服務(wù)對(duì)象,如果調(diào)用者沒有這個(gè)權(quán)限,這兩個(gè)方法必須返回null。
5.2.8 Getting Service Properties
To allow for interrogation of service objects, the ServiceReference interface defines these two methods:
• getPropertyKeys() – Returns an array of the property keys that are available.
• getProperty(String) – Returns the value of a property.
Both of these methods must continue to provide information about the
referenced service object, even after it has been unregistered from the
Framework. This requirement can be useful when a ServiceReference
object is stored with the Log Service.
5.2.9 Getting Service Objects
The BundleContext object is used to obtain the actual service object so
that the Framework can manage dependencies. If a bundle retrieves a
service object, that bundle becomes dependent upon the life cycle of
that registered service object. This dependency is tracked by the
BundleContext object used to obtain the service object, and is one
reason that it is important to be careful when sharing BundleContext
objects with other bundles.
BundleContext對(duì)象用于獲取真實(shí)的服務(wù)對(duì)象,因此框架要管理它們的依賴關(guān)系。如果一個(gè)bundle獲得了一個(gè)服務(wù)對(duì)象,那么這個(gè)bundle
將依賴于那個(gè)注冊(cè)的服務(wù)對(duì)象的生命周期,這個(gè)依賴關(guān)系由用于獲取服務(wù)對(duì)象的BundleContext去跟蹤,這樣做的原因是防止bundles間共享
BundleContext。
The method BundleContext.getService(ServiceReference) returns an object
that implements the interfaces as defined by the objectClass property.
方法BundleContext.getService(ServiceReference)返回一個(gè)實(shí)現(xiàn)了屬性中"objectClass"指定接口的對(duì)象。
This method has the following characteristics:
這個(gè)方法有以下特性:
• Returns null if the underlying service object has been unregistered.
• 如果服務(wù)對(duì)象已經(jīng)被注銷,返回null
• Determines if the caller has ServicePermission[<interface
name>,GET], to get the service object using at least one of the
service interfaces under which the service was registered. This
permission check is necessary so that ServiceReference objects can be
passed around freely without compromising security.
• 決定調(diào)用者是否有ServicePermission[<interface
name>,GET]權(quán)限使用至少其中一個(gè)服務(wù)接口去得到一個(gè)注冊(cè)于該服務(wù)接口名下的服務(wù)對(duì)象。這個(gè)檢查是有必要的,這樣才能使
ServiceReference對(duì)象自由傳遞而不需要考慮安全問題。
• Increments the usage count of the service object by one for this BundleContext object.
• 這個(gè)BundleContext對(duì)象使用這個(gè)服務(wù)對(duì)象的次數(shù)加1.
• If the service object does not implement the ServiceFactory
interface, it is returned. Otherwise, if the bundle context’s usage
count of the service object is one, the object is cast to a
ServiceFactory object and the getService method is called to create a
customized service object for the calling bundle which is then
returned. Otherwise, a cached copy of this
customized object is returned. See Service Factory on page 117 for more information about ServiceFactory objects.
•
如果服務(wù)對(duì)象沒有實(shí)現(xiàn)ServiceFactory接口,它直接被返回。否則,如果bundlecontext使用這個(gè)服務(wù)對(duì)象的次數(shù)是1,這個(gè)對(duì)象將被
cast為一個(gè)ServiceFactory對(duì)象,getService方法被調(diào)用來為調(diào)用bundle生成一個(gè)定制的服務(wù)對(duì)象,如果次數(shù)不是1,一個(gè)緩
存的定制對(duì)象被返回。
5.2.10 Information About Services
The Bundle interface defines these two methods for returning information pertaining to service usage of the bundles:
接口Bundle定義了2個(gè)方法用于返回bundles使用的服務(wù)的相關(guān)信息
• getRegisteredServices() – Returns the service objects that the bundle has registered with the Framework.
• getRegisteredServices() – 返回bundle已經(jīng)注冊(cè)的服務(wù)對(duì)象。
• getServicesInUse() – Returns the service objects that the bundle is using.
• getServicesInUse() – 返回bundle使用的服務(wù)對(duì)象。
5.3 Service Events
• ServiceEvent – Reports registration,
unregistration, and property changes for service objects. All events of
this kind must be delivered synchronously. The type of the event is
given by the getType() method, which returns an int. Event types can be
extended in the future;
unknown event types should be ignored.
• ServiceListener – Called with a ServiceEvent when a service object
has been registered or modified, or is in the process of unregistering.
A security check must be performed for each registered listener when a
ServiceEvent occurs. The listener must not be called unless the bundle
which registered the listener has the required
ServicePermission[<interface name>,GET] for at least one of the
interfaces under which the service object is registered.
• ServiceEvent – 報(bào)告服務(wù)對(duì)象的注冊(cè),注銷和屬性改變,所有這個(gè)類型的事件必須被同步發(fā)送。事件的類型通過getType()方法獲得,返回的是一個(gè)int。事件類型可以被擴(kuò)展,未知的時(shí)間類型應(yīng)該被忽略。
• ServiceListener –附帶一個(gè)ServiceEvent做為參數(shù)被調(diào)用于一個(gè)服務(wù)對(duì)象被注冊(cè)或改變,或是在注銷時(shí),
。當(dāng)一個(gè)ServiceEvent發(fā)生時(shí),必須為每一個(gè)注冊(cè)的監(jiān)聽器執(zhí)行安全檢查,如果監(jiān)聽器對(duì)一個(gè)服務(wù)接口沒有ServicePermission[<interface name>,GET]權(quán)限,監(jiān)聽器必須不能被調(diào)用。
A bundle that uses a service object should register a ServiceListener
object to track the availability of the service object, and take
appropriate action when the service object is unregistering.
一個(gè)使用服務(wù)對(duì)象的bundle應(yīng)該注冊(cè)一個(gè)ServiceListener對(duì)象來跟蹤這個(gè)服務(wù)對(duì)象的有效性,在服務(wù)對(duì)象注銷時(shí)采取合適的處理。
5.4 Stale References
The Framework must manage the dependencies between bundles. This
management is, however, restricted to Framework structures. Bundles
must listen to events generated by the Framework to clean up and remove
stale references.
框架必須管理bundles間的依賴關(guān)系,但是這個(gè)管理限制于框架結(jié)構(gòu)。bundles必須監(jiān)聽框架生成的事件,清理和移除失效的引用。
A stale reference is a reference to a Java object that belongs to the
class loader of a bundle that is stopped or is associated with a
service object that is unregistered. Standard Java does not provide any
generic means to clean up stale references, and bundle developers must
analyze their code carefully to ensure that stale references are
deleted.
一個(gè)失效的引用是指,引用了一個(gè)屬于停止了的bundle的classloader或是關(guān)聯(lián)了一個(gè)已經(jīng)注銷了的java對(duì)象。標(biāo)準(zhǔn)java沒有提供一個(gè)通用方法去清理失效的引用,bundle開發(fā)者必須仔細(xì)分析代碼以確保失效的引用被刪除。
Stale references are potentially harmful because they hinder the Java
garbage collector from harvesting the classes, and possibly the
instances, of stopped bundles. This may result in significantly
increased memory usage and can cause updating native code libraries to
fail. Bundles using services are strongly recommended to use either the
Service Tracker or Declarative
Services.
失效的引用是一個(gè)潛在的危險(xiǎn),因?yàn)樗鼈兎恋K了java
gc回收停止了的bundles的對(duì)象實(shí)例,這可能導(dǎo)致增加內(nèi)存的使用和引起本地代碼庫更新失敗。對(duì)于bundle使用services,強(qiáng)烈推薦使用
Service Tracker或Declarative Services。
Service developers can minimize the consequences of (but not completely
prevent) stale references by using the following mechanisms:
• Implement service objects using the ServiceFactory interface. The
methods in the ServiceFactory interface simplify tracking bundles that
use their service objects. See Service Factory on page 117.
• Use indirection in the service object implementations. Service
objects handed out to other bundles should use a pointer to the actual
service object implementation. When the service object becomes invalid,
the pointer is set to null, effectively removing the reference to the
actual service object.
服務(wù)開發(fā)者可以使用以下機(jī)制來盡可能的減少失效的引用:
• 服務(wù)對(duì)象實(shí)現(xiàn)ServiceFactory接口,接口ServiceFactory中的方法簡(jiǎn)化了對(duì)使用服務(wù)對(duì)象的bundles的跟蹤
• 使用簡(jiǎn)介引用服務(wù)對(duì)象的實(shí)現(xiàn)。bundles注冊(cè)的服務(wù)應(yīng)該使用一個(gè)指針指向真是的服務(wù)對(duì)象的實(shí)現(xiàn),當(dāng)服務(wù)對(duì)象不可用時(shí),這個(gè)指針設(shè)置為null,有效的移除對(duì)真實(shí)服務(wù)對(duì)象的引用。
The behavior of a service that becomes unregistered is undefined. Such
services may continue to work properly or throw an exception at their
discretion. This type of error should be logged.
一個(gè)服務(wù)注銷的行為沒有定義,這些服務(wù)可能會(huì)繼續(xù)工作或是隨意拋出一個(gè)exception,錯(cuò)誤類型應(yīng)該被記錄。
5.5 Filters
The Framework provides a Filter interface, and uses a filter syntax in
the getServiceReferences method that is defined in Filter Syntax on
page 29. Filter objects can be created by calling
BundleContext.createFilter(String) or
FrameworkUtil.createFilter(String) with the chosen filter string. The
filter supports the following match methods:
• match(ServiceReference) – Match the properties of the Service Reference performing key lookup in a case insensitive way.
• match(Dictionary) – Match the entries in the given Dictionary object performing key lookup in a case insensitive way.
• matchCase(Dictionary) – Match the entries in the given Dictionary object performing key lookup in a case sensitive way.
框架提供了一個(gè)Filter接口,并在方法getServiceReferences 中使用了一個(gè)過濾器(filter)語法,F(xiàn)ilter
對(duì)象可以通過調(diào)用BundleContext.createFilter(String)或
FrameworkUtil.createFilter(String)以一個(gè)過濾器字符串為參數(shù)來創(chuàng)建。filter 支持以下匹配方法:
• match(ServiceReference) – 匹配ServiceReference的屬性,不區(qū)分大小寫
• match(Dictionary) – 匹配給定的Dictionary 對(duì)象實(shí)體,不區(qū)分大小寫
• matchCase(Dictionary) – 匹配給定的Dictionary 對(duì)象實(shí)體,區(qū)分大小寫
A Filter object can be used numerous times to determine if the match
argument, a ServiceReference object or a Dictionary object, matches the
filter string that was used to create the Filter object.
一個(gè)Filter對(duì)象可以無限使用于決定參數(shù)ServiceReference 對(duì)象或Dictionary 對(duì)象是否匹配創(chuàng)建Filter對(duì)象時(shí)的過濾器字符串。
This matching requires comparing the value string in the filter to a
target object from the service properties or dictionary. This
comparison can be executed with the Comparable interface if the target
object’s class implements a constructor taking a single String object
and the class implements the Comparable interface. That is, if the
target object is of class Target, the class Target must implement:
• A constructor Target(String)
• Implement the java.lang.Comparable interface
這個(gè)匹配需要在過濾器中比對(duì)一個(gè)目標(biāo)對(duì)象中的字符串值與服務(wù)屬性或字典對(duì)象中的字符串值,如果目標(biāo)對(duì)象實(shí)現(xiàn)了接口Comparable并有以一個(gè)String對(duì)象為參數(shù)的構(gòu)造函數(shù),可以使用Comparable來執(zhí)行比對(duì)
If the target object does not implement java.lang.Comparable, the =,
~=, <=, >= operators must return only true when the objects are
equal (using the equals(Object) method). The Target class does not need
to be a public class.
如果目標(biāo)對(duì)象沒有實(shí)現(xiàn)Comparable接口,=, ~=, <=, >= 操作只有當(dāng)2個(gè)對(duì)象相等(使用equals(Object) 方法)的時(shí)候才返回true
The following example shows how a class can verify the ordering of an enumeration with a filter.
public class B implements Comparable {
String keys[] = {"bugs", "daffy", "elmer", "pepe"};
int index;
public B(String s) {
for ( index=0; index<keys.length; index++ )
if ( keys[index].equals(s) )
return;
}
public int compareTo( Object other ) {
B vother = (B) other;
return index - vother.index;
}
}
The class could be used with the following filter: (!(enum>=elmer)) -> matches bugs and daffy
The Filter.toString method must always return the filter string with unnecessary white space removed.
5.6 Service Factory
A Service Factory allows customization of the service object that is
returned when a bundle calls BundleContext.getService(ServiceReference).
當(dāng)一個(gè)bundle調(diào)用BundleContext.getService(ServiceReference)方法時(shí),ServiceFactory允許返回的是一個(gè)定制的服務(wù)對(duì)象。
Often, the service object that is registered by a bundle is returned
directly. If, however, the service object that is registered implements
the ServiceFactory interface, the Framework must call methods on this
object to create a unique service object for each distinct bundle that
gets the service.
通常,由一個(gè)bundle注冊(cè)的服務(wù)對(duì)象會(huì)直接返回。但是如果這個(gè)服務(wù)對(duì)象實(shí)現(xiàn)了ServiceFactory接口,框架必須調(diào)用這個(gè)對(duì)象中的方法來為各個(gè)不同的bundle創(chuàng)建一個(gè)唯一的服務(wù)對(duì)象。
When the service object is no longer used by a bundle – for example,
when that bundle is stopped – then the Framework must notify the
ServiceFactory object.
當(dāng)服務(wù)對(duì)象不再被bundle使用時(shí) - 例如,當(dāng)bundle停止時(shí) - 框架必須通知ServiceFactory對(duì)象。
ServiceFactory objects help manage bundle dependencies that are not
explicitly managed by the Framework. By binding a returned service
object to the requesting bundle, the service can be notified when that
bundle ceases to use the service, such as when it is stopped, and
release resources associated with providing the service to that bundle.
ServiceFactory對(duì)象幫助管理那些框架并不明確管理的bundle的依賴關(guān)系(例如服務(wù)注冊(cè)引發(fā)的依賴關(guān)系),通過將一個(gè)服務(wù)對(duì)象綁定到注冊(cè)
它的bundle,這個(gè)服務(wù)會(huì)在bundle終止使用它時(shí)得到通知,例如當(dāng)bundle停止,并釋放與服務(wù)相關(guān)聯(lián)的資源時(shí)。
The ServiceFactory interface defines the following methods:
接口ServiceFactory定義了以下方法:
• getService(Bundle,ServiceRegistration) – This method is called by the
Framework if a call is made to BundleContext.getService and the
following are true:
• getService(Bundle,ServiceRegistration) – 如果是通過BundleContext.getService調(diào)用,并滿足以下條件,這個(gè)方法將被框架調(diào)用來返回一個(gè)服務(wù)對(duì)象
• The ServiceReference argument to BundleContext.getService refers to
a service object that implements the ServiceFactory interface.
• BundleContext.getService中的參數(shù)ServiceReference對(duì)象引用的服務(wù)對(duì)象實(shí)現(xiàn)了接口ServiceFactory
• The bundle’s usage count of that service object is zero; that is,
the bundle currently does not have any dependencies on the service
object.
• bundle中的這個(gè)服務(wù)對(duì)象的使用次數(shù)是0,也就是說目前這個(gè)bundle對(duì)這個(gè)服務(wù)對(duì)象還沒有任何依賴關(guān)系。
The call to BundleContext.getService must be routed by the Framework
to this method, passing to it the Bundle object of the caller. The
Framework must cache the mapping of the requesting bundle-to-service,
and return the cached service object to the bundle on future calls to
BundleContext.getService, as long as the requesting bundle's usage
count of the service object is greater than zero. The Framework must
check the service object returned by this method. If it is not an
instance of all the classes named when the service factory was
registered, null is returned to the caller that called getService. This
check must be done as specified in Registering Services on page 110.
如果以上條件成立,框架必須將對(duì)BundleContext.getService的調(diào)用路由到這個(gè)方法
(ServiceFactory.getService(Bundle,ServiceRegistration)),將調(diào)用者的Bundle對(duì)象傳入。
框架必須緩存bundle對(duì)服務(wù)的請(qǐng)求的映射關(guān)系,當(dāng)下次這個(gè)bundle再次請(qǐng)求這個(gè)服務(wù)時(shí),直接返回緩存中的服務(wù)對(duì)象,并且要使請(qǐng)求的bundle對(duì)
這個(gè)服務(wù)的使用次數(shù)大于0.框架必須檢查這個(gè)方法返回的服務(wù)對(duì)象,如果這個(gè)對(duì)象(實(shí)現(xiàn)了ServiceFactory接口)不是注冊(cè)它時(shí)所指定的類名(這
個(gè)類名可以是接口,也可以是具體類)的實(shí)例,必須返回null。
• ungetService(Bundle,ServiceRegistration,Object) – This method is
called by the Framework if a call is made to BundleContext.ungetService
and the following are true:
• ungetService(Bundle,ServiceRegistration,Object) – 如果是通過BundleContext.ungetService調(diào)用,并滿足以下條件,這個(gè)方法將被框架調(diào)用
• The ServiceReference argument to BundleContext.ungetService refers
to a service object that implements the ServiceFactory interface.
• BundleContext.ungetService的參數(shù)ServiceReference對(duì)象引用的服務(wù)對(duì)象實(shí)現(xiàn)了ServiceFactory接口
• The bundle’s usage count for that service object must drop to zero
after this call returns; that is, the bundle is about to release its
last dependency on the service object.
• 這個(gè)調(diào)用返回之后,bundle對(duì)這個(gè)服務(wù)對(duì)象的使用次數(shù)減少到0;也就是說,bundle會(huì)釋放它對(duì)這個(gè)服務(wù)對(duì)象的全部依賴關(guān)系。
The call to BundleContext.ungetService must be routed by the Framework
to this method so the ServiceFactory object can release the service
object previously created.
Additionally, the cached copy of the previously created service object
must be unreferenced by the Framework so it may be garbage collected.
如果以上條件成立,框架必須將對(duì)BundleContext.ungetService
的調(diào)用路由到這個(gè)方法(ServiceFactory.ungetService(Bundle,ServiceRegistration)),使
ServiceFactory對(duì)象能釋放以前生成的服務(wù)對(duì)象。
另外,以前生成的服務(wù)對(duì)象的緩存片嘟啊必須被框架解除引用,使之可以被gc
5.7 Releasing Services
In order for a bundle to release a service object, it must remove the
dynamic dependency on the bundle that registered the service object.
The BundleContext interface defines a method to release service
objects: ungetService(ServiceReference). A ServiceReference object is
passed as the argument of this method.
為了使一個(gè)bundle釋放一個(gè)服務(wù)對(duì)象,它必須移除與注冊(cè)了服務(wù)對(duì)象的bundle上的動(dòng)態(tài)依賴關(guān)系。接口BundleContext定義了一個(gè)釋放服
務(wù)對(duì)象的方法:ungetService(ServiceReference),參數(shù)是一個(gè)ServiceReference對(duì)象。
This method returns a boolean value:
• false if the bundle’s usage count of the service object is already
zero when the method was called, or the service object has already been
unregistered.
• true if the bundle’s usage count of the service object was more than zero before this method was called.
這個(gè)方法返回一個(gè)boolean值:
• false - 當(dāng)這個(gè)方法被調(diào)用時(shí),bundle中對(duì)服務(wù)對(duì)象的使用次數(shù)是0,或者服務(wù)對(duì)象已經(jīng)被注銷。
• true - 這個(gè)方法調(diào)用時(shí),bundle中對(duì)服務(wù)對(duì)象的使用次數(shù)大于0
5.8 Unregistering Services
The ServiceRegistration interface defines the unregister() method to
unregister the service object. This must remove the service object from
the Framework service registry. The ServiceReference object for this
ServiceRegistration object can no longer be used to access the service
object.
ServiceRegistration接口定義了unregister()方法用于注銷服務(wù)對(duì)象,這個(gè)方法必須從框架的服務(wù)注冊(cè)器中移除這個(gè)服務(wù)對(duì)象。ServiceRegistration對(duì)象的ServiceReference對(duì)象將不再能用來訪問服務(wù)對(duì)象。
The fact that this method is on the ServiceRegistration object ensures
that only the bundle holding this object can unregister the associated
service object. The bundle that unregisters a service object, however,
might not be the same bundle that registered it. As an example, the
registering bundle could have passed the ServiceRegistration object to
another bundle, endowing that bundle with the responsibility of
unregistering the service object. Passing ServiceRegistration objects
should be done with caution.
ServiceRegistration對(duì)象中的這個(gè)方法實(shí)際上確保了只有持有這個(gè)對(duì)象的bundle能夠注銷這個(gè)關(guān)聯(lián)著的服務(wù)對(duì)象(
注:
之前提到不需要在Activator中手動(dòng)注銷服務(wù),因?yàn)榉?wù)的注銷由框架管理,當(dāng)Bundle停止時(shí),它的BundleContext會(huì)注銷服務(wù),參見
org.eclipse.osgi.framework.internal.core.BundleContextImpl.close)。注銷一個(gè)服務(wù)的bundle可能不是注冊(cè)它的那個(gè)bundle,例如,注冊(cè)服務(wù)的bundle可以將ServiceRegistration對(duì)象傳給別的bundle,由那個(gè)bundle負(fù)責(zé)注銷服務(wù)對(duì)象。傳遞ServiceRegistration對(duì)象應(yīng)慎重。
After ServiceRegistration.unregister successfully completes, the service object must be:
ServiceRegistration.unregister成功完成后,服務(wù)對(duì)象必須:
• Completely removed from the Framework service registry. Therefore,
ServiceReference objects obtained for that service object can no longer
be used to access the service object. Calling BundleContext.getService
method with the ServiceReference object must return null.
• 完全從框架服務(wù)注冊(cè)器中移除,因此用于獲取服務(wù)對(duì)象的ServiceReference對(duì)象無法再用來訪問這個(gè)服務(wù)對(duì)象,BundleContext.getService方法返回null。
• Unregistered, even if other bundles had dependencies upon it. Bundles
must be notified of the unregistration through the publishing of a
ServiceEvent object of type ServiceEvent.UNREGISTERING. This event is
sent synchronously in order to give bundles the opportunity to release
the service object. After receiving an event of type
ServiceEvent.UNREGISTERING, a bundle should release the service object
and release any references it has to this object, so that the service
object can be garbage collected by the Java VM.
•
被注銷,即使其他bundles還在依賴它。bundles必須通過發(fā)布的類型為ServiceEvent.UNREGISTERING的
ServiceEvent對(duì)象得到通知。這個(gè)事件發(fā)送是同步的,以便bundles有機(jī)會(huì)釋放服務(wù)對(duì)象。收到
ServiceEvent.UNREGISTERING事件后,bundle應(yīng)該釋放服務(wù)對(duì)象并釋放這個(gè)服務(wù)對(duì)象對(duì)這個(gè)bundle的引用.
• Released by all using bundles. For each bundle whose usage count for
the service object remains greater than zero after all invoked
ServiceListener objects have returned, the Framework must set the usage
count to zero and release the service object.
• 被所有使用它的bundles釋放(事件通知)。在所有ServiceListener得到通知之后,對(duì)于每個(gè)對(duì)這個(gè)服務(wù)對(duì)象的使用次數(shù)仍然大于0的bundle,框架必須釋放服務(wù)對(duì)象,并將使用次數(shù)設(shè)置為0.
5.9 Multiple Version Export Considerations
Allowing multiple bundles to export a package with a given name causes
some complications for Framework implementers and bundle programmers:
The class name no longer uniquely identifies the exported class. This
affects the service registry and permission checking.
允許多個(gè)bundles輸出一個(gè)包增加了框架實(shí)現(xiàn)和bundle開發(fā)者的復(fù)雜度,因?yàn)轭惷辉偈禽敵鲱惖奈ㄒ粯?biāo)識(shí)符,這會(huì)影響服務(wù)注冊(cè)和權(quán)限檢查。
5.9.1 Service Registry
Bundles must not be exposed to services for which there are conflicting
class loaders. A bundle that gets a service should be able to expect
that it can safely cast the service object to any of the associated
interfaces or classes under which the service was registered and that
it can access. NoClassCastExceptions should occur because those
interfaces do not come from the same class loader. The service registry
must therefore ensure that bundles can only see services that are not
incompatible with them. A service is not incompatible with the bundle
getting the service when that bundle is not wired to another source
class loader for this interface package than the bundle registering the
service. That is, it is either wired to the same source
class loader or it has no wire for that package at all.
在class
loaders沖突的情況下,bundles不能被暴露給服務(wù)。獲取一個(gè)服務(wù)的bundle應(yīng)該被認(rèn)為是能安全的將服務(wù)對(duì)象cast到關(guān)聯(lián)的接口或類。如
果接口和服務(wù)對(duì)象不是來自同一個(gè)class
loader,應(yīng)該拋出NoClassCastExceptions。因此服務(wù)注冊(cè)器必須確保bundles僅僅能看到與之兼容的服務(wù)。當(dāng)獲取服務(wù)的
bundle沒有為這個(gè)接口連接到其他源class
loader時(shí)(沒有連接到其他bundle),一個(gè)服務(wù)與獲取的服務(wù)的bundle兼容。也就是說,要嘛接口和服務(wù)對(duì)象連接到同一個(gè)源class
loader,要嘛根本就不連接它們(無法獲取服務(wù)對(duì)象)。
It is paramount that bundles are not accidentally confronted with
incompatible services. Therefore, the following methods need to filter
ServiceReference objects depending on the incompatibility of the
interfaces with the calling bundle. The bundle is identified by the
used BundleContext:
• getServiceReference(String) – Only return a Service Reference that is
not incompatible with the calling bundle for the specified interface.
• getServiceReferences(String,String) – Only return Service References
that are not incompatible with the calling bundle for the specified
interface.
有一點(diǎn)很重要,bundles絕對(duì)不能意外的遇到不兼容的服務(wù),因此,以下方法需要根據(jù)接口與調(diào)用bundle的兼容性來過濾ServiceReference對(duì)象,使用BundleContext來識(shí)別bundle
• getServiceReference(String) – 僅僅返回一個(gè)ServiceReference對(duì)象,它與調(diào)用bundle指定的接口兼容
• getServiceReferences(String,String) –返回多個(gè)ServiceReference對(duì)象。
The getAllServiceReferences(String,String) provides access to the
service registry without any compatibility restrictions. Services
acquired through this method can cause Class Cast Exceptions for the
correct class names.
The ServiceReference isAssignableTo(Bundle,String) method is also
available to test if the bundle that registered the service referenced
by this ServiceReference and the specified bundle are both wired to
same source for the specified interface.
5.9.2 Service Events
Service events must only be delivered to event listeners that are not incompatible with the Service Reference.
服務(wù)事件必須被發(fā)送到與ServiceReference兼容的事件監(jiān)聽器。
Some bundles need to listen to all service events regardless the
compatibility issues. A new type of ServiceListener is therefore added:
AllServiceListener. This is a marker interface; it extends
ServiceListener. Listeners that use this marker interface indicate to
the Framework that they want to see all services, including services
that are incompatible with them.
一些bundles需要監(jiān)聽所有服務(wù)事件而不想關(guān)心兼容性問題,因此,添加了一個(gè)新的對(duì)象類型:
AllServiceListener。這是一個(gè)標(biāo)記接口,它繼承了ServiceListener。使用這個(gè)標(biāo)記接口的監(jiān)聽器表示它想要監(jiān)聽所有的服
務(wù),包括那些不兼容的服務(wù)。