2010年12月21日火曜日

5.2.9 サービスオブジェクトの取得 (Getting Service Objects)

フレームワークが依存関係を管理できるようにするため, 実際のサービスオブジェクトを取得するのに BundleContext オブジェクトが使用される. バンドルがサービスオブジェクトを取得すると, そのバンドルは, 登録済みのサービスオブジェクトのライフサイクルに依存することになる. この依存関係は, 当該サービスオブジェクトを取得するのに使用した BundleContext オブジェクトにより追跡される. そしてこれが, BundleContext オブジェクトを他のバンドルと共有する際に注意を払うことが重要であるということの一つの理由となっている.

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 objecxt 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.getService(ServiceReference) は, objectClass プロパティーで定義されたインターフェース群を実装するオブジェクトを返す.

The method BundleContext.getService(ServiceReference) returns an object that implements the interfaces as defined by the objectClass property.

このメソッドには以下の特徴がある:

This method has the following characteristics:

  • 対応するサービスオブジェクトが既に登録解除されている場合は null を返す.
  • 呼び出し元が当該サービスリファレンスに関連付けられたサービスオブジェクトを取得するための ServicePermission [ServiceReference, GET] パーミッションを持っているかどうかを判定する. セキュリティーを損なうことなく ServiceReference オブジェクトを自由に取り交わすために, このパーミッション検査が必要となる.
  • この BundleContext オブジェクトにおける当該サービスオブジェクトの使用カウントを一つ増やす.
  • サービスオブジェクトが ServiceFactory インターフェースを実装していない場合, そのサービスオブジェクトが返される. そうでない場合, 当該バンドルコンテキストにおける, そのサービスオブジェクトの使用カウントが 1 であれば, そのオブジェクトを ServiceFactory オブジェクトへとキャストし, 呼び出し元バンドル用にカスタマイズされたサービスオブジェクトを生成するために getService メソッドを呼び, 生成されたオブジェクトを返す. ServiceFactory オブジェクトに関する情報については 134 ページの「サービスファクトリー」を参照のこと.

  • Returns null if the underlying service object has been unregistered.
  • Determines if the caller has ServicePermission [ServiceReference, GET], to get the service object associated with the Service Reference. This permission check is necessary so that ServiceReference objects can be passed around freely without compromising security.
  • Increments the usage count of the service object by one for this BundleContext object.
  • 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 134 for more information about ServiceFactory objects.