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"));
}
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();
}
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();
}
}
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();
}
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();
}
}
Aggregations