Search in sources :

Example 21 with ServiceRegistration

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

the class ProviderBundleTrackerCustomizerGenericCapabilityTest method testCustomAttributesBundle.

@Test
public void testCustomAttributesBundle() throws Exception {
    Bundle mediatorBundle = EasyMock.createMock(Bundle.class);
    EasyMock.expect(mediatorBundle.getBundleId()).andReturn(42l).anyTimes();
    EasyMock.replay(mediatorBundle);
    BaseActivator activator = new BaseActivator() {

        @Override
        public void start(BundleContext context) throws Exception {
        }
    };
    ProviderBundleTrackerCustomizer customizer = new ProviderBundleTrackerCustomizer(activator, mediatorBundle);
    ServiceRegistration sreg = EasyMock.createMock(ServiceRegistration.class);
    EasyMock.replay(sreg);
    BundleContext implBC = mockSPIBundleContext(sreg);
    Dictionary<String, String> headers = new Hashtable<String, String>();
    headers.put(SpiFlyConstants.REQUIRE_CAPABILITY, SpiFlyConstants.PROVIDER_REQUIREMENT);
    headers.put(SpiFlyConstants.PROVIDE_CAPABILITY, SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "; " + SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "=org.apache.aries.mytest.MySPI; approval=yeah; ");
    Bundle implBundle = mockSPIBundle(implBC, headers);
    List<ServiceRegistration> registrations = customizer.addingBundle(implBundle, null);
    assertEquals(1, registrations.size());
    Collection<Bundle> bundles = activator.findProviderBundles("org.apache.aries.mytest.MySPI");
    assertEquals(1, bundles.size());
    assertSame(implBundle, bundles.iterator().next());
    Map<String, Object> attrs = activator.getCustomBundleAttributes("org.apache.aries.mytest.MySPI", implBundle);
    assertEquals(1, attrs.size());
    assertEquals("yeah", attrs.get("approval"));
}
Also used : Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 22 with ServiceRegistration

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

the class ProviderBundleTrackerCustomizerTest method testAddingRemovedBundle.

@Test
public void testAddingRemovedBundle() throws Exception {
    Bundle mediatorBundle = EasyMock.createMock(Bundle.class);
    EasyMock.expect(mediatorBundle.getBundleId()).andReturn(42l).anyTimes();
    EasyMock.replay(mediatorBundle);
    BaseActivator activator = new BaseActivator() {

        @Override
        public void start(BundleContext context) throws Exception {
        }
    };
    ProviderBundleTrackerCustomizer customizer = new ProviderBundleTrackerCustomizer(activator, mediatorBundle);
    ServiceRegistration sreg = EasyMock.createMock(ServiceRegistration.class);
    sreg.unregister();
    EasyMock.expectLastCall();
    EasyMock.replay(sreg);
    BundleContext implBC = mockSPIBundleContext(sreg);
    Bundle implBundle = mockSPIBundle(implBC);
    assertEquals("Precondition", 0, activator.findProviderBundles("org.apache.aries.mytest.MySPI").size());
    // Call addingBundle();
    List<ServiceRegistration> registrations = customizer.addingBundle(implBundle, null);
    Collection<Bundle> bundles = activator.findProviderBundles("org.apache.aries.mytest.MySPI");
    assertEquals(1, bundles.size());
    assertSame(implBundle, bundles.iterator().next());
    // The bc.registerService() call should now have been made
    EasyMock.verify(implBC);
    // Call removedBundle();
    customizer.removedBundle(implBundle, null, registrations);
    // sreg.unregister() should have been called.
    EasyMock.verify(sreg);
}
Also used : Bundle(org.osgi.framework.Bundle) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 23 with ServiceRegistration

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

the class MBeanServerTest method test_MServerBean.

@Test
public void test_MServerBean() throws Exception {
    final String instanceName = "simple.test.instance";
    final String objectNameString = "domain:instance=" + instanceName;
    final ObjectName objectName = new ObjectName(objectNameString);
    final TestClass testInstance = new TestClass(instanceName);
    // get or create the dynamic MBean Server
    final MBeanServer server = getOrCreateMBeanServer();
    // MBean server not registered as service, unknown object
    assertNotRegistered(server, objectName);
    // expect the MBean to be registered with the static server
    final ServiceRegistration mBeanReg = registerService(TestClassMBean.class.getName(), testInstance, objectNameString);
    // MBean server not registered, expect object to not be known
    assertNotRegistered(server, objectName);
    // register MBean server, expect MBean registered
    ServiceRegistration mBeanServerReg = registerMBeanServer(server);
    assertRegistered(server, objectName);
    // expect MBean to return expected value
    TestCase.assertEquals(instanceName, server.getAttribute(objectName, "InstanceName"));
    // unregister MBean server, expect MBean to be unregistered
    mBeanServerReg.unregister();
    assertNotRegistered(server, objectName);
    // unregister MBean, expect to not be registered any more
    mBeanReg.unregister();
    assertNotRegistered(server, objectName);
}
Also used : TestClassMBean(org.apache.aries.jmx.whiteboard.integration.helper.TestClassMBean) TestClass(org.apache.aries.jmx.whiteboard.integration.helper.TestClass) ObjectName(javax.management.ObjectName) MBeanServer(javax.management.MBeanServer) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 24 with ServiceRegistration

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

the class MBeanTest method test_simple_MBean_different_package.

@Test
public void test_simple_MBean_different_package() throws Exception {
    final String instanceName = "simple.test.instance.2";
    final String objectNameString = "domain:instance=" + instanceName;
    final ObjectName objectName = new ObjectName(objectNameString);
    final TestClass testInstance = new TestClass2(instanceName);
    final MBeanServer server = getStaticMBeanServer();
    // expect MBean to not be registered yet
    assertNotRegistered(server, objectName);
    // expect the MBean to be registered with the static server
    final ServiceRegistration reg = registerService(TestClassMBean.class.getName(), testInstance, objectNameString);
    assertRegistered(server, objectName);
    // expect MBean to return expected value
    TestCase.assertEquals(instanceName, server.getAttribute(objectName, "InstanceName"));
    // unregister MBean, expect to not be registered any more
    reg.unregister();
    assertNotRegistered(server, objectName);
}
Also used : TestClassMBean(org.apache.aries.jmx.whiteboard.integration.helper.TestClassMBean) TestClass(org.apache.aries.jmx.whiteboard.integration.helper.TestClass) TestClass2(org.apache.aries.jmx.whiteboard.integration.helper2.TestClass2) ObjectName(javax.management.ObjectName) MBeanServer(javax.management.MBeanServer) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 25 with ServiceRegistration

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

the class ManagedObjectManager method register.

public synchronized void register(ManagedObject cm, Properties props) {
    String key = cm.getPersistentId();
    ConfigurationWatcher reg = map.get(key);
    if (reg == null) {
        reg = new ConfigurationWatcher();
        ServiceRegistration registration = cm.getBundle().getBundleContext().registerService(ManagedService.class.getName(), reg, (Dictionary) props);
        reg.setRegistration(registration);
        map.put(key, reg);
    }
    reg.add(cm);
    try {
        Dictionary<String, Object> config = CmUtils.getProperties(reg.getRegistration().getReference(), key);
        cm.updated(config);
    } catch (Throwable t) {
    // Ignore
    }
}
Also used : ManagedService(org.osgi.service.cm.ManagedService) 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