In AEM (Adobe Experience Manager), OSGi (Open Service Gateway Initiative) configurations can be created in a separate file and loaded into the system at runtime.
Here’s how to create an OSGi configuration in a separate file:
1. Create a configuration file
Create a new file with the extension .cfg
in the directory /apps/myapp/config/
. For example, create a file named com.example.myapp.cfg
.
2. Define the configuration properties
In the configuration file, define the configuration properties using the following format: property=value
. For example:
myProperty=myValue
3. Place the configuration file in the AEM package
Place the configuration file in the AEM package along with other files that make up the application. For example, place the configuration file in /apps/myapp/config/com.example.myapp.cfg
.
4. Include the configuration file in the OSGi bundle
In the bundle
element of the pom.xml
file for the OSGi bundle, add a reference to the configuration file in the Embed-Dependency
section. For example:
<Embed-Dependency> com.example.myapp:myapp-core, com.example.myapp:myapp-ui, com.example.myapp:myapp-config </Embed-Dependency>
5. Load the configuration file at runtime
In your OSGi component, use the @Designate
annotation to specify the configuration file that should be loaded at runtime. For example:
package com.example.service.impl; import org.osgi.service.cm.ConfigurationException; import org.osgi.service.cm.ManagedService; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; import org.osgi.service.metatype.annotations.Designate; @Component(service = ManagedService.class) @Designate(ocd = MyServiceConfiguration.class, factory = true) public class MyService implements ManagedService { private String myProperty; @Override public void updated(Dictionary<String, ?> properties) throws ConfigurationException { if (properties != null) { myProperty = (String) properties.get("myProperty"); } } public String getMyProperty() { return myProperty; } @Reference private ConfigurationAdmin configAdmin; }
In this example, we use the @Designate
annotation to specify the MyServiceConfiguration
class as the configuration definition for the MyService
OSGi component. The factory
attribute is set to true
to indicate that this is a factory configuration. We also implement the ManagedService
interface and override the updated
method to receive the configuration properties at runtime.
6. Define the configuration definition
Create a configuration definition class that defines the configuration properties. For example:
package com.example.service.impl; import org.osgi.service.metatype.annotations.AttributeDefinition; import org.osgi.service.metatype.annotations.ObjectClassDefinition; @ObjectClassDefinition(name = "My Service Configuration", description = "Configuration for My Service") public @interface MyServiceConfiguration { @AttributeDefinition(name = "My Property", description = "A custom property") String myProperty() default "default value"; }
In this example, we use the @ObjectClassDefinition
annotation to define the configuration definition for the MyService
OSGi component. The name
and description
attributes provide metadata for the configuration definition. We also define a single configuration property using the @AttributeDefinition
annotation.
That’s it! You have now created an OSGi configuration in a separate file in AEM, and loaded it at runtime using the @Designate
annotation and the OSGi Configuration API.