An component is a piece of code that is managed by OSGi container. The container would be responsible for its instantiation and management.
Attributes of a component:
The above table have been noted from: https://www.knopflerfish.org/releases/5.2.0/docs/javadoc/org/osgi/service/component/annotations/Component.html
- configurationPolicy: The attribute can hold following values of ConfigurationPolicy
- IGNORE: Always allow the component configuration to be satisfied and do not use the corresponding Configuration object even if it is present.
- OPTIONAL: Use the corresponding Configuration object if present but allow the component to be satisfied even if the corresponding Configuration object is not present.
- REQUIRE: There must be a corresponding Configuration object for the component configuration to become satisfied. A component is activated only after all its service dependencies are satisfied by the container.
- factory: used to create a configuration factory. More implementation details are available on link.
- name: The attribute doesn’t support special characters. If invalid, the component will not be registered.
- property: A replacement for “@Properties felix annotation used at Class-level”.
- service: registers component as a Service. More implementation details available on link.
To create a component, add @Component annotation to a class. Also, configure its attributes as per your need.
In the following example, we have:
- @Activate annotation: It is used to mark a function which would be called when the component activates. The function can have any name.
- @Deactivate annotation: It is used to mark a function which would be called when the component deactivates. The function can have any name.
- Declared a custom property using property attribute.
More on defining property:
The property annotations have moved to their own class which declutters the component or service. For components with a large amount of properties, create an independent class, while a component with only one or two properties may be fine as a subclass.
A property value is read from componentContext in @Activate/@Deactivate methods.
If you need multiple properties for a component, declare them as an indiviadual value array of values for property attribute:
To define multiple values of a property, create each value as a separate element of the property Array.
Creating configurable properties
The annotations described in this section will help you to create Components whose configurations can be edited via OSGi console. To achieve the same:
- Create a separate or an inner interface which would hold configurations. In example, we have created Config interface.
- Add @ObjectClassDefinition annotation to the interface. Also, add desired attributes
- name: The name would help you search the configuration in OSGi’s configuration manager.
- Add @Designate to the Component that would consume the configurations. The ocd attribute should refer to the Configuration interface created in Step-2.
- Declare properties that you would like to configure via @AttributeDefinition
- To define property names similar to
blog.name, your method would be named
- Following image maps annotation attributes with the OSGi UI.
- Please note that there are 2 ways to define default values:
- defaultValue attribute of @AttributeDefinition: The value is displayed to the user, when he/she tries to configure the interface via Configuration manager. OSGi will NOT pick this default value, if no Configuration exists. Thus, when you install a bundle, the output would appear as:
- Specifying default value in variable declarartion: The value is displayed to the user, when he/she tries to configure the interface via Configuration manager. OSGi will pick this default value, even if no Configuration exists.
- To define property names similar to
Also, note that we no longer need PropertiesUtil to resolve OSGi configurations. 🙂
Via Declarative Services, the number of annotations have been reduced. For example: @Component annotation is used for:
- Filter etc..
All of the above can be created by utilizing attributes of @Component details. More details are available on specific links