OSGI R6 Configuration @AttributeDefinition Essentials Reference Guide

When defining any OSGI component’s configuration its standard practice to use Declarative Services; these configurations can be edited in the OSGI Apache Felix Console. This can be done by creating a class interface with the annotated @ObjectClassDefinition, which includes a determined list of rules as @AttributeDefinition items.

The @AttributeDefinition defines the supporting configuration for a given OSGI component’s configuration. It allows you to set data types and default values for each given configuration. When an admin user interacts with the Felix console, how the @AttributeDefinition is defined will determine the look and feel of each of the input fields are presented.


@AttributeDefinition Essential Parameters Explained

As we know, the most basic way to define a configuration will be to utilise the annotation @AttributeDefinition. Let’s explore the essential configurations.

The @AttributeDefinition annotation accepts parameters as such:

  • line 2: name: defines the human read-able label
  • line 3: description: description for the attribute configuration.
  • line 4: type: definition of the attribute’s data type.
  •           line 6: <T>: definition of the attribute’s data type
  •           line 6: name_of_conf_reference: configuration’s reference id : name.of.conf.reference
  •           line 7: <T>[]: multifield (multi data set) is supported.
    1
    2
    3
    4
    5
    6
    7
    @AttributeDefinition(
    name=""
    description=""
    type=""
    }
    name_of_conf_reference() default "";
    // [] name_of_conf_reference_list() default "";

    Explained:
    Illustrating how the label, description, and the configuration reference shows up on the OSGI Apache Felix console.
    OSGI Apache Felix Console - visual description of whats being reflected by the code.


    Attribute Types

    With the understanding of how the @AttributeDefinition Essential Parameters are used, we can go ahead to utilise and to familiarize ourselves with the supported essential data attribute types; most of the basic attributes are covered below:

    1. String Attribute Type
    2. String[] Attribute Type
    3. Long Attribute Type
    4. int Attribute Type
    5. Short Attribute Type
    6. Char Attribute Type
    7. byte Attribute Type
    8. Float Attribute Type
    9. Double Attribute Type
    10. Boolean Attribute Type
    11. Password Attribute Type
    12. Options Attribute Type

     

    1. String Attribute Type

    1
    2
    3
    4
    5
    @AttributeDefinition(
    name = "String Label",
    description = "String Config Example Description",
    type = AttributeType.STRING)
    String config_string_example() default "Default String";

    2. String[] Attribute Type

    1
    2
    3
    4
    5
    @AttributeDefinition(
    name = "String[] Label",
    description = "String[] Config Example Description",
    type = AttributeType.STRING)
    String[] config_string_array_example() default {"item1", "item2"};
    Explained:
    Illustrating how the multi-fields shows up on the OSGI Felix console.
    OSGI Apache Felix Console - Multi-Field Highlight Example
    Note:
    Configuration can be set as multifield can be enabled without much effort, simply change the attribute type to an list type, shown in the #2. String[] Attribute Type example.

    3. Long Attribute Type

    1
    2
    3
    4
    5
    @AttributeDefinition(
    name = "Long Label",
    description = "Long Config Example Description",
    type = AttributeType.LONG)
    long config_long_example() default 0L;

    4. int Attribute Type

    1
    2
    3
    4
    5
    @AttributeDefinition(
    name = "int Label",
    description = "innt Config Example Description",
    type = AttributeType.INTEGER)
    int config_number_example() default 0;

    5. Short Attribute Type

    1
    2
    3
    4
    5
    @AttributeDefinition(
    name = "Short Label",
    description = "Short Config Example Description",
    type = AttributeType.SHORT)
    short config_short_example() default 0;

    6. Char Attribute Type

    1
    2
    3
    4
    5
    @AttributeDefinition(
    name = "Char Label",
    description = "Char Config Example Description",
    type = AttributeType.CHARACTER)
    char config_char_example() default 0;

    7. Byte Attribute Type

    1
    2
    3
    4
    5
    @AttributeDefinition(
    name = "Byte Label",
    description = "Byte Config Example Description",
    type = AttributeType.BYTE)
    byte config_byte_example() default 0;
    See Code Examples!
    Click here to view OSGI R6 Configuration Code Examples.

    8. Double Attribute Type

    1
    2
    3
    4
    5
    @AttributeDefinition(
    name = "Double Label",
    description = "Double Config Example Description",
    type = AttributeType.DOUBLE)
    double config_double_example() default 0;

    9. Float Attribute Type

    1
    2
    3
    4
    5
    @AttributeDefinition(
    name = "Float Label",
    description = "Float Config Example Description",
    type = AttributeType.FLOAT)
    float config_float_example() default 0;

    10. Boolean Attribute Type

    1
    2
    3
    4
    5
    @AttributeDefinition(
    name = "Boolean Label",
    description = "Boolean Config Example Description",
    type = AttributeType.BOOLEAN)
    boolean config_boolean_example() default true;

    11. Password Attribute Type

    1
    2
    3
    4
    5
    @AttributeDefinition(
    name = "Password Label",
    description = "Password Config Example Description",
    type = AttributeType.PASSWORD)
    String config_password_config_example() default "";

    12. Options Attribute Type

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    @AttributeDefinition(
    name = "Dropdown Label",
    description = "Dropdown Config Example Description",
    options = {
    @Option(label = "PRODUCTION", value = "PRODUCTION"),
    @Option(label = "STAGING", value = "STAGING"),
    @Option(label = "UAT", value = "UAT"),
    @Option(label = "QA", value = "QA"),
    @Option(label = "DEVELOP", value = "DEVELOP")
    }
    )
    String config_dropdown_example() default "DEVELOP";

    See Code Examples!
    Click here to view OSGI R6 Configuration Code Examples.

Hello, I am an enthusiastic Adobe Community Advisor and a seasoned Lead AEM Developer. I am currently serving as an AEM Technical Lead at MNPDigital.ca, bringing over a decade of extensive web engineering experience and more than eight years of practical AEM experience to the table. My goal is to give back to the AEM Full Stack Development community by sharing my wealth of knowledge with others. You can connect with me on LinkedIn.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back To Top