Apache Sling Resource Presence

Clone this repo:
  1. 9d507b7 set scope of dependency org.apache.sling.testing.paxexam to test by Oliver Lietz · 4 months ago master
  2. 4d26828 SLING-8510 Make testing compliant with Java 9 and higher by Oliver Lietz · 4 months ago
  3. f07c1a4 SLING-8509 Mark fields in ResourcePresenter volatile by Oliver Lietz · 4 months ago
  4. f2782d0 remove default values from bnd file by Oliver Lietz · 1 year, 2 months ago
  5. be96063 Updating badges for org-apache-sling-resource-presence by Radu Cotescu · 9 months ago

Build Status Test Status Maven Central JavaDocs License

Apache Sling Resource Presence

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.

Getting Started

  1. Configure a service user mapping for org.apache.sling.resource.presence to allow reading resources, using e.g. service user sling-readall.

  2. Configure a presenter to observe a resource by path, e.g. path=/apps

    Whenever resource /apps is available the presenter will register an OSGi service for it and unregister it whenever /apps gets removed.

  3. You can depend on that service now, e.g. using a @Reference annotation with a target on your component:

        @Reference(
            target = "(path=/apps)"
        )
        private ResourcePresence apps;
    

Using Resource Presence with Pax Exam

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 new Option[]{
        [...],
        factoryConfiguration("org.apache.sling.resource.presence.internal.ResourcePresenter")
            .put("path", "/apps")
            .asOption()
    };
}

@Test
public void testApps() {
    assertThat(apps.getPath(), is("/apps"));
}