Declarative Services

The DeclarativeServicesTestCase shows how a service can be made available through a Declarative Services descriptor.

<component name="sample.component" immediate="true">
  <implementation class="org.jboss.test.osgi.example.ds.SampleComparator" />
  <property name="service.description" value="Sample Comparator Service" />
  <property name="service.vendor" value="Apache Software Foundation" />
  <service>
    <provide interface="java.util.Comparator" />
  </service>
</component>

The test then verifies that the service becomes available

public void testImmediateService() throws Exception {

  // Track the service provided by the test bundle
  final CountDownLatch latch = new CountDownLatch(1);
  ServiceTracker tracker = new ServiceTracker(context, Comparator.class.getName(), null) {
     public Object addingService(ServiceReference reference) {
         Comparator<Object> service = (Comparator<Object>) super.addingService(reference);
         latch.countDown();
         return service;
     }
  };
  tracker.open();

  // Wait for the service to become available
  if (latch.await(2, TimeUnit.SECONDS) == false)
    throw new TimeoutException("Timeout tracking Comparator service");
}

Declarative Services support in AS7

This test uses the OSGi Repository functionality to provision the runtime with the required support functionality like this

DeclarativeServicesSupport.provideDeclarativeServices(context, bundle);

To enable declarative services support in AS7 you would configure this capability

<capability name="org.apache.felix:org.apache.felix.scr:1.6.0"/>