Search in sources :

Example 76 with ServiceRegistration

use of org.osgi.framework.ServiceRegistration in project felix by apache.

the class MyCtrlPoint method deviceNotifyReceived.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.cybergarage.upnp.device.NotifyListener#deviceNotifyReceived(org.cybergarage.upnp.ssdp.SSDPPacket)
	 */
public void deviceNotifyReceived(SSDPPacket ssdpPacket) {
    Activator.logger.DEBUG("[Importer] deviceNotifyReceived");
    Activator.logger.PACKET(ssdpPacket.toString());
    /*
		 * if the packet is 
		 * 		NOTIFY or ISALIVE or *new* ROOT	then create and register the UPnPDevice and 
		 * 										all the embeeded device too
		 * 		DEVICE or SERVICE	then if they already exist in OSGi do nothing otherwise I'll create and 
		 * 							register all the UPnPDevice need starting from the root device
		 * 		*root* BYEBYE		then I'll unregister it and all its children from OSGi Framework 
		 * 		*service* BYEBYE	then I'll re-register the UPnPDevice that contain the service with the updated
		 * 							properties 
		 * 		*device* BYEBYE		then I'll re-register the UPnPDevice that contain the device with the updated
		 * 							properties and also unregister the UPnPDevice that has left
		 */
    String usn = ssdpPacket.getUSN();
    ParseUSN parseUSN = new ParseUSN(usn);
    String udn = parseUSN.getUDN();
    ServiceReference[] refs = null;
    String filter = "(&" + UPNP_DEVICE_FLTR + EXPORT_FLTR + ")";
    try {
        refs = context.getServiceReferences(UPnPDevice.class.getName(), filter);
    } catch (InvalidSyntaxException e) {
        e.printStackTrace();
    }
    if (refs != null) {
        for (int i = 0; i < refs.length; i++) {
            UPnPDevice dev = (UPnPDevice) context.getService(refs[i]);
            Dictionary dic = dev.getDescriptions(null);
            if (((String) dic.get(UPnPDevice.UDN)).equals(udn)) {
                return;
            }
        }
    }
    if (ssdpPacket.isAlive()) {
        Activator.logger.DEBUG("[Importer] ssdpPacket.isAlive");
        if (devices.containsKey(udn)) {
            Activator.logger.DEBUG("[Importer] Device already discovered");
            if (parseUSN.isService()) {
                doServiceUpdating(udn, parseUSN.getServiceType());
            }
        } else {
            doDeviceRegistration(udn);
        }
    } else if (ssdpPacket.isByeBye()) {
        Activator.logger.DEBUG("[Importer] ssdpPacket.isByeBye");
        synchronized (devices) {
            if (devices.containsKey(udn)) {
                if (parseUSN.isDevice()) {
                    Activator.logger.DEBUG("[Importer] parseUSN.isDevice ...unregistering all the children devices ");
                    // unregistering all the children devices
                    UPnPDeviceImpl dev = ((OSGiDeviceInfo) devices.get(udn)).getOSGiDevice();
                    removeOSGiandUPnPDeviceHierarchy(dev);
                } else if (parseUSN.isService()) {
                    Activator.logger.DEBUG("[Importer] parseUSN.isService ...registering modified device again ");
                    /* 
						 * I have to unregister the UPnPDevice and register it again 
						 * with the updated properties  
						 */
                    UPnPDeviceImpl device = ((OSGiDeviceInfo) devices.get(udn)).getOSGiDevice();
                    ServiceRegistration registar = ((OSGiDeviceInfo) devices.get(udn)).getRegistration();
                    String[] oldServicesID = (String[]) (device.getDescriptions(null).get(UPnPService.ID));
                    String[] oldServiceType = (String[]) (device.getDescriptions(null).get(UPnPService.TYPE));
                    Device cyberDevice = findDeviceCtrl(this, udn);
                    Vector vec = new Vector();
                    for (int i = 0; i < oldServiceType.length; i++) {
                        Service ser = cyberDevice.getService(oldServicesID[i]);
                        if (!(ser.getServiceType().equals(parseUSN.getServiceType()))) {
                            vec.add(oldServicesID[i]);
                        }
                    }
                    // new serviceID
                    String[] actualServicesID = new String[vec.size()];
                    actualServicesID = (String[]) vec.toArray(new String[] {});
                    // new serviceType
                    String[] actualServiceType = new String[oldServiceType.length - 1];
                    vec.clear();
                    for (int i = 0; i < oldServiceType.length; i++) {
                        if (!(oldServiceType[i].equals(parseUSN.getServiceType()))) {
                            vec.add(oldServiceType[i]);
                        }
                    }
                    actualServiceType = (String[]) vec.toArray(new String[] {});
                    // unrigistering and registering again with the new properties
                    unregisterUPnPDevice(registar);
                    device.setProperty(UPnPService.ID, actualServicesID);
                    device.setProperty(UPnPService.TYPE, actualServiceType);
                    registerUPnPDevice(null, device, device.getDescriptions(null));
                    searchForListener(cyberDevice);
                }
            }
        }
    // synchronized(devices)
    } else {
    /*
			 * if it is a service means that it has deleted when the 
			 * owner was unregister so I can skip this bye-bye
			 * 
			 * //TODO Understand the comment
			 *
			 */
    }
}
Also used : Dictionary(java.util.Dictionary) UPnPDevice(org.osgi.service.upnp.UPnPDevice) Device(org.cybergarage.upnp.Device) UPnPService(org.osgi.service.upnp.UPnPService) Service(org.cybergarage.upnp.Service) UPnPDeviceImpl(org.apache.felix.upnp.basedriver.importer.core.upnp.UPnPDeviceImpl) ControlPoint(org.cybergarage.upnp.ControlPoint) ServiceReference(org.osgi.framework.ServiceReference) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) UPnPDevice(org.osgi.service.upnp.UPnPDevice) ParseUSN(org.apache.felix.upnp.basedriver.importer.util.ParseUSN) Vector(java.util.Vector) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 77 with ServiceRegistration

