Package org.osgi.framework

Examples of org.osgi.framework.ServiceEvent


                        .returns(null);
            }
        });

        trackerListener.serviceChanged(
                new ServiceEvent(ServiceEvent.REGISTERED, referenceMock));

        // ---------------------------------------------------------------------
        // Modify Service
        // ---------------------------------------------------------------------

        // Modifying the service does not require any changes to the use count
        // of the service.

        managerMock.expects.serviceModified(referenceMock)
                .description("Modify Service");

        trackerListener.serviceChanged(
                new ServiceEvent(ServiceEvent.MODIFIED, referenceMock));

        // ---------------------------------------------------------------------
        // Unregister Service
        // ---------------------------------------------------------------------

        // Unregistering the service must decrement the use count of the
        // service.

        expectations.add(new OrderedExpectations() {
            public void add() {

                managerMock.expects.serviceUnregistering(referenceMock)
                        .description("Unregistering Service");

                contextMock.expects.ungetService(referenceMock)
                        .description("Unregistering Decrement Use Count")
                        .returns(false);
            }
        });

        trackerListener.serviceChanged(
                new ServiceEvent(ServiceEvent.UNREGISTERING, referenceMock));
    }
View Full Code Here


                        .returns(null);
            }
        });

        trackerListener.serviceChanged(
                new ServiceEvent(ServiceEvent.REGISTERED, referenceMock));

        // ---------------------------------------------------------------------
        // Modify Service
        // ---------------------------------------------------------------------

        // Modifying the service does not require any changes to the use count
        // of the service.

        pluginManagerMock.expects.pluginModified(referenceMock)
                .description("Modify Service");

        trackerListener.serviceChanged(
                new ServiceEvent(ServiceEvent.MODIFIED, referenceMock));

        // ---------------------------------------------------------------------
        // Unregister Service
        // ---------------------------------------------------------------------

        // Unregistering the service must decrement the use count of the
        // service.

        expectations.add(new OrderedExpectations() {
            public void add() {

                pluginManagerMock.expects.pluginUnregistering(referenceMock)
                        .description("Unregistering Service");

                contextMock.expects.ungetService(referenceMock)
                        .description("Unregistering Decrement Use Count")
                        .returns(false);
            }
        });

        trackerListener.serviceChanged(
                new ServiceEvent(ServiceEvent.UNREGISTERING, referenceMock));
    }
View Full Code Here

        Map<BundleContext, Collection<ListenerInfo>> listeners = new HashMap<BundleContext, Collection<ListenerInfo>>();
        listeners.put(client1BC, Collections.<ListenerInfo>emptyList());

        // Send the event. It should have no effect because the service doens't match the filter
        assertEquals("Precondition", 0, gpc.proxyMap.size());
        geh.event(new ServiceEvent(ServiceEvent.REGISTERED, sref), listeners);
        assertEquals("No proxy should have been created because the service doesn't match the filter", 0, gpc.proxyMap.size());
        assertEquals("Nothing should have been removed from the listeners", 1, listeners.size());

        long service2ID = 887L;
        Dictionary<String, Object> props2 = new Hashtable<String, Object>();
        props2.put(Constants.SERVICE_ID, service2ID);
        props2.put("a", "b");
        props2.put("foo", "bar");
        ServiceReference<?> sref2 = mockServiceReference(props2);
        ServiceEvent se2 = new ServiceEvent(ServiceEvent.REGISTERED, sref2);

        // Send the event to the system bundle and the bundle that contains the hook, should have no effect
        Map<BundleContext, Collection<ListenerInfo>> listeners2 = new Hashtable<BundleContext, Collection<ListenerInfo>>();
        listeners2.put(frameworkBC, Collections.<ListenerInfo>emptyList());
        listeners2.put(hookBC, Collections.<ListenerInfo>emptyList());
        geh.event(se2, listeners2);
        assertEquals("No proxies to be created for the hook bundle or the system bundle", 0, gpc.proxyMap.size());
        assertEquals("Nothing should have been removed from the listeners", 2, listeners2.size());

        // This is the first time that a proxy actually does get created
        Map<BundleContext, Collection<ListenerInfo>> listeners3 = new HashMap<BundleContext, Collection<ListenerInfo>>();
        listeners3.put(client1BC, Collections.<ListenerInfo>emptyList());
        geh.event(se2, listeners3);
        assertEquals("The service should be hidden from these listeners", 0, listeners3.size());
        assertEquals("Proxy should have been created for this client", 1, gpc.proxyMap.size());
        assertEquals(new Long(service2ID), gpc.proxyMap.keySet().iterator().next());

        // Update the service, now an additional client is interested
        props2.put("a", "c"); // Will change the properties of sref
        Map<BundleContext, Collection<ListenerInfo>> listeners4 = new HashMap<BundleContext, Collection<ListenerInfo>>();

        BundleContext client2BC = mockBundleContext(11);
        listeners4.put(client2BC, Collections.<ListenerInfo>emptyList());
        listeners4.put(client1BC, Collections.<ListenerInfo>emptyList());
        geh.event(new ServiceEvent(ServiceEvent.MODIFIED, sref2), listeners4);
        assertEquals("The service should be hidden from these listeners", 0, listeners4.size());
        assertEquals("There should not be an additional proxy for client 2", 1, gpc.proxyMap.size());
        assertNotNull(gpc.proxyMap.get(service2ID));

        long service3ID = 1L;
        Dictionary<String, Object> props3 = new Hashtable<String, Object>();
        props3.put(Constants.SERVICE_ID, service3ID);
        props3.put("foo", "bar");
        ServiceReference<?> sref3 = mockServiceReference(props3);

        // An event for a new service
        Map<BundleContext, Collection<ListenerInfo>> listeners5 = new HashMap<BundleContext, Collection<ListenerInfo>>();
        listeners5.put(client1BC, Collections.<ListenerInfo>emptyList());
        listeners5.put(client1BC, Collections.<ListenerInfo>emptyList()); // Should be ignored
        geh.event(new ServiceEvent(ServiceEvent.REGISTERED, sref3), listeners5);
        assertEquals("There should be an additional procy for client1 to the new service", 2, gpc.proxyMap.size());
        assertEquals("The service should be hidden from these listeners", 0, listeners5.size());
        assertNotNull(gpc.proxyMap.get(service2ID));
        assertNotNull(gpc.proxyMap.get(service3ID));
    }
