How to register an OSGI service
Join the DZone community and get the full member experience.
Join For Free
For this , I will use a code segment from wso2 appfactory.
The necessary code should be written inside the activate method which usually resides inside 'internal' package
protected void activate(ComponentContext context) { BundleContext bundleContext = context.getBundleContext(); try { MavenBuildDriver driver = new MavenBuildDriver(); bundleContext.registerService(BuildDriver.class.getName(), driver, null); if (log.isDebugEnabled()) { log.debug("Appfactory common bundle is activated"); } } catch (Throwable e) { log.error("Error in creating appfactory configuration", e); } }
In the above code BuildDriver is the interface.
the driver is an object of the class which implements the BuildDriver interface.
The content inside internal is not exposed. refer the pom.xml
<Private-Package> org.wso2.carbon.appfactory.maven.build.internal.*, org.wso2.carbon.appfactory.jenkins.build.internal.* </Private-Package>
Published at DZone with permission of Achala Chathuranga Aponso, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments