Search in sources :

Example 71 with ServiceRegistration

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());
}
Also used : EventHook(org.osgi.framework.hooks.service.EventHook) MockControl(org.easymock.MockControl) Bundle(org.osgi.framework.Bundle) ServiceEvent(org.osgi.framework.ServiceEvent) Hashtable(java.util.Hashtable) Collection(java.util.Collection) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) ServiceReference(org.osgi.framework.ServiceReference)

Example 72 with ServiceRegistration

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());
}
Also used : FindHook(org.osgi.framework.hooks.service.FindHook) Bundle(org.osgi.framework.Bundle) ListenerHook(org.osgi.framework.hooks.service.ListenerHook) Hashtable(java.util.Hashtable) EventHook(org.osgi.framework.hooks.service.EventHook) MockControl(org.easymock.MockControl) ServiceEvent(org.osgi.framework.ServiceEvent) Collection(java.util.Collection) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 73 with ServiceRegistration

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());
}
Also used : MockControl(org.easymock.MockControl) ServiceFactory(org.osgi.framework.ServiceFactory) FindHook(org.osgi.framework.hooks.service.FindHook) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 74 with ServiceRegistration

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");
}
Also used : Enumeration(java.util.Enumeration) UPnPDevice(org.osgi.service.upnp.UPnPDevice) Device(org.cybergarage.upnp.Device) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 75 with ServiceRegistration

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);
    }
}
Also used : UPnPDevice(org.osgi.service.upnp.UPnPDevice) Device(org.cybergarage.upnp.Device) ServiceList(org.cybergarage.upnp.ServiceList) ArrayList(java.util.ArrayList) UPnPDeviceImpl(org.apache.felix.upnp.basedriver.importer.core.upnp.UPnPDeviceImpl) ControlPoint(org.cybergarage.upnp.ControlPoint) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Aggregations

ServiceRegistration (org.osgi.framework.ServiceRegistration)365 Test (org.junit.Test)106 Hashtable (java.util.Hashtable)94 Bundle (org.osgi.framework.Bundle)64 BundleContext (org.osgi.framework.BundleContext)64 Dictionary (java.util.Dictionary)62 Ensure (org.apache.felix.dm.itest.util.Ensure)42 Properties (java.util.Properties)39 ServiceReference (org.osgi.framework.ServiceReference)38 ArrayList (java.util.ArrayList)34 HashMap (java.util.HashMap)28 ServiceFactory (org.osgi.framework.ServiceFactory)21 IOException (java.io.IOException)16 Map (java.util.Map)15 List (java.util.List)12 ServiceEvent (org.osgi.framework.ServiceEvent)12 HashSet (java.util.HashSet)11 CountDownLatch (java.util.concurrent.CountDownLatch)10 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)10 Collection (java.util.Collection)9