use of org.apache.felix.scr.impl.metadata.ComponentMetadata in project felix by apache.
the class SingleComponentManagerTest method testGetServiceWithNullComponentContext.
@Test
public void testGetServiceWithNullComponentContext() throws Exception {
ComponentMetadata cm = new ComponentMetadata(DSVersion.DS13);
cm.setImplementationClassName("foo.bar.SomeClass");
cm.validate(null);
@SuppressWarnings("unchecked") ComponentContainer<Object> cc = Mockito.mock(ComponentContainer.class);
Mockito.when(cc.getComponentMetadata()).thenReturn(cm);
Mockito.when(cc.getActivator()).thenReturn(componentActivator);
SingleComponentManager<?> scm = new SingleComponentManager<Object>(cc, new ComponentMethodsImpl()) {
@Override
boolean getServiceInternal(ServiceRegistration<Object> serviceRegistration) {
return true;
}
};
BundleContext bc = Mockito.mock(BundleContext.class);
Bundle b = Mockito.mock(Bundle.class);
Mockito.when(b.getBundleContext()).thenReturn(bc);
scm.setState(scm.getState(), State.unsatisfiedReference);
assertNull("m_componentContext is null, this should not cause an NPE", scm.getService(b, serviceRegistration));
Field u = SingleComponentManager.class.getDeclaredField("m_useCount");
u.setAccessible(true);
AtomicInteger use = (AtomicInteger) u.get(scm);
assertEquals(0, use.get());
}
use of org.apache.felix.scr.impl.metadata.ComponentMetadata in project felix by apache.
the class ComponentRegistry method checkComponentName.
// ---------- ComponentHolder registration by component name
/**
* Checks whether the component name is "globally" unique or not. If it is
* unique, it is reserved until the actual component is registered with
* {@link #registerComponentHolder(String, ComponentHolder)} or until
* it is unreserved by calling {@link #unregisterComponentHolder(String)}.
* If a component with the same name has already been reserved or registered
* a ComponentException is thrown with a descriptive message.
*
* @param bundle the bundle registering the component
* @param name the component name to check and reserve
* @throws ComponentException if the name is already in use by another
* component.
*/
final ComponentRegistryKey checkComponentName(final Bundle bundle, final String name) {
// register the name if no registration for that name exists already
final ComponentRegistryKey key = new ComponentRegistryKey(bundle, name);
ComponentHolder<?> existingRegistration = null;
boolean present;
synchronized (m_componentHoldersByName) {
present = m_componentHoldersByName.containsKey(key);
if (!present) {
m_componentHoldersByName.put(key, null);
} else {
existingRegistration = m_componentHoldersByName.get(key);
}
}
// existing registration to provide more information if possible
if (present) {
String message = "The component name '" + name + "' has already been registered";
if (existingRegistration != null) {
Bundle cBundle = existingRegistration.getActivator().getBundleContext().getBundle();
ComponentMetadata cMeta = existingRegistration.getComponentMetadata();
StringBuffer buf = new StringBuffer(message);
buf.append(" by Bundle ").append(cBundle.getBundleId());
if (cBundle.getSymbolicName() != null) {
buf.append(" (").append(cBundle.getSymbolicName()).append(")");
}
buf.append(" as Component of Class ").append(cMeta.getImplementationClassName());
message = buf.toString();
}
throw new ComponentException(message);
}
return key;
}
Aggregations