To access an OSGi service from a Java™ agent, you import the packages of the service
and then include the OSGi FrameworkUtil class to
access this service from your code.
Procedure
- In a Java agent,
import the packages that correspond to the OSGi service in the META-INF/MANIFEST.MF file
of the Java agent project.
- Make sure that the correct version is specified. If the
OSGi project is packaged within the solution, the qualifier component
must be preserved in the version restriction.
- Use the
OSGi FrameworkUtil class to get
access to your service. For example:
BundleContext ctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
ServiceReference<org.acme.service.MyService.IMyService>serviceref = null;
Object service = null;
if( ctx != null ) {
serviceref = ctx.getServiceReference("org.acme.service.MyService.IMyService");
if( serviceref != null ) {
service = ctx.getService(serviceref);
}
}
Results
When the service is started, you can retrieve the reference
to the service.