View Full Code Here

        Map<BundleContext, Collection<ListenerInfo>> listeners = new HashMap<BundleContext, Collection<ListenerInfo>>();
        listeners.put(client1BC, Collections.<ListenerInfo>emptyList());

        // Send the event. It should have no effect because the service is already a proxy for the client
        assertEquals("Precondition", 0, gpc.proxyMap.size());
        geh.event(new ServiceEvent(ServiceEvent.REGISTERED, sref), listeners);
        assertEquals("No changes expected for the proxy map.", 0, gpc.proxyMap.size());
        assertEquals("The event should be delivered to the client", 1, listeners.size());

        // send the event to a different client, this client should also see the event as the proxy Service Factory is shared
        Map<BundleContext, Collection<ListenerInfo>> listeners2 = new HashMap<BundleContext, Collection<ListenerInfo>>();
        listeners2.put(mockBundleContext(51L), Collections.<ListenerInfo>emptyList());
        geh.event(new ServiceEvent(ServiceEvent.REGISTERED, sref), listeners2);
        assertEquals("No changes expected for the proxy map.", 0, gpc.proxyMap.size());
        assertEquals("The event should be delivered to the client", 1, listeners2.size());
    }
View Full Code Here

        gpc.createProxyQueue.put(new MockCreateProxyRunnable(originalServiceID));
        gpc.createProxyQueue.put(new MockCreateProxyRunnable(777));
        assertEquals("Precondition", 2, gpc.createProxyQueue.size());

        gpc.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, originalRef));
        assertEquals("Registered events should be ignored", 2, gpc.proxyMap.size());
        assertEquals("Registered events should be ignored", 2, gpc.createProxyQueue.size());

        Hashtable<String, Object> proxyProps = new Hashtable<String, Object>(props);
        proxyProps.put(GuardProxyCatalog.PROXY_SERVICE_KEY, Boolean.TRUE);
        ServiceReference<?> proxyRef = mockServiceReference(proxyProps);
        gpc.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, proxyRef));
        assertEquals("Unregistering the proxy should be ignored by the listener", 2, gpc.proxyMap.size());
        assertEquals("Unregistering the proxy should be ignored by the listener", 2, gpc.createProxyQueue.size());

        gpc.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, originalRef));
        assertEquals("The proxy for this service should have been removed", 1, gpc.proxyMap.size());
        assertEquals(anotherRef.getProperty(Constants.SERVICE_ID), gpc.proxyMap.keySet().iterator().next());
        assertEquals("The create proxy job for this service should have been removed", 1, gpc.createProxyQueue.size());
        assertEquals(777, gpc.createProxyQueue.iterator().next().getOriginalServiceID());
