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"));
}
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);
}
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);
}
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);
}
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
}
}
Aggregations