{"id":54916,"date":"2022-04-18T18:09:01","date_gmt":"2022-04-18T12:39:01","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=54916"},"modified":"2024-06-10T15:40:30","modified_gmt":"2024-06-10T10:10:30","slug":"access-multiple-implementation-of-an-osgi-service-from-a-sling-servlet-dynamically","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/access-multiple-implementation-of-an-osgi-service-from-a-sling-servlet-dynamically\/","title":{"rendered":"Access multiple implementation of an OSGi service from a Sling servlet dynamically"},"content":{"rendered":"<h3><span style=\"font-weight: 400;\">Use Case\u00a0<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Here we are going to showcase how we can access multiple implementations of an OSGI service dynamically (at run time) from a sling servlet.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">Break the requirement<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">To deal with the requirement we are going to break it in multiple requirements &#8211;<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Creating multiple implementations of an OSGi service.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Giving an unique meaningful identifier to the service which will be used to target a specific implementation.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Creating a set of objects which will hold one instance object for each service implementation.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Input and logic to Switch between the implementation.<\/span><\/li>\n<\/ol>\n<h3><span style=\"font-weight: 400;\">Creating multiple implementations\u00a0<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">We can create multiple implementations for a service by implementing our service interface, same way as we create a single implementation of a service.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">Giving an unique meaningful identifier to the Service Implementations<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">To give an identifier to a service we can utilize <\/span><b><i>@Properties<\/i><\/b><span style=\"font-weight: 400;\"> annotation, and set a property name according to your project needs and value will be different for each implementation. Here we are using <\/span><b><i>productType <\/i><\/b><span style=\"font-weight: 400;\">as property name and core is the value for default implementation.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">Creating a Map of objects<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Now that we have created multiple implementations and given some identifier, our next goal is to find the way of getting instance objects created inside the OSGi container and collect them as a Map (to allow fetching particular instance object using identifier).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This can be achieved very easily by utilizing bind and unbind methods which get executed when an object gets created or destroyed respectively.\u00a0 And we can associate a service interface for which we need to track object creation or modification.<\/span><\/p>\n<h4><a href=\"\/blog\/wp-ttn-blog\/uploads\/2022\/04\/reference.png\"><br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-54910\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/04\/reference.png\" alt=\"\" width=\"1082\" height=\"258\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/04\/reference.png 1082w, \/blog\/wp-ttn-blog\/uploads\/2022\/04\/reference-300x72.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/04\/reference-1024x244.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/04\/reference-768x183.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/04\/reference-624x149.png 624w\" sizes=\"(max-width: 1082px) 100vw, 1082px\" \/><\/a><\/h4>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2022\/04\/bind.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-54919\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/04\/bind.jpg\" alt=\"\" width=\"927\" height=\"435\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/04\/bind.jpg 927w, \/blog\/wp-ttn-blog\/uploads\/2022\/04\/bind-300x141.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/04\/bind-768x360.jpg 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/04\/bind-624x293.jpg 624w\" sizes=\"(max-width: 927px) 100vw, 927px\" \/><\/a><\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2022\/04\/unbind.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-54906\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/04\/unbind.jpg\" alt=\"\" width=\"1257\" height=\"423\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/04\/unbind.jpg 1257w, \/blog\/wp-ttn-blog\/uploads\/2022\/04\/unbind-300x101.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/04\/unbind-1024x345.jpg 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/04\/unbind-768x258.jpg 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/04\/unbind-624x210.jpg 624w\" sizes=\"(max-width: 1257px) 100vw, 1257px\" \/><\/a><\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Use Case\u00a0 Here we are going to showcase how we can access multiple implementations of an OSGI service dynamically (at run time) from a sling servlet. Break the requirement To deal with the requirement we are going to break it in multiple requirements &#8211; Creating multiple implementations of an OSGi service. Giving an unique meaningful [&hellip;]<\/p>\n","protected":false},"author":1212,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":73},"categories":[5868,446],"tags":[4963],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/54916"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1212"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=54916"}],"version-history":[{"count":5,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/54916\/revisions"}],"predecessor-version":[{"id":54928,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/54916\/revisions\/54928"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=54916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=54916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=54916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}