The Apache Felix Maven SCR Plugin is a great tool to ease the development of OSGi components and services. Components and services are defined through annotations and the plugin creates the necessary descriptors for the OSGi Declarative Services, Config Admin and Metatype services.
Introduction
In OSGi based systems functionality is mainly provided through services. Unlike traditional systems but comparable to Spring, a service is not reqiured to implement a framework defined interface. Instead services implement one or more interfaces, which stipulate the type of service provided. It is the lifetime of the bundle, which defines the lifetime of the service: A service object may be instantiated when the bundle is started and will automatically be removed when the bundle is stopped (and the service has not already been unregistered).
...
No Format |
---|
<dependency> <groupId>org.apache.felix</groupId> <artifactId>org.apache.felix.scr.annotations</artifactId> <version>1<version>0.09.0</version> </dependency> |
...
Name | Default Value | Required | SCR | Metatype | Description |
name | Fully qualified name of the Java class | no | | | Defines the Component name also used as the PID for the Configuration Admin Service |
ds | | no | Whether Declarative Services descriptor is generated or not. If this parameter is not set or set to | ||
componentAbstract | see description | no | This marks an abstract service description which is not added to the descriptor but intended for reuse through inheritance. This attribute defaults to | ||
enabled | | no | | Whether the component is enabled when the bundle starts | |
factory | no | | Whether the component is a factory component | ||
immediate | no | | Whether the component is immediately activated | ||
inherit | | no | Whether any service, property and reference declarations from base classes should be inherited by this class. | ||
metatype | | no | Whether Metatype Service data is generated or not. If this parameter is not set or set to | ||
label | | no | | This is generally used as a title for the object described by the meta type. This name may be localized by prepending a | |
description | | no | | This is generally used as a description for the object described by the meta type. This name may be localized by prepending a | |
createPid | | no | | Generate the |
Abstract Service Descriptions
...
Name | Default Value | Required | SCR | Metatype | Description | ||||||||||||||||||||||||||||||||||||||
name | The name of constant | yes | | | The name of the property. If this tag is defined on a field with an initialization expression, the value of that expression is used as the name if the field is of type | ||||||||||||||||||||||||||||||||||||||
value | no | | | The string value of the property. If the property type is not | |||||||||||||||||||||||||||||||||||||||
longValue | type | | no | | | The type long value of the property value. This must be one of | label | | no | | The label to display in a form to configure this property. This name may be localized by prepending a | description | | no | | A descriptive text to provide the client in a form to configure this property. This name may be localized by prepending a | private | Depending on the name | no | See description | Boolean flag defining whether a metatype descriptor entry should be generated for this property or not. By default a metatype descriptor entry, i.e. an | cardinality | Depends on property value(s) | no | | Defines the cardinality of the property and its collection type. If the cardinality is negative, the property is expected to be stored in a | options | no | See below | See below for a description of the | values* | no | See below | See below for a description of parameters starting with | valueRef | no | | A constant containing the value for this property. The constant can either be declared in the same class as this property or in any class that is imported. The type of the property is derived from the constant. | valueRefs | no | See below | Same as the |
...
can either be a single value or an array. | |||||||
doubleValue | no | | | The double value of the property. This can either be a single value or an array. | |||
floatValue | no | | | The float value of the property. This can either be a single value or an array. | |||
intValue | no | | | The int value of the property. This can either be a single value or an array. | |||
byteValue | no | | | The byte value of the property. This can either be a single value or an array. | |||
charValue | no | | | The char value of the property. This can either be a single value or an array. | |||
boolValue | no | | | The boolean value of the property. This can either be a single value or an array. | |||
shortValue | no | | | The short value of the property. This can either be a single value or an array. | |||
label | | no | | The label to display in a form to configure this property. This name may be localized by prepending a | |||
description | | no | | A descriptive text to provide the client in a form to configure this property. This name may be localized by prepending a | |||
propertyPrivate | Depending on the name | no | See description | Boolean flag defining whether a metatype descriptor entry should be generated for this property or not. By default a metatype descriptor entry, i.e. an | |||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="bb0c247e-efb7-4cd0-8c76-58fd054dce34"><ac:plain-text-body><![CDATA[ | cardinality | Depends on property value(s) | no | | Defines the cardinality of the property and its collection type. If the cardinality is negative, the property is expected to be stored in a | ]]></ac:plain-text-body></ac:structured-macro> | |
options | no | See below | See below for a description of the |
- Generating
<properties>
elements referring to bundle entries is not currently supported. - Multiple property annotations can be embedded in the
Properties
annoation.
Naming the Property
It is important to carefully define the name of properties. By using a constant of the form
No Format |
---|
/** @scr.property @Property(value="default value" */) static final String CONSTANT_NAME = "property.name"; |
and defining the scr.property
tag Property
annotation on this constant, the name of the property is taken from the constant value. Thus it may easily be ensured, that both the property in the descriptor files and the property used by the implementation are actually the same. In addition the value attribute can point to another constant.
The options
Parameter
Some properties may only be set to a set of possible values. To support user interfaces which provide a selection list of values or a list of checkboxes the option values and labels may be defined as parameters to the scr.property
tag. All parameters in the form of name-value pairs occurring after the options
parameter are used to build the list of available value options. The parameter name is used as the value while the parameter value is used as the label in the user interface. This label may be prepended with a %
sign to localize the string.
...
Name | Default Value | Required | SCR | Metatype | Description |
name | Fully qualified name of the Java class | no | | | Defines the Component name also used as the PID for the Configuration Admin Service |
ds | | no | Whether Declarative Services descriptor is generated or not. If this parameter is not set or set to | ||
abstract | see description | no | This marks an abstract service description which is not added to the descriptor but intended for reuse through inheritance. This attribute defaults to | ||
enabled | | no | | Whether the component is enabled when the bundle starts | |
factory | no | | Whether the component is a factory component | ||
immediate | no | | Whether the component is immediately activated | ||
inherit | | no | Whether any service, property and reference declarations from base classes should be inherited by this class. | ||
metatype | | no | Whether Metatype Service data is generated or not. If this parameter is not set or set to | ||
label | | no | | This is generally used as a title for the object described by the meta type. This name may be localized by prepending a | |
description | | no | | This is generally used as a description for the object described by the meta type. This name may be localized by prepending a | |
create-pid | | no | | Generate the |
Abstract Service Descriptions
...
Name | Default Value | Required | SCR | Metatype | Description | ||
name | The name of constant | yes | | | The name of the property. If this tag is defined on a field with an initialization expression, the value of that expression is used as the name if the field is of type | ||
value | no | | | The value of the property. If the property type is not | |||
type | | no | | | The type of the property value. This must be one of | ||
label | | no | | The label to display in a form to configure this property. This name may be localized by prepending a | |||
description | | no | | A descriptive text to provide the client in a form to configure this property. This name may be localized by prepending a | |||
private | Depending on the name | no | See description | Boolean flag defining whether a metatype descriptor entry should be generated for this property or not. By default a metatype descriptor entry, i.e. an | |||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="fd4c70cf-bea9-4da5-a4ef-37260f28f34e"><ac:plain-text-body><![CDATA[ | cardinality | Depends on property value(s) | no | | Defines the cardinality of the property and its collection type. If the cardinality is negative, the property is expected to be stored in a | ]]></ac:plain-text-body></ac:structured-macro> | |
options | no | See below | See below for a description of the | ||||
values* | no | See below | See below for a description of parameters starting with | ||||
valueRef | no | | A constant containing the value for this property. The constant can either be declared in the same class as this property or in any class that is imported. The type of the property is derived from the constant. | ||||
valueRefs | no | See below | Same as the |
...
Name | Default Value | Required | Descriptor | Description |
name | Name of the field | yes | | The local name of the reference. If the |
interface | Type of the field | yes | | The name of the service interface. This name is used by the Service Component Runtime to access the service on behalf of the component. If the |
cardinality | | no | | The cardinality of the service reference. This must be one of |
policy | | no | | The dynamicity policy of the reference. If |
target | no | | A service target filter to select specific services to be made available. In order to be able to overwrite the value of this value by a configuration property, this parameter must be declared. If the parameter is not declared, the respective declaration attribute will not be generated the parameter is not declared, the respective declaration attribute will not be generated | |
bind | See description | no | | The name of the method to be called when the service is to be bound to the component. The default value is the name created by appending the reference |
unbind bind | See description | no | | The name of the method to be called when the service is to be bound to unbound from the component. The default value is the name created by appending the reference |
Notes:
- If you define a reference on a field and there is no bind or unbind
...
See description
...
no
...
reference.unbind
...
The name of the method to be called when the service is to be unbound from the component. The default value is the name created by appending the reference name
to the string unbind
. The method must be declared public
or protected
and take single argument which is declared with the service interface type
- method, the plugin will create the necessary methods.
.h2 Differences between JavaDoc tags and annotations
In general both mechanisms provide the same functionality. There are some subtle difference which are listed in this section:
- While the
metatype
flag is turned on by default for the JavaDoc tags, the default for the annotations is to generate no metadata. The reason for this is, that it turned out that services with metadata are less often used. - The JavaDoc support adds properties and references from super classes if the source is in the same module to a component even if the super class does not have the
@scr.component
tag. With the annotations the super class is required to have theComponent
annotation. - Property values are handled differently. While the JavaDoc version has an auto detection of types together with an explicit type parameter, the annotations version has several attributes. Each type has its own attribute (like
shortValue
,intValue
and so on). This is because of a limitation in the Java annotations which only allow typed parameters
Notes:
- If you define a reference on a field and there is no bind or unbind method, the plugin will create the necessary methods.
.h2 Differences between JavaDoc tags and annotations
In general both mechanisms provide the same functionality. There are some subtle difference which are listed in this section:
- While the
metatype
flag is turned on by default for the JavaDoc tags, the default for the annotations is to generate no metadata. The reason for this is, that it turned out that services with metadata are less often used.