use of org.osgi.framework.ServiceRegistration in project felix by apache.

the class MetatypeInventoryPrinter method unregister.

void unregister() {
    ServiceRegistration registration = this.registration;
    this.registration = null;
    if (registration != null) {
        registration.unregister();
    }
}
Also used : ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 78 with ServiceRegistration

use of org.osgi.framework.ServiceRegistration in project felix by apache.

the class Felix4350Test method doTest.

protected void doTest(String componentName) throws Exception {
    ServiceRegistration dep1Reg = register(new SimpleComponent(), 0);
    ServiceRegistration dep2Reg = register(new SimpleComponent2(), 1000);
    final ComponentDescriptionDTO main = findComponentDescriptorByName(componentName);
    TestCase.assertNotNull(main);
    // needs to be async
    asyncEnable(main);
    // dep2 getService has not yet returned
    delay(300);
    dep1Reg.unregister();
    // dep2 getService has returned
    delay(2000);
    Felix4350Component.check(0, 0, false);
    dep1Reg = register(new SimpleComponent(), 0);
    delay(300);
    Felix4350Component.check(1, 0, true);
    // does not need to be asyncv??
    disableAndCheck(main);
    dep1Reg.unregister();
    dep2Reg.unregister();
    Felix4350Component.check(1, 1, false);
    dep1Reg = register(new SimpleComponent(), 0);
    dep2Reg = register(new SimpleComponent2(), 1000);
    Felix4350Component.check(1, 1, false);
    // needs to be async
    asyncEnable(main);
    delay(300);
    dep1Reg.unregister();
    delay(100);
    dep1Reg = register(new SimpleComponent(), 0);
    delay(2000);
    // n.b. counts are cumulative
    Felix4350Component.check(2, 1, true);
}
Also used : ComponentDescriptionDTO(org.osgi.service.component.runtime.dto.ComponentDescriptionDTO) SimpleComponent(org.apache.felix.scr.integration.components.SimpleComponent) SimpleComponent2(org.apache.felix.scr.integration.components.SimpleComponent2) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 79 with ServiceRegistration

use of org.osgi.framework.ServiceRegistration in project felix by apache.

the class SimpleService2Impl method drop.

public void drop() {
    ServiceRegistration sr = getRegistration();
    if (sr != null) {
        setRegistration(null);
        sr.unregister();
    }
}
Also used : ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 80 with ServiceRegistration

use of org.osgi.framework.ServiceRegistration in project sling by apache.

the class MockBundleContextDynamicReferencesTest method testAddRemoveOptionalUnaryService.

@Test
public void testAddRemoveOptionalUnaryService() {
    ServiceRegistration reg1aOptional = bundleContext.registerService(ServiceInterface1Optional.class.getName(), dependency1aOptional, null);
    assertDependency1Optional(dependency1aOptional);
    reg1aOptional.unregister();
    assertDependency1Optional(null);
}
Also used : ServiceInterface1Optional(org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.ServiceInterface1Optional) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

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