use of org.osgi.framework.ServiceRegistration in project sling by apache.
the class MockBundleContextTest method testServiceFactoryRegistration.
@Test
public void testServiceFactoryRegistration() throws InvalidSyntaxException {
// prepare test services
Class<String> clazz = String.class;
final String service = "abc";
Dictionary<String, Object> properties1 = getServiceProperties(null);
ServiceRegistration reg = bundleContext.registerService(clazz, new ServiceFactory<String>() {
@Override
public String getService(Bundle bundle, ServiceRegistration<String> registration) {
return service;
}
@Override
public void ungetService(Bundle bundle, ServiceRegistration<String> registration, String service) {
// do nothing
}
}, properties1);
ServiceReference<String> ref = bundleContext.getServiceReference(clazz);
assertNotNull(ref);
assertSame(reg.getReference(), ref);
assertSame(service, bundleContext.getService(ref));
bundleContext.ungetService(ref);
}
use of org.osgi.framework.ServiceRegistration in project sling by apache.
the class MockBundleContextTest method testObjectClassFilterMatches.
@Test
public void testObjectClassFilterMatches() throws InvalidSyntaxException {
Filter filter = bundleContext.createFilter("(" + Constants.OBJECTCLASS + "=" + Integer.class.getName() + ")");
ServiceRegistration serviceRegistration = bundleContext.registerService(Integer.class.getName(), Integer.valueOf(1), null);
assertTrue(filter.match(serviceRegistration.getReference()));
}
use of org.osgi.framework.ServiceRegistration in project sling by apache.
the class MockBundleContextTest method testServiceUnregistration.
@Test
public void testServiceUnregistration() {
// prepare test services
String clazz1 = String.class.getName();
Object service1 = new Object();
Dictionary<String, Object> properties1 = getServiceProperties(null);
ServiceRegistration reg1 = bundleContext.registerService(clazz1, service1, properties1);
assertNotNull(bundleContext.getServiceReference(clazz1));
reg1.unregister();
assertNull(bundleContext.getServiceReference(clazz1));
}
use of org.osgi.framework.ServiceRegistration in project sling by apache.
the class OsgiServiceUtilTest method testServiceFactoryViaManualRegistration.
@Test
public void testServiceFactoryViaManualRegistration() {
final ServiceFactory1 serviceFactory1 = new ServiceFactory1();
bundleContext.registerService(ServiceFactory1.class.getName(), new ServiceFactory() {
@Override
public Object getService(Bundle bundle, ServiceRegistration registration) {
return serviceFactory1;
}
@Override
public void ungetService(Bundle bundle, ServiceRegistration registration, Object service) {
// nothing to do
}
}, null);
assertSame(serviceFactory1, bundleContext.getService(bundleContext.getServiceReference(ServiceFactory1.class.getName())));
}
use of org.osgi.framework.ServiceRegistration in project webservices-axiom by apache.
the class OSGiOMMetaFactoryLocator method addingBundle.
@Override
public List<RegisteredImplementation> addingBundle(Bundle bundle, BundleEvent event) {
URL descriptorUrl = bundle.getEntry(ImplementationFactory.DESCRIPTOR_RESOURCE);
if (descriptorUrl != null) {
List<Implementation> discoveredImplementations = ImplementationFactory.parseDescriptor(new OSGiLoader(bundle), descriptorUrl);
List<RegisteredImplementation> registeredImplementations = new ArrayList<RegisteredImplementation>(discoveredImplementations.size());
synchronized (this) {
implementations.addAll(discoveredImplementations);
loadImplementations(implementations);
}
for (Implementation implementation : discoveredImplementations) {
List<ServiceRegistration<?>> registrations = new ArrayList<ServiceRegistration<?>>();
List<ServiceReference<?>> references = new ArrayList<ServiceReference<?>>();
for (Feature feature : implementation.getFeatures()) {
List<String> clazzes = new ArrayList<String>();
clazzes.add(OMMetaFactory.class.getName());
for (Class<?> extensionInterface : feature.getExtensionInterfaces()) {
clazzes.add(extensionInterface.getName());
}
Hashtable<String, Object> properties = new Hashtable<String, Object>();
properties.put("implementationName", implementation.getName());
properties.put("feature", feature.getName());
properties.put(Constants.SERVICE_RANKING, feature.getPriority());
ServiceRegistration<?> registration = bundle.getBundleContext().registerService(clazzes.toArray(new String[clazzes.size()]), implementation.getMetaFactory(), properties);
registrations.add(registration);
ServiceReference<?> reference = registration.getReference();
references.add(reference);
// Let the OSGi runtime know that the axiom-api bundle is using the service
apiBundleContext.getService(reference);
}
registeredImplementations.add(new RegisteredImplementation(implementation, registrations, references));
}
return registeredImplementations;
} else {
return null;
}
}
Aggregations