How to write a custom grant type as a OSGi Service by embedding other third party libraries.

WSO2 Identity Server is running on a Carbon Platform which is a collection of totally independent components which can be added or removed from a solution dynamically and which is written by WSO2 team. This is achieved through the use of Open Services Gateway Initiative(OSGi) framework. In OSGi, these independent components are called bundles. For further details on Carbon Platform and bundling you can read [1].

Currently, in our WSO2 Documentation [2], we have provided steps to write a custom grant-type as a pure java code which packages as a jar, instead of a bundle. This has lot of limitations if we want to use this grant-type dynamically. Therefore, it is a good practice to bundle this custom grant-type as an OSGi component instead of jar.

You can follow up the same step in [2] to write the custom grant type. Next do a small change in the pom.xml in order to bundle it as a OSGi bundle.

  • Use the packaging type as bundle.
<packaging>bundle</packaging>
  • Use maven-bundle-plugin to do bundling as a plugin. Remember to put necessary import-packages and export-packages. If any dyamic-imports put them as well.
<plugin>
<groupId>
org.apache.felix
</groupId>
<artifactId>
maven-bundle-plugin
</artifactId>
<extensions>
true
</extensions>
<version>
1.4.0
</version>
<configuration>
<instructions>
<Bundle-SymbolicName>
${project.artifactId}
</Bundle-SymbolicName>
<Bundle-Name>
${project.artifactId}
</Bundle-Name>
<Import-Package>
org.apache.commons.logging;version="[1.2,2)",
org.wso2.carbon.identity.core.util;version="[5.7.0,6)",
org.apache.commons.lang; version="2.6.0",
org.wso2.carbon.identity.oauth2.*;version="[5.3.0,6)",
org.wso2.carbon.user.core; version="4.4.11"
</Import-Package>
<Export-Package>
org.wso2.sample.identity.oauth2.grant.mobile;version="1.0.0",
org.wso2.sample.identity.oauth2.grant.password;version="1.0.0"
</Export-Package>
<DynamicImport-Package>
*
</DynamicImport-Package>
</instructions>
</configuration>
</plugin>
  • If you need to bundle other third party dependencies that is used inside your java class, use them as Embed-Dependency in your pom.xml.
<Embed-Dependency> *;scope=compile|runtime;inline=true;artifactId=jackson-core|jackson-databind|jackson-databind|jackson-annotations|
jackson-core-asl|org.codehaus.jackson|spring-core|spring-beans|spring-context|spring-web
</Embed-Dependency>

<Embed-Transitive>true</Embed-Transitive>

Dinali Rosemin Dabarera

Written by

A Software Engineer @WSO2 which is a leading middleware company in the world, an Identity Evangelist, a blogger, a nature lover, a traveler and a social worker

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade