Search in sources :

Example 51 with ServiceTracker

use of org.osgi.util.tracker.ServiceTracker in project sling by apache.

the class DataSourceIT method testDataSourceAsService.

@SuppressWarnings("unchecked")
@Test
public void testDataSourceAsService() throws Exception {
    Configuration config = ca.createFactoryConfiguration(PID, null);
    Dictionary<String, Object> p = new Hashtable<String, Object>();
    p.put("url", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");
    p.put("datasource.name", "test");
    p.put("initialSize", "5");
    p.put("defaultAutoCommit", "default");
    p.put("defaultReadOnly", "false");
    p.put("datasource.svc.properties", new String[] { "initSQL=SELECT 1" });
    p.put("maxActive", 70);
    config.update(p);
    Filter filter = context.createFilter("(&(objectclass=javax.sql.DataSource)(datasource.name=test))");
    ServiceTracker<DataSource, DataSource> st = new ServiceTracker<DataSource, DataSource>(context, filter, null);
    st.open();
    DataSource ds = st.waitForService(10000);
    assertNotNull(ds);
    Connection conn = ds.getConnection();
    assertNotNull(conn);
    //Cannot access directly so access via reflection
    assertEquals("70", getProperty(ds, "poolProperties.maxActive"));
    assertEquals("5", getProperty(ds, "poolProperties.initialSize"));
    assertEquals("SELECT 1", getProperty(ds, "poolProperties.initSQL"));
    assertEquals("false", getProperty(ds, "poolProperties.defaultReadOnly"));
    assertNull(getProperty(ds, "poolProperties.defaultAutoCommit"));
    config = ca.listConfigurations("(datasource.name=test)")[0];
    Dictionary dic = config.getProperties();
    dic.put("defaultReadOnly", Boolean.TRUE);
    config.update(dic);
    TimeUnit.MILLISECONDS.sleep(100);
    assertEquals("true", getProperty(ds, "poolProperties.defaultReadOnly"));
}
Also used : Dictionary(java.util.Dictionary) Configuration(org.osgi.service.cm.Configuration) Filter(org.osgi.framework.Filter) ServiceTracker(org.osgi.util.tracker.ServiceTracker) Hashtable(java.util.Hashtable) Connection(java.sql.Connection) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 52 with ServiceTracker

use of org.osgi.util.tracker.ServiceTracker in project sling by apache.

the class TestsManagerImpl method activate.

protected void activate(ComponentContext ctx) {
    bundleContext = ctx.getBundleContext();
    tracker = new ServiceTracker(bundleContext, TestsProvider.class.getName(), null);
    tracker.open();
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker)

Example 53 with ServiceTracker

use of org.osgi.util.tracker.ServiceTracker in project bnd by bndtools.

the class Activator method start.

public void start(BundleContext context) throws Exception {
    this.context = context;
    this.packageAdminTracker = new ServiceTracker(context, PackageAdmin.class.getName(), null);
    this.packageAdminTracker.open();
    active = true;
    if (!Boolean.valueOf(context.getProperty(TESTER_SEPARATETHREAD)) && Boolean.valueOf(context.getProperty("launch.services"))) {
        // can't
        // register
        // services
        // on
        // mini
        // framework
        Hashtable<String, String> ht = new Hashtable<String, String>();
        ht.put("main.thread", "true");
        ht.put(Constants.SERVICE_DESCRIPTION, "JUnit tester");
        context.registerService(Runnable.class.getName(), this, ht);
    } else {
        thread = new Thread(this, "bnd Runtime Test Bundle");
        thread.start();
    }
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) Hashtable(java.util.Hashtable)

Example 54 with ServiceTracker

use of org.osgi.util.tracker.ServiceTracker in project cxf by apache.

the class Activator method start.

public void start(BundleContext context) throws Exception {
    _context = context;
    // Use _tracker to capture when a HttpService comes and goes.
    // 
    // When this bundle is started, a HttpService may not be alive. Thus, we use
    // ServiceTracker to automatically monitor when a HttpService comes alive and
    // then register this our CXF-based JAX-RS service with it.
    // 
    _tracker = new ServiceTracker(_context, HttpService.class.getName(), new ServiceTrackerCustomizer() {

        public Object addingService(ServiceReference serviceReference) {
            try {
                HttpService service = (HttpService) _context.getService(serviceReference);
                Dictionary<String, String> initParams = new Hashtable<String, String>();
                initParams.put("javax.ws.rs.Application", SampleApplication.class.getName());
                service.registerServlet(_path, new SampleServlet(), initParams, null);
                return service;
            } catch (Exception ex) {
                ex.printStackTrace();
                throw new RuntimeException(ex);
            }
        }

        public void modifiedService(ServiceReference serviceReference, Object o) {
        // do nothing
        }

        public void removedService(ServiceReference serviceReference, Object o) {
            HttpService service = (HttpService) _context.getService(serviceReference);
            if (service != null) {
                service.unregister(_path);
            }
        }
    });
    _tracker.open();
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) HttpService(org.osgi.service.http.HttpService) Hashtable(java.util.Hashtable) ServiceReference(org.osgi.framework.ServiceReference)

Example 55 with ServiceTracker

use of org.osgi.util.tracker.ServiceTracker in project rt.equinox.framework by eclipse.

the class ServiceTrackerTests method testServiceTracker02.

public void testServiceTracker02() {
    final String testMethodName = getName();
    // simple ServiceTracker test
    Runnable runIt = new Runnable() {

        public void run() {
        // nothing
        }
    };
    Hashtable props = new Hashtable();
    props.put(testMethodName, Boolean.FALSE);
    ServiceRegistration reg = OSGiTestsActivator.getContext().registerService(Runnable.class.getName(), runIt, props);
    ServiceTracker testTracker = null;
    try {
        final boolean[] results = new boolean[] { false, false, false };
        ServiceTrackerCustomizer testCustomizer = new ServiceTrackerCustomizer() {

            public Object addingService(ServiceReference reference) {
                results[0] = true;
                return reference;
            }

            public void modifiedService(ServiceReference reference, Object service) {
                results[1] = true;
            }

            public void removedService(ServiceReference reference, Object service) {
                results[2] = true;
            }
        };
        try {
            // $NON-NLS-1$ //$NON-NLS-2$
            testTracker = new ServiceTracker(OSGiTestsActivator.getContext(), FrameworkUtil.createFilter("(&(objectclass=java.lang.Runnable)(" + testMethodName.toLowerCase() + "=true))"), testCustomizer);
        } catch (InvalidSyntaxException e) {
            // $NON-NLS-1$
            fail("filter error", e);
        }
        testTracker.open();
        // $NON-NLS-1$
        assertFalse("Did call addingService", results[0]);
        // $NON-NLS-1$
        assertFalse("Did call modifiedService", results[1]);
        // $NON-NLS-1$
        assertFalse("Did call removedService", results[2]);
        clearResults(results);
        // change props to match
        props.put(testMethodName, Boolean.TRUE);
        reg.setProperties(props);
        // $NON-NLS-1$
        assertTrue("Did not call addingService", results[0]);
        // $NON-NLS-1$
        assertFalse("Did call modifiedService", results[1]);
        // $NON-NLS-1$
        assertFalse("Did call removedService", results[2]);
        clearResults(results);
        // change props to still match
        // $NON-NLS-1$
        props.put("testChangeProp", Boolean.TRUE);
        reg.setProperties(props);
        // $NON-NLS-1$
        assertFalse("Did call addingService", results[0]);
        // $NON-NLS-1$
        assertTrue("Did not call modifiedService", results[1]);
        // $NON-NLS-1$
        assertFalse("Did call removedService", results[2]);
        clearResults(results);
        // change props to no longer match
        props.put(testMethodName, Boolean.FALSE);
        reg.setProperties(props);
        // $NON-NLS-1$
        assertFalse("Did call addingService", results[0]);
        // $NON-NLS-1$
        assertFalse("Did call modifiedService", results[1]);
        // $NON-NLS-1$
        assertTrue("Did not call removedService", results[2]);
        clearResults(results);
        // change props to no longer match
        // $NON-NLS-1$
        props.put("testChangeProp", Boolean.FALSE);
        reg.setProperties(props);
        // $NON-NLS-1$
        assertFalse("Did call addingService", results[0]);
        // $NON-NLS-1$
        assertFalse("Did call modifiedService", results[1]);
        // $NON-NLS-1$
        assertFalse("Did call removedService", results[2]);
        clearResults(results);
    } finally {
        if (reg != null)
            reg.unregister();
        if (testTracker != null)
            testTracker.close();
    }
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) Hashtable(java.util.Hashtable) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer)

Aggregations

ServiceTracker (org.osgi.util.tracker.ServiceTracker)115 ServiceReference (org.osgi.framework.ServiceReference)33 Filter (org.osgi.framework.Filter)28 ServiceTrackerCustomizer (org.osgi.util.tracker.ServiceTrackerCustomizer)19 Hashtable (java.util.Hashtable)18 Activate (org.apache.felix.scr.annotations.Activate)18 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)14 BundleContext (org.osgi.framework.BundleContext)12 ConfigurationException (org.osgi.service.cm.ConfigurationException)10 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)8 IOException (java.io.IOException)7 Dictionary (java.util.Dictionary)7 Bundle (org.osgi.framework.Bundle)7 Converter (net.heartsome.cat.converter.Converter)6 AndFilter (net.heartsome.cat.converter.util.AndFilter)6 EqFilter (net.heartsome.cat.converter.util.EqFilter)6 Configuration (org.osgi.service.cm.Configuration)6 HashMap (java.util.HashMap)5 CdiContainer (org.osgi.service.cdi.CdiContainer)4