Package org.apache.felix.dm.tracker

Examples of org.apache.felix.dm.tracker.ServiceTracker


@RunWith(PaxExam.class)
public class ServiceTrackerTest extends TestBase {
    @Test
    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();
    }
View Full Code Here


        st.close();
    }
   
    @Test
    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();
    }
View Full Code Here

        st.close();
    }
   
    @Test
    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();
    }
View Full Code Here

        synchronized (m_lock) {
            if (m_context != null) {
                throw new IllegalStateException("Filter already open.");
            }
            try {
                m_tracker = new ServiceTracker(context, context.createFilter("(" + Constants.OBJECTCLASS + "=*)"), this);
            }
            catch (InvalidSyntaxException e) {
                throw new Error();
            }
            m_context = context;
View Full Code Here

        }
        m_tracker.open(true, true);
    }

    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();
    }
View Full Code Here

            m_services.add(service);
            if (!m_isStarted) {
                if (m_trackedServiceName != null) {
                    if (m_trackedServiceFilter != null) {
                        try {
                            m_tracker = new ServiceTracker(m_context, m_context.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(m_context, m_trackedServiceReference, this);
                    } else {
                        m_tracker = new ServiceTracker(m_context, m_trackedServiceName.getName(), this);
                    }
                } else {
                    throw new IllegalStateException("Could not create tracker for dependency, no service name specified.");
                }
                m_isStarted = true;
View Full Code Here

        synchronized (m_lock) {
            if (m_context != null) {
                throw new IllegalStateException("Filter already open.");
            }
            try {
                m_tracker = new ServiceTracker(context, context.createFilter("(" + Constants.OBJECTCLASS + "=*)"), this);
            }
            catch (InvalidSyntaxException e) {
                throw new Error();
            }
            m_context = context;
View Full Code Here

        }
        m_tracker.open(true, true);
    }

    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();
    }
View Full Code Here

        synchronized (m_lock) {
            if (m_context != null) {
                throw new IllegalStateException("Filter already open.");
            }
            try {
                m_tracker = new ServiceTracker(context, context.createFilter("(" + Constants.OBJECTCLASS + "=*)"), this);
            }
            catch (InvalidSyntaxException e) {
                throw new Error();
            }
            m_context = context;
View Full Code Here

        }
        m_tracker.open(true, true);
    }

  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();
  }
View Full Code Here

TOP

Related Classes of org.apache.felix.dm.tracker.ServiceTracker

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.