This module is part of the Apache Sling project.
This module creates proxy OSGi services that are registered only if specific resources are present.
Like for example:
@Reference(target="(path=/content/foo/bar)")
private ResourcePresence barIsPresent;
The barIsPresent service is registered only if the /content/foo/bar resource is present, allowing OSGi
components to be dependent on the presence of specific resources.
This is mostly meant for testing, to wait for test content before running specific tests.
-
Configure a service user mapping for
org.apache.sling.resource.presenceto allow reading resources, using e.g. service usersling-readall. -
Configure a presenter to observe a resource by path, e.g.
path=/appsWhenever resource
/appsis available the presenter will register an OSGi service for it and unregister it whenever/appsgets removed. -
You can depend on that service now, e.g. using a
@Referenceannotation with atargeton your component:@Reference( target = "(path=/apps)" ) private ResourcePresence apps;
When running tests with resources involved, you can use a resource presence to delay test execution until required resources are available.
@Inject
@Filter(value = "(path=/apps)")
private ResourcePresence apps;
@Configuration
public Option[] configuration() {
return options(
[...],
factoryConfiguration("org.apache.sling.resource.presence.internal.ResourcePresenter")
.put("path", "/apps")
.asOption()
);
}
@Test
public void testApps() {
assertThat(apps.getPath(), is("/apps"));
}
