use of org.opennms.core.soa.Registration in project opennms by OpenNMS.
the class ReferenceListFactoryBeanTest method testListListeners.
@Test
public void testListListeners() throws Exception {
ServiceRegistry registry = new DefaultServiceRegistry();
Registration reg1 = registry.register(new MyProvider("prov1"), Hello.class, Goodbye.class);
Registration reg2 = registry.register(new MyProvider("prov2"), Hello.class, Goodbye.class);
ReferenceListFactoryBean<Hello> bean = new ReferenceListFactoryBean<>();
bean.setServiceInterface(Hello.class);
bean.setServiceRegistry(registry);
CountingListener listener = new CountingListener();
bean.addListener(listener);
bean.afterPropertiesSet();
assertEquals(2, listener.getTotalProvided());
Registration reg3 = registry.register(new MyProvider("prov3"), Hello.class, Goodbye.class);
assertEquals(3, listener.getTotalProvided());
reg2.unregister();
assertEquals(2, listener.getTotalProvided());
reg1.unregister();
reg3.unregister();
assertEquals(0, listener.getTotalProvided());
}
use of org.opennms.core.soa.Registration in project opennms by OpenNMS.
the class RegistrationListenerBeanTest method testCallBindUnbindMethods.
@Test
public void testCallBindUnbindMethods() throws Exception {
RegistrationListenerBean<Hello> listener = new RegistrationListenerBean<>();
listener.setServiceInterface(Hello.class);
listener.setTarget(this);
listener.setBindMethod("bind");
listener.setUnbindMethod("unbind");
listener.afterPropertiesSet();
ServiceRegistry registry = new DefaultServiceRegistry();
Registration reg1 = registry.register(new MyProvider("prov1"), Hello.class, Goodbye.class);
Registration reg2 = registry.register(new MyProvider("prov2"), Hello.class, Goodbye.class);
ReferenceListFactoryBean<Hello> bean = new ReferenceListFactoryBean<>();
bean.setServiceInterface(Hello.class);
bean.setServiceRegistry(registry);
bean.addListener(listener);
bean.afterPropertiesSet();
assertEquals(2, getTotalProvided());
Registration reg3 = registry.register(new MyProvider("prov3"), Hello.class, Goodbye.class);
assertEquals(3, getTotalProvided());
reg2.unregister();
assertEquals(2, getTotalProvided());
reg1.unregister();
reg3.unregister();
assertEquals(0, getTotalProvided());
}
use of org.opennms.core.soa.Registration in project opennms by OpenNMS.
the class NamespaceHandlerTest method testFilteredReferenceListBeanDefinition.
@Test
@DirtiesContext
public void testFilteredReferenceListBeanDefinition() {
assertNotNull(m_bigGoodbyeList);
int expected = m_bigGoodbyeList.size() + 1;
Map<String, String> bigProps = new HashMap<String, String>();
bigProps.put("size", "big");
Registration bigRegistration = m_defaultServiceRegistry.register(new MyProvider("alsoBig"), bigProps, Goodbye.class);
Map<String, String> props = new HashMap<String, String>();
props.put("size", "small");
Registration smallRegistration = m_defaultServiceRegistry.register(new MyProvider("alsoSmall"), props, Goodbye.class);
assertEquals(expected, m_bigGoodbyeList.size());
expected = m_bigGoodbyeList.size() - 1;
bigRegistration.unregister();
smallRegistration.unregister();
assertEquals(expected, m_bigGoodbyeList.size());
}
use of org.opennms.core.soa.Registration in project opennms by OpenNMS.
the class NamespaceHandlerTest method testReferenceListBeanDefinition.
@Test
@DirtiesContext
public void testReferenceListBeanDefinition() {
assertNotNull(m_helloList);
int expected = m_helloList.size() + 1;
Registration registration = m_defaultServiceRegistry.register(new MyProvider(), Hello.class);
assertEquals(expected, m_helloList.size());
expected = m_helloList.size() - 1;
registration.unregister();
assertEquals(expected, m_helloList.size());
}
use of org.opennms.core.soa.Registration in project opennms by OpenNMS.
the class OnmsOSGiBridgeActivator method registerWithOnmsRegistry.
private void registerWithOnmsRegistry(final ServiceReference<?> reference) {
LOG.debug("registerWithOnmsRegistry: {}", reference.getBundle());
// skip this service if this should not be exported
if (!isOnmsExported(reference))
return;
// skip this service if its came from the opennms registry originally
if (isOnmsSource(reference))
return;
// if this service is already registered then skip it
if (m_osgiReference2onmsRegistrationMap.containsKey(reference))
return;
final BundleContext bundleContext = m_bundleContext.get();
if (bundleContext == null) {
LOG.warn("No BundleContext found, skipping registration of services: {}", reference);
return;
}
final String[] classNames = (String[]) reference.getProperty(Constants.OBJECTCLASS);
try {
final Class<?>[] providerInterfaces = findClasses(classNames);
final Object provider = bundleContext.getService(reference);
final Map<String, String> properties = new LinkedHashMap<String, String>();
for (final String key : reference.getPropertyKeys()) {
final Object val = reference.getProperty(key);
final StringBuilder buf = new StringBuilder();
if (val instanceof Object[]) {
final Object[] a = (Object[]) val;
for (int i = 0; i < a.length; i++) {
if (i != 0)
buf.append(',');
buf.append(a[i]);
}
} else {
buf.append(val);
}
properties.put(key, buf.toString());
}
properties.put(REGISTRATION_SOURCE, OSGI_SOURCE);
LOG.debug("OnmsOSGiBridgeActivator: registering...");
final Registration onmsRegistration = getRegistry().register(provider, properties, providerInterfaces);
LOG.debug("OnmsOSGiBridgeActivator: registry = {}", getRegistry());
m_osgiReference2onmsRegistrationMap.put(reference, onmsRegistration);
LOG.info("OnmsOSGiBridgeActivator: registered provider {} for interfaces: {} with properties: {}", provider, Arrays.toString(providerInterfaces), properties);
} catch (final ClassNotFoundException e) {
LOG.warn("OnmsOSGiBridgeActivator: Unable to find class used by exported OSGi service", e);
}
}
Aggregations