Search in sources :

Example 1 with ServiceTracker

use of org.apache.felix.dm.tracker.ServiceTracker in project felix by apache.

the class ServiceDependencyImpl method start.

@Override
public void start() {
    if (m_trackedServiceName != null) {
        BundleContext ctx = m_component.getBundleContext();
        if (m_trackedServiceFilter != null) {
            try {
                m_tracker = new ServiceTracker(ctx, ctx.createFilter(m_trackedServiceFilter), this);
            } catch (InvalidSyntaxException e) {
                throw new IllegalStateException("Invalid filter definition for dependency: " + m_trackedServiceFilter);
            }
        } else if (m_trackedServiceReference != null) {
            m_tracker = new ServiceTracker(ctx, m_trackedServiceReference, this);
        } else {
            m_tracker = new ServiceTracker(ctx, m_trackedServiceName.getName(), this);
        }
    } else {
        throw new IllegalStateException("Could not create tracker for dependency, no service name specified.");
    }
    if (m_debug) {
        m_tracker.setDebug(m_debugKey);
    }
    m_tracker.open();
    super.start();
}
Also used : ServiceTracker(org.apache.felix.dm.tracker.ServiceTracker) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) BundleContext(org.osgi.framework.BundleContext)

Example 2 with ServiceTracker

use of org.apache.felix.dm.tracker.ServiceTracker in project felix by apache.

the class MultiPropertyFilterIndex method close.

public void close() {
    ServiceTracker tracker;
    synchronized (m_lock) {
        if (m_context == null) {
            throw new IllegalStateException("Filter already closed.");
        }
        tracker = m_tracker;
        m_tracker = null;
        m_context = null;
    }
    tracker.close();
}
Also used : ServiceTracker(org.apache.felix.dm.tracker.ServiceTracker)

Example 3 with ServiceTracker

use of org.apache.felix.dm.tracker.ServiceTracker in project felix by apache.

the class TrackedTest method testSetInitialTrackAspects.

@Test
public void testSetInitialTrackAspects() {
    System.out.println("testSetInitialTrackAspects");
    TestCustomizer customizer = new TestCustomizer();
    ServiceTracker tracker = new TestTracker(customizer);
    tracker.open(false, true);
    Tracked tracked = tracker.getTracked();
    Object[] initialReferences = new Object[] { createServiceReference(1L), createServiceReference(2L, 1L, 10), createServiceReference(3L, 1L, 5) };
    tracked.setInitial(initialReferences);
    tracked.trackInitial();
    tracked.getExecutor().execute();
    assertArrayEquals(new Long[] { 1L, 2L, 3L }, customizer.getServiceReferenceIds());
}
Also used : Tracked(org.apache.felix.dm.tracker.ServiceTracker.Tracked) ServiceTracker(org.apache.felix.dm.tracker.ServiceTracker) Test(org.junit.Test)

Example 4 with ServiceTracker

use of org.apache.felix.dm.tracker.ServiceTracker in project felix by apache.

the class TrackedTest method testSetInitialHideAspects.

@Test
public void testSetInitialHideAspects() {
    System.out.println("testSetInitialHideAspects");
    TestCustomizer customizer = new TestCustomizer();
    ServiceTracker tracker = new TestTracker(customizer);
    tracker.open();
    Tracked tracked = tracker.getTracked();
    Object[] initialReferences = new Object[] { createServiceReference(1L), createServiceReference(2L, 1L, 10), createServiceReference(3L), createServiceReference(4L, 1L, 5), createServiceReference(5L, 3L, 5) };
    tracked.setInitial(initialReferences);
    tracked.trackInitial();
    tracked.getExecutor().execute();
    assertArrayEquals(new Long[] { 2L, 5L }, customizer.getServiceReferenceIds());
}
Also used : Tracked(org.apache.felix.dm.tracker.ServiceTracker.Tracked) ServiceTracker(org.apache.felix.dm.tracker.ServiceTracker) Test(org.junit.Test)

Example 5 with ServiceTracker

use of org.apache.felix.dm.tracker.ServiceTracker in project felix by apache.

the class TrackedTest method testUnHideAspect.

@Test
public void testUnHideAspect() {
    System.out.println("testUnhideAspect");
    TestCustomizer customizer = new TestCustomizer();
    ServiceTracker tracker = new TestTracker(customizer);
    tracker.open();
    Tracked tracked = tracker.getTracked();
    ServiceReference<?>[] initialReferences = new ServiceReference[] { createServiceReference(1L), createServiceReference(2L, 1L, 10), createServiceReference(3L), createServiceReference(4L, 1L, 5), createServiceReference(5L, 3L, 5) };
    tracked.setInitial(initialReferences);
    tracked.trackInitial();
    tracked.getExecutor().execute();
    assertArrayEquals(new Long[] { 2L, 5L }, customizer.getServiceReferenceIds());
    // create a service event that unregisters service with id 2, we would expect it to be swapped with 4.
    ServiceEvent event = new ServiceEvent(ServiceEvent.UNREGISTERING, initialReferences[1]);
    tracked.serviceChanged(event);
    assertArrayEquals(new Long[] { 5L, 4L }, customizer.getServiceReferenceIds());
    // create a service event that unregisters service with id 4, we would expect it to be swapped with 1.
    event = new ServiceEvent(ServiceEvent.UNREGISTERING, initialReferences[3]);
    tracked.serviceChanged(event);
    assertArrayEquals(new Long[] { 5L, 1L }, customizer.getServiceReferenceIds());
}
Also used : Tracked(org.apache.felix.dm.tracker.ServiceTracker.Tracked) ServiceTracker(org.apache.felix.dm.tracker.ServiceTracker) ServiceEvent(org.osgi.framework.ServiceEvent) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Aggregations

ServiceTracker (org.apache.felix.dm.tracker.ServiceTracker)11 Tracked (org.apache.felix.dm.tracker.ServiceTracker.Tracked)4 Test (org.junit.Test)4 ServiceRegistration (org.osgi.framework.ServiceRegistration)3 Hashtable (java.util.Hashtable)2 ServiceEvent (org.osgi.framework.ServiceEvent)2 ServiceReference (org.osgi.framework.ServiceReference)2 BundleContext (org.osgi.framework.BundleContext)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1