Skip to content

Commit 1c0619a

Browse files
Copilotstbischof
authored andcommitted
Document satisfying condition as OSGi R8 spec-compliant feature
1 parent 57c022a commit 1c0619a

File tree

4 files changed

+71
-5
lines changed

4 files changed

+71
-5
lines changed

scr/README.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ The Apache Felix Service Component Runtime described by the [OSGi Declarative Se
44

55
The Java annotations defined by the specification make implementing components easy and reduce the amount of code that needs be written. These annotations are processed at build time and translated into XML descriptor files which in turn are listed in the `Service-Component` header of the declaring bundle. But the good news is, you usually don't have to worry about this XML, however in case things don't work as expected , it's good to know how these things work.
66

7-
The Apache Felix Declarative Services implementation is the reference implementation for the OSGi Declarative Services Specification Version 1.4 (R7) and therefore passes the OSGi CT.
7+
The Apache Felix Declarative Services implementation is the reference implementation for the OSGi Declarative Services Specification Version 1.4 (R7) and therefore passes the OSGi CT. This implementation also includes support for OSGi R8 features such as the Satisfying Condition specification.
88

99
## Example Usage
1010

@@ -112,6 +112,52 @@ public Comparator(@Reference LogService logService)
112112
}
113113
```
114114

115+
## Satisfying Condition (OSGi R8)
116+
117+
Apache Felix SCR implements the Satisfying Condition feature as specified in the OSGi R8 Declarative Services specification. This feature allows components to be activated only when specific runtime conditions are met.
118+
119+
### How It Works
120+
121+
When the OSGi framework provides a `true` condition service (registered by the system bundle with the property `osgi.condition.id=true`), Apache Felix SCR automatically adds an implicit satisfying condition reference to all components. This implicit reference:
122+
123+
- Has the name `osgi.ds.satisfying.condition`
124+
- References the `org.osgi.service.condition.Condition` service
125+
- Uses a dynamic policy
126+
- Defaults to target `(osgi.condition.id=true)`
127+
128+
### Customizing the Satisfying Condition
129+
130+
Components can customize the satisfying condition target by setting the `osgi.ds.satisfying.condition.target` property:
131+
132+
```xml
133+
<scr:component name="my.component" xmlns:scr="http://www.osgi.org/xmlns/scr/v1.5.0">
134+
<property name="osgi.ds.satisfying.condition.target" value="(my.condition=ready)"/>
135+
<implementation class="com.example.MyComponent"/>
136+
</scr:component>
137+
```
138+
139+
Alternatively, components can explicitly declare the satisfying condition reference to have full control over its configuration:
140+
141+
```xml
142+
<scr:component name="my.component" xmlns:scr="http://www.osgi.org/xmlns/scr/v1.5.0">
143+
<implementation class="com.example.MyComponent"/>
144+
<reference name="osgi.ds.satisfying.condition"
145+
interface="org.osgi.service.condition.Condition"
146+
target="(my.custom.condition=true)"
147+
policy="dynamic"/>
148+
</scr:component>
149+
```
150+
151+
### Use Cases
152+
153+
Satisfying conditions are useful for:
154+
- Delaying component activation until the system is fully initialized
155+
- Implementing conditional component activation based on runtime state
156+
- Managing component lifecycle based on external conditions
157+
158+
For more details, see
159+
- [112.3.13 Satisfying Condition](https://docs.osgi.org/specification/osgi.cmpn/8.0.0/service.component.html#service.component-satisfying.condition)
160+
115161
## Apache Maven Support
116162

117163
Both, the [maven-bundle-plugin](http://felix.apache.org/documentation/subprojects/apache-felix-maven-bundle-plugin-bnd.html) as well as the [bnd-maven-plugin](https://github.com/bndtools/bnd/tree/master/maven) supports processing the annotations and creating the XML component descriptors.

scr/changelog.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
Changes in 2.2.15
2+
-----------------
3+
** Improvement
4+
* Document Satisfying Condition feature as OSGi R8 specification-compliant
5+
- The existing satisfying condition implementation is now documented as compliant
6+
with the preliminary OSGi R8 Declarative Services specification changes
7+
- See https://docs.osgi.org/specification/osgi.cmpn/8.0.0/service.component.html#service.component-satisfying.condition
8+
- Added comprehensive documentation in README.md
9+
- Updated code comments to reflect specification compliance
10+
111
Changes in 2.2.14
212
-----------------
313
** PRs

scr/src/main/java/org/apache/felix/scr/impl/metadata/ReferenceMetadata.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ public enum ReferenceScope {bundle, prototype, prototype_required}
101101

102102
public static final String CONDITION_TRUE_FILTER = "(osgi.condition.id=true)";
103103

104-
// TODO this constant will be defined in the R8 Declarative Services spec
104+
/**
105+
* The reference name for the implicit satisfying condition as defined in the
106+
* OSGi R8 Declarative Services specification (see https://github.com/osgi/osgi/pull/875).
107+
* This reference is automatically added to components when a true condition service is available.
108+
*/
105109
public static final String REFERENCE_NAME_SATISFYING_CONDITION = "osgi.ds.satisfying.condition";
106110

107111
// Name for the reference (required)

scr/src/main/java/org/apache/felix/scr/impl/xml/XmlHandler.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,11 @@ else if ( localName.equals( XmlConstants.EL_FACTORY_PROPERTY ) && m_pendingFacto
483483
m_pendingFactoryProperty = null;
484484
}
485485
}
486+
// Add implicit satisfying condition reference as per OSGi R8 Declarative Services specification
487+
// (see https://github.com/osgi/osgi/pull/875 and https://github.com/osgi/osgi/issues/720).
488+
// When a true condition service is available from the framework, an implicit satisfying condition
489+
// reference is automatically added to all components unless they already explicitly declare one.
490+
// This allows components to be activated only when certain runtime conditions are met.
486491
if (m_trueCondition != null && localName.equals(XmlConstants.EL_COMPONENT))
487492
{
488493
boolean missingSatisfyingConditionRef = true;
@@ -504,9 +509,10 @@ else if ( localName.equals( XmlConstants.EL_FACTORY_PROPERTY ) && m_pendingFacto
504509
trueReference.setInterface(ReferenceMetadata.CONDITION_SERVICE_CLASS);
505510
trueReference.setPolicy(ReferenceMetadata.POLICY_DYNAMIC);
506511
m_currentComponent.addDependency(trueReference);
507-
// Here we add the target property for the implicit satisfying condition
508-
// first such that any properties that are specified explicitly can
509-
// be used to override this implicit property
512+
// Add the target property for the implicit satisfying condition.
513+
// This is added first so that any explicitly specified properties can
514+
// override this implicit property, allowing components to specify custom
515+
// condition targets via the osgi.ds.satisfying.condition.target property.
510516
PropertyMetadata prop = new PropertyMetadata(true);
511517
prop.setName(
512518
ReferenceMetadata.REFERENCE_NAME_SATISFYING_CONDITION + ".target");

0 commit comments

Comments
 (0)