use of org.osgi.framework.ServiceRegistration in project felix by apache.
the class ServiceRegistryTest method testRegisterEventHookService.
public void testRegisterEventHookService() {
MockControl control = MockControl.createNiceControl(Bundle.class);
Bundle b = (Bundle) control.getMock();
control.replay();
MockControl controlContext = MockControl.createNiceControl(BundleContext.class);
BundleContext c = (BundleContext) controlContext.getMock();
controlContext.expectAndReturn(c.getBundle(), b);
controlContext.replay();
ServiceRegistry sr = new ServiceRegistry(new Logger(), null);
EventHook hook = new EventHook() {
@Override
public void event(ServiceEvent event, Collection contexts) {
}
};
assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
ServiceRegistration reg = sr.registerService(c.getBundle(), new String[] { EventHook.class.getName() }, hook, new Hashtable());
assertEquals(1, sr.getHookRegistry().getHooks(EventHook.class).size());
assertTrue(sr.getHookRegistry().getHooks(EventHook.class).iterator().next() instanceof ServiceReference);
assertSame(reg.getReference(), sr.getHookRegistry().getHooks(EventHook.class).iterator().next());
assertSame(hook, ((ServiceRegistrationImpl) reg).getService());
assertEquals("Postcondition failed", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
assertEquals("Postcondition failed", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
sr.unregisterService(b, reg);
assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
}
use of org.osgi.framework.ServiceRegistration in project felix by apache.
the class ServiceRegistryTest method testRegisterCombinedService.
public void testRegisterCombinedService() {
MockControl control = MockControl.createNiceControl(Bundle.class);
Bundle b = (Bundle) control.getMock();
control.replay();
MockControl controlContext = MockControl.createNiceControl(BundleContext.class);
BundleContext c = (BundleContext) controlContext.getMock();
controlContext.expectAndReturn(c.getBundle(), b);
controlContext.replay();
ServiceRegistry sr = new ServiceRegistry(new Logger(), null);
class CombinedService implements ListenerHook, FindHook, EventHook, Runnable {
@Override
public void added(Collection listeners) {
}
@Override
public void removed(Collection listener) {
}
@Override
public void find(BundleContext context, String name, String filter, boolean allServices, Collection references) {
}
@Override
public void event(ServiceEvent event, Collection contexts) {
}
@Override
public void run() {
}
}
CombinedService hook = new CombinedService();
assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
ServiceRegistration reg = sr.registerService(c.getBundle(), new String[] { Runnable.class.getName(), ListenerHook.class.getName(), FindHook.class.getName(), EventHook.class.getName() }, hook, new Hashtable());
assertEquals(1, sr.getHookRegistry().getHooks(ListenerHook.class).size());
assertSame(reg.getReference(), sr.getHookRegistry().getHooks(ListenerHook.class).iterator().next());
assertSame(hook, ((ServiceRegistrationImpl) reg).getService());
assertEquals(1, sr.getHookRegistry().getHooks(EventHook.class).size());
assertSame(reg.getReference(), sr.getHookRegistry().getHooks(EventHook.class).iterator().next());
assertSame(hook, ((ServiceRegistrationImpl) reg).getService());
assertEquals(1, sr.getHookRegistry().getHooks(FindHook.class).size());
assertSame(reg.getReference(), sr.getHookRegistry().getHooks(FindHook.class).iterator().next());
assertSame(hook, ((ServiceRegistrationImpl) reg).getService());
sr.unregisterService(b, reg);
assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
}
use of org.osgi.framework.ServiceRegistration in project felix by apache.
the class ServiceRegistryTest method testRegisterFindHookServiceFactory.
public void testRegisterFindHookServiceFactory() {
MockControl control = MockControl.createNiceControl(Bundle.class);
Bundle b = (Bundle) control.getMock();
control.replay();
MockControl controlContext = MockControl.createNiceControl(BundleContext.class);
BundleContext c = (BundleContext) controlContext.getMock();
controlContext.expectAndReturn(c.getBundle(), b);
controlContext.replay();
ServiceRegistry sr = new ServiceRegistry(new Logger(), null);
MockControl sfControl = MockControl.createNiceControl(ServiceFactory.class);
sfControl.replay();
ServiceFactory sf = (ServiceFactory) sfControl.getMock();
assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
ServiceRegistration reg = sr.registerService(c.getBundle(), new String[] { FindHook.class.getName() }, sf, new Hashtable());
assertEquals(1, sr.getHookRegistry().getHooks(FindHook.class).size());
assertSame(reg.getReference(), sr.getHookRegistry().getHooks(FindHook.class).iterator().next());
assertSame(sf, ((ServiceRegistrationImpl) reg).getService());
assertEquals("Postcondition failed", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
assertEquals("Postcondition failed", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
sr.unregisterService(b, reg);
assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
}
use of org.osgi.framework.ServiceRegistration in project felix by apache.
the class ThreadExporter method cleanUp.
/**
*/
public synchronized void cleanUp() {
Activator.logger.INFO("Cleaning...");
Enumeration keys;
Activator.logger.INFO("Removing temporary listener....");
keys = exportedDevices.keys();
while (keys.hasMoreElements()) {
ServiceRegistration sr = ((ExportedDeviceInfo) exportedDevices.get(keys.nextElement())).getServiceRegistration();
sr.unregister();
}
Activator.logger.INFO("Done");
Activator.logger.INFO("Removing device....");
keys = exportedDevices.keys();
while (keys.hasMoreElements()) {
Device dev = ((ExportedDeviceInfo) exportedDevices.get(keys.nextElement())).getDevice();
dev.stop();
}
Activator.logger.INFO("Done");
}
use of org.osgi.framework.ServiceRegistration in project felix by apache.
the class MyCtrlPoint method doServiceUpdating.
public void doServiceUpdating(String udn, String serviceType) {
Activator.logger.DEBUG("[Importer] check for service updating");
OSGiDeviceInfo deviceinfo = (OSGiDeviceInfo) devices.get(udn);
UPnPDeviceImpl device = deviceinfo.getOSGiDevice();
boolean isSerPresent = device.existServiceType(serviceType);
if (!isSerPresent) {
/*
* The serivice doesn't exist so it's new.
* Find the udn of owner device and re-register the owner
*/
ServiceRegistration registar = ((OSGiDeviceInfo) devices.remove(udn)).getRegistration();
String[] oldServicesID = (String[]) device.getDescriptions(null).get(UPnPServiceImpl.ID);
String[] oldServicesType = (String[]) device.getDescriptions(null).get(UPnPServiceImpl.TYPE);
// to handle multiple instance of a serivice of the same type
Device cyberDevice = findDeviceCtrl(this, udn);
ServiceList serviceList = cyberDevice.getServiceList();
ArrayList newServicesID = new ArrayList();
for (int i = 0; i < serviceList.size(); i++) {
if (serviceList.getService(i).getServiceType().equals(serviceType)) {
newServicesID.add(serviceList.getService(i).getServiceID());
}
}
// adding the new servicesID
String[] currentServicesID = new String[(oldServicesID.length + newServicesID.size())];
int endOld = 1;
for (int i = 0; i < oldServicesID.length; i++) {
currentServicesID[i] = oldServicesID[i];
endOld++;
}
int j = 0;
for (; endOld < currentServicesID.length; endOld++) {
currentServicesID[endOld] = (String) newServicesID.get(j);
j++;
}
// adding the new ServiceType
String[] currentServicesType = new String[oldServicesType.length + 1];
for (int i = 0; i < oldServicesType.length; i++) {
currentServicesType[i] = oldServicesType[i];
}
currentServicesType[currentServicesType.length - 1] = serviceType;
// unregistring the OSGi Device
// and setting new properties
unregisterUPnPDevice(registar);
device.setProperty(UPnPService.ID, currentServicesID);
device.setProperty(UPnPServiceImpl.TYPE, currentServicesType);
// registering the service with the updated properties
// TODO Check if null to the first paramaters is correct or it requires the reference to the cyberdomo upnp device
registerUPnPDevice(null, device, device.getDescriptions(null));
searchForListener(cyberDevice);
}
}
Aggregations