Creating OSGI Factory Configurations in AEM
AEM houses a powerful open source framework in its technology stack,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 provides that is : ability to create Factory Configurations. Factory Configurations is a way to create a single service, and bind multiple configurations to it & 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.
Here are the steps for how to create factory configuration in CQ.
Step 1:
Firstly create a Component/Service that holds configurations values. This component will act a interface for adding properties of multiple configurations. Key for creating this Component/Service class is that declare this class as a configurationFactory and pass configuration policy as required. Here is the sample code :
@Component(label = "Factory configuration", immediate = true, enabled = true,
metatype = true,
description = "This is factory configuration which acts as a interface for allowing user to enter property values",
policy = ConfigurationPolicy.REQUIRE, configurationFactory = true)
@Property(name = "dummy.prop", label = "Dummy property", description = "This is just dummy property", value = "Dummy Value")
public class FactoryConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(FactoryConfig.class);
@Activate
public void activate(ComponentContext componentContext) {
Dictionary properties = componentContext.getProperties();
String dummyProperty = PropertiesUtil.toString(properties.get("dummy.prop"), "");
LOGGER.info("Read the dummy property value : " + dummyProperty);
}
Step 2:
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 dynamic binding of OSGi. We will have to create a collection of FactoryConfig class and binding & unbinding methods are also required that will add or remove each FactoryConfig object in/from the collections.
@Component(label = 'Factory Congifuration', description = "", immediate = true, metatype = true, enabled = true)
@Service(FactoryConfigConsumer.class)
public class FactoryConfigConsumer {
@Reference(referenceInterface = FactoryConfig.class,
cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE,
policy = ReferencePolicy.DYNAMIC, name = "Code Brains Demo Factory configurations")
private List factoryConfigs;
protected synchronized void bindFactoryConfig(final FactoryConfig config) {
if (factoryConfigs == null) {
factoryConfigs = new ArrayList<FactoryConfig>();
}
factoryConfigs.add(config);
}
protected synchronized void unbindFactoryConfig(final FactoryConfig config) {
factoryConfigs.remove(config);
}
}
Pingback: 9A0-384 : Adobe Experience manager certification Preparation Useful links – Adobe Docker
Pingback: 9A0-384 : Adobe Experience manager certification preparation Notes – AEM Docs