Search in sources :

Example 6 with ServiceRegistration

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

the class ServiceRegistryContextTest method checkProxyDynamism.

@Test
public void checkProxyDynamism() throws NamingException {
    BundleMock mock = new BundleMock("scooby.doo", new Properties());
    Thread.currentThread().setContextClassLoader(mock.getClassLoader());
    InitialContext ctx = new InitialContext();
    String className = Runnable.class.getName();
    Runnable t = Skeleton.newMock(Runnable.class);
    Runnable t2 = Skeleton.newMock(Runnable.class);
    // we don't want the default service
    reg.unregister();
    ServiceRegistration reg = bc.registerService(className, t, null);
    bc.registerService(className, t2, null);
    Runnable r = (Runnable) ctx.lookup("osgi:service/java.lang.Runnable");
    r.run();
    Skeleton.getSkeleton(t).assertCalledExactNumberOfTimes(new MethodCall(Runnable.class, "run"), 1);
    Skeleton.getSkeleton(t2).assertNotCalled(new MethodCall(Runnable.class, "run"));
    reg.unregister();
    r.run();
    Skeleton.getSkeleton(t).assertCalledExactNumberOfTimes(new MethodCall(Runnable.class, "run"), 1);
    Skeleton.getSkeleton(t2).assertCalledExactNumberOfTimes(new MethodCall(Runnable.class, "run"), 1);
}
Also used : BundleMock(org.apache.aries.mocks.BundleMock) Properties(java.util.Properties) MethodCall(org.apache.aries.unittest.mocks.MethodCall) InitialContext(javax.naming.InitialContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 7 with ServiceRegistration

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

the class ParserServiceImportAndIncludeXSDsTest method waitForConfig.

private boolean waitForConfig() throws InterruptedException {
    final AtomicBoolean ready = new AtomicBoolean();
    @SuppressWarnings("rawtypes") ServiceRegistration reg = context().registerService(BlueprintListener.class, new BlueprintListener() {

        @Override
        public void blueprintEvent(BlueprintEvent event) {
            if (TEST_BUNDLE.equals(event.getBundle().getSymbolicName()) && BlueprintEvent.CREATED == event.getType()) {
                synchronized (ready) {
                    ready.set(true);
                    ready.notify();
                }
            }
        }
    }, null);
    try {
        synchronized (ready) {
            if (!ready.get()) {
                ready.wait(3000);
            }
        }
        return ready.get();
    } finally {
        reg.unregister();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BlueprintListener(org.osgi.service.blueprint.container.BlueprintListener) BlueprintEvent(org.osgi.service.blueprint.container.BlueprintEvent) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 8 with ServiceRegistration

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

the class ParserServiceImportCmAndIncorrectNamespaceHandlersTest method waitForConfig.

private void waitForConfig() throws InterruptedException {
    final CountDownLatch ready = new CountDownLatch(2);
    final AtomicBoolean failure = new AtomicBoolean(false);
    @SuppressWarnings("rawtypes") ServiceRegistration reg = context().registerService(BlueprintListener.class, new BlueprintListener() {

        @Override
        public void blueprintEvent(BlueprintEvent event) {
            if (NS_HANDLER_BUNDLE.equals(event.getBundle().getSymbolicName()) && BlueprintEvent.CREATED == event.getType()) {
                ready.countDown();
            } else if (TEST_BUNDLE.equals(event.getBundle().getSymbolicName()) && (BlueprintEvent.CREATED == event.getType() || BlueprintEvent.FAILURE == event.getType())) {
                ready.countDown();
                if (BlueprintEvent.FAILURE == event.getType()) {
                    failure.set(true);
                }
            }
        }
    }, null);
    try {
        assertTrue(ready.await(3000, TimeUnit.MILLISECONDS));
        assertFalse("org.apache.aries.blueprint.aries1503.test bundle should successfully start Blueprint container", failure.get());
    } finally {
        reg.unregister();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BlueprintListener(org.osgi.service.blueprint.container.BlueprintListener) CountDownLatch(java.util.concurrent.CountDownLatch) BlueprintEvent(org.osgi.service.blueprint.container.BlueprintEvent) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 9 with ServiceRegistration

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

the class ServiceStateMBeanTest method testAttributeChangeNotifications.

@Test
public void testAttributeChangeNotifications() throws Exception {
    final List<AttributeChangeNotification> attributeChanges = new ArrayList<AttributeChangeNotification>();
    mbeanServer.addNotificationListener(objectName, new NotificationListener() {

        public void handleNotification(Notification notification, Object handback) {
            if (notification instanceof AttributeChangeNotification) {
                attributeChanges.add((AttributeChangeNotification) notification);
            }
        }
    }, null, null);
    assertEquals("Precondition", 0, attributeChanges.size());
    long[] idsWithout = mbean.getServiceIds();
    String svc = "A String Service";
    ServiceRegistration reg = bundleContext.registerService(String.class.getName(), svc, null);
    long id = (Long) reg.getReference().getProperty(Constants.SERVICE_ID);
    long[] idsWith = new long[idsWithout.length + 1];
    System.arraycopy(idsWithout, 0, idsWith, 0, idsWithout.length);
    idsWith[idsWith.length - 1] = id;
    Arrays.sort(idsWith);
    waitForListToReachSize(attributeChanges, 1);
    AttributeChangeNotification ac = attributeChanges.get(0);
    assertEquals("ServiceIds", ac.getAttributeName());
    long seq1 = ac.getSequenceNumber();
    assertTrue(Arrays.equals(idsWithout, (long[]) ac.getOldValue()));
    assertTrue(Arrays.equals(idsWith, (long[]) ac.getNewValue()));
    Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put("somekey", "someval");
    reg.setProperties(props);
    // Setting the properties updates the service registration, however it should not cause the attribute notification
    // Give the system a bit of time to send potential notifications
    Thread.sleep(500);
    assertEquals("Changing the service registration should not cause an attribute notification", 1, attributeChanges.size());
    reg.unregister();
    waitForListToReachSize(attributeChanges, 2);
    AttributeChangeNotification ac2 = attributeChanges.get(1);
    assertEquals("ServiceIds", ac2.getAttributeName());
    assertEquals(seq1 + 1, ac2.getSequenceNumber());
    assertTrue(Arrays.equals(idsWith, (long[]) ac2.getOldValue()));
    assertTrue(Arrays.equals(idsWithout, (long[]) ac2.getNewValue()));
}
Also used : AttributeChangeNotification(javax.management.AttributeChangeNotification) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) AttributeChangeNotification(javax.management.AttributeChangeNotification) Notification(javax.management.Notification) NotificationListener(javax.management.NotificationListener) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test) AbstractIntegrationTest(org.apache.aries.jmx.AbstractIntegrationTest)

Example 10 with ServiceRegistration

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

the class Activator method stop.

public void stop(BundleContext ctx) {
    tm.close();
    t.close();
    if (nshReg != null) {
        for (ServiceRegistration reg : nshReg) {
            safeUnregisterService(reg);
        }
    }
}
Also used : ServiceRegistration(org.osgi.framework.ServiceRegistration)

Aggregations

ServiceRegistration (org.osgi.framework.ServiceRegistration)188 Test (org.junit.Test)80 Hashtable (java.util.Hashtable)61 BundleContext (org.osgi.framework.BundleContext)35 Bundle (org.osgi.framework.Bundle)26 Dictionary (java.util.Dictionary)23 Properties (java.util.Properties)22 HashMap (java.util.HashMap)21 ServiceReference (org.osgi.framework.ServiceReference)21 ArrayList (java.util.ArrayList)18 Map (java.util.Map)13 IOException (java.io.IOException)8 ServiceFactory (org.osgi.framework.ServiceFactory)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 URL (java.net.URL)6 HashSet (java.util.HashSet)6 List (java.util.List)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 ObjectName (javax.management.ObjectName)5 InitialContext (javax.naming.InitialContext)5