Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to register an OSGI service

DZone's Guide to

How to register an OSGI service

Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

  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>



Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:

Published at DZone with permission of Achala Chathuranga Aponso, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}