このタスクについて
Plain Old Java コードを使用することで、オブジェクトを作成し、BundleContext クラスを使用してそれをサービスとして登録することができます。
コードを実行する必要があるため、このオブジェクトの登録は通常 BundleActivator インターフェースで行います。オブジェクトを登録する際には、提供されるインターフェースを指定し、プロパティー・マップを指定することができます。ServiceRegistration オブジェクトが戻されます。
必要に応じて、この ServiceRegistration オブジェクトを使用していつでもプロパティーを変更できます。
サービスが完了したら、
ServiceRegistration オブジェクトを使用してサービスを登録抹消します。
サービスを入手するには、必要なインターフェースを実装するサービスを BundleContext で照会します。
オプションで、LDAP 構文フィルターを指定して、サービス・プロパティーの突き合わせを行います。
呼び出すメソッドに応じて、 最適な一致を取り出したり、すべての一致を取り出したりできます。
その後、プロパティーを提供する、戻された ServiceReference を使用して、コードでさらに突き合わせを行うことができます。
ServiceReference を使用して、実際のサービス・オブジェクトを取得できます。
サービスの使用が終わったら、BundleContext を使用してサービスを解放します。
- ご使用のバンドルに次のコードを追加して、サービス・インターフェースを宣言します。
package com.ibm.foo.simple;
/**
* Our multifunctional sample interface
*/
public interface Foo
{
}
- インターフェースの実装コードを指定します。
package com.ibm.foo.simple;
/**
* The implementation of the Foo interface
*/
public class FooImpl implements Foo
{
public FooImpl()
{
}
public FooImpl(String vendor)
{
}
/**
* used by the ServiceFactory implementation.
*/
public void destroy() {
}
}
- BundleContext を使用して、サービスの登録、サービス・プロパティーの変更、サービスの登録抹消をコードで直接行います。
import java.util.Dictionary;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
/**
* Registers and unregsiters a Foo service directly,
* and shows how to modify the service properties in code.
*/
public class FooController
{
private final BundleContext bundleContext;
private ServiceRegistration<Foo> sr;
public FooController( BundleContext bundleContext )
{
this.bundleContext = bundleContext;
}
public void register(Dictionary<String, Object> serviceProperties) {
Foo foo = new FooImpl();
//typed service registration with one interface
sr = bundleContext.registerService( Foo.class, foo, serviceProperties );
//or
//untyped service registration with one interface
sr = (ServiceRegistration<Foo>)bundleContext.registerService(
Foo.class.getName(), foo, serviceProperties );
//or
//untyped service registration with more than one interface (or class)
sr = (ServiceRegistration<Foo>)bundleContext.registerService(new String[] {
Foo.class.getName(), FooImpl.class.getName()}, foo, serviceProperties );
}
public void modifyFoo(Dictionary<String, Object> serviceProperties) {
//with the service registration you can modify the service properties at any time
sr.setProperties( serviceProperties );
}
public void unregisterFoo() {
//when you are done unregister the service using the service registration
sr.unregister();
}
}
- 以下のように、別のクラスからサービスを入手して戻します。
package com.ibm.foo.simple;
import java.util.Collection;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
/**
* A simple Foo client that directly obtains the Foo service and returns it when done.
*/
public class FooUser
{
private final BundleContext bundleContext;
public FooUser( BundleContext bundleContext )
{
this.bundleContext = bundleContext;
}
/**
* assume there's only one Foo
*/
public void useFooSimple() {
ServiceReference<Foo> sr = bundleContext.getServiceReference( Foo.class );
String[] propertyKeys = sr.getPropertyKeys();
for (String key: propertyKeys) {
Object prop = sr.getProperty( key );
//think about whether this is the Foo we want....
}
Foo foo = bundleContext.getService( sr );
try {
//use foo
} finally {
//we're done
bundleContext.ungetService( sr );
}
}
/**
* Use a filter to select a particular Foo. Note we get a collection back and have to pick one.
* @throws InvalidSyntaxException
*/
public void useFooFilter() throws InvalidSyntaxException {
Collection<ServiceReference<Foo>> srs = bundleContext.getServiceReferences(
Foo.class, "(&(service.vendor=IBM)(id='myFoo')" );
ServiceReference<Foo> sr = srs.iterator().next();
String[] propertyKeys = sr.getPropertyKeys();
for (String key: propertyKeys) {
Object prop = sr.getProperty( key );
//think about whether this is the Foo we want....
}
Foo foo = bundleContext.getService( sr );
try {
//use foo
} finally {
//we're done
bundleContext.ungetService( sr );
}
}
}