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:
Create a Component/Service class to hold 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.
Step 2:
Create a service to that actually uses the FactoryConfig Component/Service as a interface to allow user 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.
Here is the link to code repository for this service : OSGi Factory Config
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.
![]() |
Factory Configuration |
Here are the steps for how to create factory configuration in CQ.
Step 1:
Create a Component/Service class to hold 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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:
Create a service to that actually uses the FactoryConfig Component/Service as a interface to allow user 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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<FactoryConfig> 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); | |
} | |
} |
Here is the link to code repository for this service : OSGi Factory Config
No comments:
Post a Comment