Search in sources :

Example 6 with ServiceTracker

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

the class ServiceTrackerTest method testPlainServiceTracker.

public void testPlainServiceTracker() {
    ServiceTracker st = new ServiceTracker(context, ServiceInterface.class.getName(), null);
    st.open();
    ServiceRegistration sr = context.registerService(ServiceInterface.class.getName(), new ServiceProvider(), null);
    Assert.assertEquals("There should be one service that matches the tracker", 1, st.getServices().length);
    sr.unregister();
    Assert.assertNull("There should be no service that matches the tracker", st.getServices());
    st.close();
}
Also used : ServiceTracker(org.apache.felix.dm.tracker.ServiceTracker) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 7 with ServiceTracker

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

the class ServiceTrackerTest method testExistingAspectServiceTracker.

public void testExistingAspectServiceTracker() {
    ServiceTracker st = new ServiceTracker(context, ServiceInterface.class.getName(), null);
    ServiceRegistration sr = context.registerService(ServiceInterface.class.getName(), new ServiceProvider(), null);
    final long sid = ServiceUtil.getServiceId(sr.getReference());
    ServiceRegistration asr = context.registerService(ServiceInterface.class.getName(), new ServiceProvider(), new Hashtable() {

        {
            put(DependencyManager.ASPECT, sid);
            put(Constants.SERVICE_RANKING, 10);
        }
    });
    ServiceRegistration asr2 = context.registerService(ServiceInterface.class.getName(), new ServiceProvider(), new Hashtable() {

        {
            put(DependencyManager.ASPECT, sid);
            put(Constants.SERVICE_RANKING, 20);
        }
    });
    st.open();
    Assert.assertEquals("There should be one service that matches the tracker", 1, st.getServices().length);
    Assert.assertEquals("Service ranking should be 20", Integer.valueOf(20), (Integer) st.getServiceReference().getProperty(Constants.SERVICE_RANKING));
    asr2.unregister();
    Assert.assertEquals("There should be one service that matches the tracker", 1, st.getServices().length);
    Assert.assertEquals("Service ranking should be 10", Integer.valueOf(10), (Integer) st.getServiceReference().getProperty(Constants.SERVICE_RANKING));
    asr.unregister();
    Assert.assertEquals("There should be one service that matches the tracker", 1, st.getServices().length);
    Assert.assertNull("Service should not have a ranking", st.getServiceReference().getProperty(Constants.SERVICE_RANKING));
    sr.unregister();
    Assert.assertNull("There should be no service that matches the tracker", st.getServices());
    st.close();
}
Also used : ServiceTracker(org.apache.felix.dm.tracker.ServiceTracker) Hashtable(java.util.Hashtable) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 8 with ServiceTracker

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

the class ServiceTrackerTest method testAspectServiceTracker.

public void testAspectServiceTracker() {
    ServiceTracker st = new ServiceTracker(context, ServiceInterface.class.getName(), null);
    st.open();
    ServiceRegistration sr = context.registerService(ServiceInterface.class.getName(), new ServiceProvider(), null);
    Assert.assertEquals("There should be one service that matches the tracker", 1, st.getServices().length);
    final long sid = ServiceUtil.getServiceId(sr.getReference());
    ServiceRegistration asr = context.registerService(ServiceInterface.class.getName(), new ServiceProvider(), new Hashtable() {

        {
            put(DependencyManager.ASPECT, sid);
            put(Constants.SERVICE_RANKING, 10);
        }
    });
    Assert.assertEquals("There should be one service that matches the tracker", 1, st.getServices().length);
    Assert.assertEquals("Service ranking should be 10", Integer.valueOf(10), (Integer) st.getServiceReference().getProperty(Constants.SERVICE_RANKING));
    ServiceRegistration asr2 = context.registerService(ServiceInterface.class.getName(), new ServiceProvider(), new Hashtable() {

        {
            put(DependencyManager.ASPECT, sid);
            put(Constants.SERVICE_RANKING, 20);
        }
    });
    Assert.assertEquals("There should be one service that matches the tracker", 1, st.getServices().length);
    Assert.assertEquals("Service ranking should be 20", Integer.valueOf(20), (Integer) st.getServiceReference().getProperty(Constants.SERVICE_RANKING));
    asr.unregister();
    Assert.assertEquals("There should be one service that matches the tracker", 1, st.getServices().length);
    Assert.assertEquals("Service ranking should be 20", Integer.valueOf(20), (Integer) st.getServiceReference().getProperty(Constants.SERVICE_RANKING));
    asr2.unregister();
    Assert.assertEquals("There should be one service that matches the tracker", 1, st.getServices().length);
    Assert.assertNull("Service should not have a ranking", st.getServiceReference().getProperty(Constants.SERVICE_RANKING));
    sr.unregister();
    Assert.assertNull("There should be no service that matches the tracker", st.getServices());
    st.close();
}
Also used : ServiceTracker(org.apache.felix.dm.tracker.ServiceTracker) Hashtable(java.util.Hashtable) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 9 with ServiceTracker

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

the class AdapterFilterIndex 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 10 with ServiceTracker

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

the class AspectFilterIndex 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)

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