import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import com.example.whiteboard.WhiteboardService;
public class WhiteboardProvider implements BundleActivator {
private ServiceRegistration<WhiteboardService> registration;
public void start(BundleContext bundleContext) throws Exception {
WhiteboardService service = new WhiteboardServiceImpl();
registration = bundleContext.registerService(WhiteboardService.class, service, null);
}
public void stop(BundleContext bundleContext) throws Exception {
registration.unregister();
}
}
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import com.example.whiteboard.WhiteboardService;
public class WhiteboardConsumer implements BundleActivator {
private ServiceReference<WhiteboardService> reference;
private WhiteboardService service;
public void start(BundleContext bundleContext) throws Exception {
reference = bundleContext.getServiceReference(WhiteboardService.class);
service = bundleContext.getService(reference);
service.draw("Hello, World!");
}
public void stop(BundleContext bundleContext) throws Exception {
bundleContext.ungetService(reference);
}
}
Bundle-Activator: com.example.provider.WhiteboardProvider
Export-Package: com.example.whiteboard
groovy
plugins {
id 'java'
id 'eclipse'
id 'groovy'
id 'osgi-bundle'
}
dependencies {
implementation 'org.osgi:org.osgi.core:7.0.0'
implementation 'org.apache.felix:org.apache.felix.framework:7.0.1'
}
osgi {
bundle {
instructions 'Bundle-Name': 'Whiteboard Provider',
'Bundle-SymbolicName': 'com.example.provider',
'Bundle-Version': '1.0.0',
'Export-Package': 'com.example.whiteboard',
'Bundle-Activator': 'com.example.provider.WhiteboardProvider'
}
}