Search in sources :

Example 16 with ComponentMetadata

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());
}
Also used : Field(java.lang.reflect.Field) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Bundle(org.osgi.framework.Bundle) ComponentMethodsImpl(org.apache.felix.scr.impl.inject.ComponentMethodsImpl) ComponentMetadata(org.apache.felix.scr.impl.metadata.ComponentMetadata) ServiceRegistration(org.osgi.framework.ServiceRegistration) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 17 with ComponentMetadata

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;
}
Also used : Bundle(org.osgi.framework.Bundle) ComponentException(org.osgi.service.component.ComponentException) ComponentMetadata(org.apache.felix.scr.impl.metadata.ComponentMetadata)

Aggregations

ComponentMetadata (org.apache.felix.scr.impl.metadata.ComponentMetadata)17 ComponentContainer (org.apache.felix.scr.impl.manager.ComponentContainer)3 SingleComponentManager (org.apache.felix.scr.impl.manager.SingleComponentManager)3 Bundle (org.osgi.framework.Bundle)3 BundleContext (org.osgi.framework.BundleContext)3 IOException (java.io.IOException)2 Field (java.lang.reflect.Field)2 Dictionary (java.util.Dictionary)2 Hashtable (java.util.Hashtable)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ComponentMethodsImpl (org.apache.felix.scr.impl.inject.ComponentMethodsImpl)2 TargetedPID (org.apache.felix.scr.impl.metadata.TargetedPID)2 BaseObject (org.apache.felix.scr.impl.metadata.instances.BaseObject)2 Level1Object (org.apache.felix.scr.impl.metadata.instances.Level1Object)2 Level3Object (org.apache.felix.scr.impl.metadata.instances.Level3Object)2 Level2Object (org.apache.felix.scr.impl.metadata.instances2.Level2Object)2 Test (org.junit.Test)2 ServiceRegistration (org.osgi.framework.ServiceRegistration)2 ComponentException (org.osgi.service.component.ComponentException)2 BufferedReader (java.io.BufferedReader)1