The OSGi Configuration Admin Service (Config Admin) is a dynamic configuration management service in OSGi that allows bundles to retrieve and update configuration data at runtime. It provides a way to separate configuration from code, making applications more flexible and maintainable.
? Centralized Configuration Management – Manages configuration for multiple bundles.
? Dynamic Updates – Configurations can be updated at runtime without restarting bundles.
? Persistence Support – Configurations can be stored persistently and reloaded.
? Decoupling of Configuration & Code – Keeps application logic separate from its configuration.
? Multi-Bundle Configuration – Allows different bundles to share configuration settings.
The Config Admin Service stores and manages configurations as key-value pairs. Bundles that need configuration can retrieve these settings or listen for updates dynamically.
Configurations are typically stored in a .cfg
or .config
file.
Example (com.example.myconfig.cfg
):
my.property.name=Hello OSGi!
my.property.value=42
A bundle can retrieve configuration dynamically using Declarative Services (DS).
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Modified;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.service.component.annotations.Property;
import org.osgi.service.metatype.annotations.Designate;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.cm.ConfigurationAdmin;
import java.util.Map;
@Component(
immediate = true,
configurationPolicy = ConfigurationPolicy.REQUIRE
)
@Designate(ocd = MyConfig.class)
public class MyService {
private String propertyValue;
@Activate
@Modified
protected void activate(MyConfig config) {
this.propertyValue = config.myPropertyName();
System.out.println("Updated Configuration: " + propertyValue);
}
}
The configuration is mapped to an OSGi metatype interface.
import org.osgi.service.metatype.annotations.ObjectClassDefinition;
import org.osgi.service.metatype.annotations.AttributeDefinition;
@ObjectClassDefinition(name = "My Configuration")
public @interface MyConfig {
@AttributeDefinition(name = "Property Name")
String myPropertyName() default "Default Value";
}
Feature | Benefit |
---|---|
Centralized Config Management | Keeps configuration in one place. |
Dynamic Updates | Allows changes without restarting bundles. |
Persistence Support | Stores configuration even after a restart. |
Decoupled Logic | Keeps business logic independent from configuration. |
Multi-Bundle Support | Shares configuration across multiple bundles. |