{"id":17700,"date":"2015-02-28T11:25:37","date_gmt":"2015-02-28T05:55:37","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=17700"},"modified":"2016-08-30T11:01:23","modified_gmt":"2016-08-30T05:31:23","slug":"creating-osgi-factory-configurations-in-aem","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/creating-osgi-factory-configurations-in-aem\/","title":{"rendered":"Creating OSGI Factory Configurations in AEM"},"content":{"rendered":"<p><a title=\"AEM Development\" href=\"http:\/\/www.tothenew.com\/wcm\/cq-aem-development-consulting\">AEM<\/a> houses a powerful open source framework in its technology\u00a0stack,<b><i>Apache Felix<\/i><\/b>. Felix is a open source implementation of OSGi. OSGi provides a way to manage bundles and their configurations.<\/p>\n<p>OSGi provides a way to configure services and modify those configurations on the run-time. But apart from this there is a another powerful feature that OSGi provides that is : ability to create <i>Factory Configurations. Factory Configurations <\/i>is a way to create a single service, and bind multiple configurations to it\u00a0\u00a0&amp; then consume those configurations from different classes\/Services. You all might have used Logger service provided by CQ out of the box, logger is an example of factory configuration service.<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/02\/ig_blog.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-17704\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/02\/ig_blog.png\" alt=\"ig_blog_CQ_FactoryConfig\" width=\"1158\" height=\"351\" \/><\/a><\/p>\n<p>Here are the steps for how to create factory configuration in CQ.<\/p>\n<p><i>Step 1:<\/i><\/p>\n<p>Firstly create a Component\/Service that\u00a0holds configurations values. This component will act a interface for adding properties of multiple configurations. Key for creating this\u00a0Component\/Service class is that declare this class as a <b><i>configurationFactory<\/i><\/b> and pass configuration policy as <i><b>required<\/b>. Here is the sample code :<\/i><\/p>\n<pre><code>@Component(label = \"Factory configuration\", immediate = true, enabled = true, \r\nmetatype = true, \r\ndescription = \"This is factory configuration which acts as a interface for allowing user to enter property values\",\r\npolicy = ConfigurationPolicy.REQUIRE, configurationFactory = true)\r\n@Property(name = \"dummy.prop\", label = \"Dummy property\", description = \"This is just dummy property\", value = \"Dummy Value\")\r\n public class FactoryConfig { \u00a0 \u00a0 \u00a0\r\n \u00a0 \u00a0 \u00a0private static final Logger LOGGER = LoggerFactory.getLogger(FactoryConfig.class); \u00a0 \u00a0 \u00a0\r\n\r\n \u00a0 \u00a0 \u00a0@Activate \u00a0 \u00a0 \u00a0\r\n \u00a0 \u00a0 \u00a0public void activate(ComponentContext componentContext) { \u00a0 \u00a0 \u00a0\u00a0 \u00a0 \u00a0\r\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0Dictionary properties = componentContext.getProperties(); \u00a0 \u00a0 \u00a0\u00a0 \r\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0String dummyProperty = PropertiesUtil.toString(properties.get(\"dummy.prop\"), \"\");\r\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0LOGGER.info(\"Read the dummy property value : \" + dummyProperty);\r\n \u00a0 \u00a0 \u00a0}\r\n<\/code><\/pre>\n<p><i>Step 2:<\/i><\/p>\n<p>Now that you have created the interface that takes input from user, create a service that actually uses the FactoryConfig Component\/Service to add configuration and accept values of properties. For creating this service we will use\u00a0<b>dynamic binding <\/b>of OSGi. We will have to create a collection of FactoryConfig class and <b><i>binding &amp; unbinding<\/i><\/b> methods are also required that will add or remove each FactoryConfig object in\/from the collections.<\/p>\n<pre><code>@Component(label = 'Factory Congifuration', description = \"\", immediate = true, metatype = true, enabled = true)\r\n@Service(FactoryConfigConsumer.class)\r\npublic class FactoryConfigConsumer {\r\n\r\n@Reference(referenceInterface = FactoryConfig.class, \r\ncardinality = ReferenceCardinality.OPTIONAL_MULTIPLE,\r\npolicy = ReferencePolicy.DYNAMIC, name = \"Code Brains Demo Factory configurations\")\r\nprivate List factoryConfigs;\r\n\r\n\u00a0 \u00a0 \u00a0protected synchronized void bindFactoryConfig(final FactoryConfig config) {\r\n\u00a0 \u00a0 \u00a0\u00a0 \u00a0 \u00a0if (factoryConfigs == null) {\r\n\u00a0 \u00a0 \u00a0\u00a0 \u00a0 \u00a0\u00a0 \u00a0 \u00a0factoryConfigs = new ArrayList&lt;FactoryConfig&gt;();\r\n\u00a0 \u00a0 \u00a0\u00a0 \u00a0 \u00a0}\r\n\r\n\u00a0 \u00a0 \u00a0\u00a0 \u00a0 \u00a0factoryConfigs.add(config);\r\n\u00a0 \u00a0 \u00a0}\r\n\r\n\u00a0 \u00a0 \u00a0protected synchronized void unbindFactoryConfig(final FactoryConfig config) {\r\n\u00a0 \u00a0 \u00a0\u00a0 \u00a0 \u00a0factoryConfigs.remove(config);\r\n\u00a0 \u00a0 \u00a0}\r\n}<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>AEM houses a powerful open source framework in its technology\u00a0stack,Apache Felix. Felix is a open source implementation of OSGi. OSGi provides a way to manage bundles and their configurations. OSGi provides a way to configure services and modify those configurations on the run-time. But apart from this there is a another powerful feature that OSGi [&hellip;]<\/p>\n","protected":false},"author":111,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":376},"categories":[1],"tags":[1235,4847,1662,1659,1660,1663,4850,1661],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/17700"}],"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\/111"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=17700"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/17700\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=17700"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=17700"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=17700"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}