오브젝트를 작성하여 이를 써드파티 기능에서 사용할 수 있도록
OSGi 서비스로 등록할 수 있습니다.
이 태스크 정보
일반 이전 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 );
}
}
}