OSGi provides a way to configure services and modify those configurations on the run-time. But apart from this, there is another powerful feature that OSGi provides that is: the 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, the logger is an example of a 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 as an interface for adding properties of multiple configurations. The key to creating this Component/Service class is that declare this class as a configurationFactory and pass configuration policy as required. Here is the sample code :
Firstly create a Component/Service that holds configurations values. This component will act as an interface for adding properties of multiple configurations. The key to 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 the 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 the 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);
}
}
No comments:
Post a Comment
If you have any doubts or questions, please let us know.