View Full Code Here

                assertTrue(!GuardProxyCatalog.PROXY_CREATOR_THREAD_NAME.equals(t.getName()));
            }
        }

        // make the proxy manager appear
        pmListenerHolder[0].serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, pmSref));
        Thread.sleep(400); // give the system some time to send the events...

        Thread ourThread = null;
        Thread[] tarray2 = new Thread[Thread.activeCount()];
        Thread.enumerate(tarray2);
        for (Thread t : tarray2) {
            if (t != null) {
                if (t.getName().equals(GuardProxyCatalog.PROXY_CREATOR_THREAD_NAME)) {
                    ourThread = t;
                }
            }
        }
        assertNotNull(ourThread);
        assertTrue(ourThread.isDaemon());
        assertTrue(ourThread.isAlive());
        assertNotNull(holder.registration);

        assertEquals(0, gpc.createProxyQueue.size());

        int numProxyThreads = 0;
        pmListenerHolder[0].serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, pmSref2));
        Thread.sleep(300); // give the system some time to send the events...

        Thread[] tarray3 = new Thread[Thread.activeCount()];
        Thread.enumerate(tarray3);
        for (Thread t : tarray3) {
            if (t != null) {
                if (t.getName().equals(GuardProxyCatalog.PROXY_CREATOR_THREAD_NAME)) {
                    numProxyThreads++;
                }
            }
        }
        assertEquals("Maximum 1 proxy thread, even if there is more than 1 proxy service", 1, numProxyThreads);

        // Clean up thread
        pmListenerHolder[0].serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, pmSref));
        Thread.sleep(300); // Give the system some time to stop the threads...
        Thread[] tarray4 = new Thread[Thread.activeCount()];
        Thread.enumerate(tarray4);
        for (Thread t : tarray4) {
            if (t != null) {
View Full Code Here

        // now change the original service and let the proxy react
        serviceProps.put("test", "property");
        assertEquals("Precondition, the mocked reference should have picked up this change",
                "property", sr.getProperty("test"));

        gpc.serviceChanged(new ServiceEvent(ServiceEvent.MODIFIED, sr));
        assertEquals("Changing the service should not change the number of proxies", 1, gpc.proxyMap.size());

        for (String key : serviceProps.keySet()) {
            if (GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY.equals(key)) {
                assertEquals(new HashSet(Arrays.asList("role.1", "role.2")), reg.getReference().getProperty(key));
View Full Code Here

        // now change the original service and let the proxy react
        serviceProps.put(GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY, "foobar");
        assertEquals("Precondition, the mocked reference should have picked up this change",
                "foobar", sr.getProperty(GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY));

        gpc.serviceChanged(new ServiceEvent(ServiceEvent.MODIFIED, sr));
        assertEquals("Changing the service should not change the number of proxies", 1, gpc.proxyMap.size());

        assertFalse("The roles property set on the modified service should have been removed",
                Arrays.asList(reg.getReference().getPropertyKeys()).contains(GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY));
        assertEquals(Boolean.TRUE, reg.getReference().getProperty(GuardProxyCatalog.PROXY_SERVICE_KEY));
View Full Code Here

                ServiceReference<?> cdRef = EasyMock.createMock(ServiceReference.class);
                EasyMock.expect(cdRef.getProperty(CommandProcessor.COMMAND_SCOPE)).andReturn("foo");
                EasyMock.expect(cdRef.getProperty(CommandProcessor.COMMAND_FUNCTION)).andReturn("bar");
                EasyMock.replay(cdRef);

                ServiceEvent event = new ServiceEvent(ServiceEvent.REGISTERED, cdRef);
                commandListener.serviceChanged(event);
                assertEquals(4, scp.getCommands().size());
                assertTrue(scp.getCommands().contains("foo:bar"));

                ServiceReference<?> cd2Ref = EasyMock.createMock(ServiceReference.class);
                EasyMock.expect(cd2Ref.getProperty(CommandProcessor.COMMAND_SCOPE)).andReturn("xxx");
                EasyMock.expect(cd2Ref.getProperty(CommandProcessor.COMMAND_FUNCTION)).andReturn(
                        new String[] {"aaa", "bbb"});
                EasyMock.replay(cd2Ref);

                ServiceEvent event2 = new ServiceEvent(ServiceEvent.REGISTERED, cd2Ref);
                commandListener.serviceChanged(event2);
                assertEquals(6, scp.getCommands().size());
                assertTrue(scp.getCommands().contains("xxx:aaa"));
                assertTrue(scp.getCommands().contains("xxx:bbb"));
View Full Code Here

                EasyMock.expect(cdRef.getProperty(CommandProcessor.COMMAND_SCOPE)).andReturn("foo");
                EasyMock.expect(cdRef.getProperty(CommandProcessor.COMMAND_FUNCTION)).andReturn("bar");
                EasyMock.expect(cdRef.getProperty("service.ranking")).andReturn("1");
                EasyMock.replay(cdRef);

                ServiceEvent event = new ServiceEvent(ServiceEvent.REGISTERED, cdRef);
                commandListener.serviceChanged(event);
                assertEquals(4, scp.getCommands().size());
                assertTrue(scp.getCommands().contains("foo:bar"));

                ServiceReference<?> cd2Ref = EasyMock.createMock(ServiceReference.class);
                EasyMock.expect(cd2Ref.getProperty(CommandProcessor.COMMAND_SCOPE)).andReturn("xxx");
                EasyMock.expect(cd2Ref.getProperty(CommandProcessor.COMMAND_FUNCTION)).andReturn(
                        new String[] {"aaa", "bbb"});
                EasyMock.expect(cd2Ref.getProperty("service.ranking")).andReturn("2");
                EasyMock.replay(cd2Ref);

                ServiceEvent event2 = new ServiceEvent(ServiceEvent.REGISTERED, cd2Ref);
                commandListener.serviceChanged(event2);
                assertEquals(6, scp.getCommands().size());
                assertTrue(scp.getCommands().contains("xxx:aaa"));
                assertTrue(scp.getCommands().contains("xxx:bbb"));
View Full Code Here

TOP

Related Classes of org.osgi.framework.ServiceEvent

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.