Search in sources :

Example 1 with BeanMetadata

use of org.osgi.service.blueprint.reflect.BeanMetadata in project camel by apache.

the class BlueprintContainerRegistry method lookupByType.

public static <T> Map<String, T> lookupByType(BlueprintContainer blueprintContainer, Class<T> type, boolean includeNonSingletons) {
    Bundle bundle = (Bundle) blueprintContainer.getComponentInstance("blueprintBundle");
    Map<String, T> objects = new LinkedHashMap<String, T>();
    Set<String> ids = blueprintContainer.getComponentIds();
    for (String id : ids) {
        try {
            ComponentMetadata metadata = blueprintContainer.getComponentMetadata(id);
            Class<?> cl = null;
            if (metadata instanceof BeanMetadata) {
                BeanMetadata beanMetadata = (BeanMetadata) metadata;
                // should we skip the bean if its prototype and we are only looking for singletons?
                if (!includeNonSingletons) {
                    String scope = beanMetadata.getScope();
                    if (BeanMetadata.SCOPE_PROTOTYPE.equals(scope)) {
                        continue;
                    }
                }
                cl = bundle.loadClass(beanMetadata.getClassName());
            } else if (metadata instanceof ReferenceMetadata) {
                ReferenceMetadata referenceMetadata = (ReferenceMetadata) metadata;
                cl = bundle.loadClass(referenceMetadata.getInterface());
            }
            if (cl != null && type.isAssignableFrom(cl)) {
                Object o = blueprintContainer.getComponentInstance(metadata.getId());
                objects.put(metadata.getId(), type.cast(o));
            }
        } catch (Throwable t) {
        // ignore
        }
    }
    return objects;
}
Also used : BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) Bundle(org.osgi.framework.Bundle) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) ReferenceMetadata(org.osgi.service.blueprint.reflect.ReferenceMetadata) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with BeanMetadata

use of org.osgi.service.blueprint.reflect.BeanMetadata in project aries by apache.

the class AnnotationEnablingNameSpaceHandlerTest method testAnnotationDisabled.

@Test
public void testAnnotationDisabled() throws Exception {
    ComponentDefinitionRegistry cdr = parseCDR("enable-annotations2.xml");
    checkCompTop(cdr);
    BeanMetadata pmd = (BeanMetadata) cdr.getComponentDefinition(TxNamespaceHandler.ANNOTATION_PARSER_BEAN_NAME);
    assertNull(pmd);
}
Also used : ComponentDefinitionRegistry(org.apache.aries.blueprint.ComponentDefinitionRegistry) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) Test(org.junit.Test)

Example 3 with BeanMetadata

use of org.osgi.service.blueprint.reflect.BeanMetadata in project aries by apache.

the class AnnotationEnablingNameSpaceHandlerTest method checkCompTop.

private void checkCompTop(ComponentDefinitionRegistry cdr) {
    BeanMetadata compTop = (BeanMetadata) cdr.getComponentDefinition("top");
    assertNotNull(compTop);
    assertEquals(0, cdr.getInterceptors(compTop).size());
//assertNull(txenhancer.getComponentMethodTxAttribute(compTop, "increment"));
}
Also used : BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata)

Example 4 with BeanMetadata

use of org.osgi.service.blueprint.reflect.BeanMetadata in project aries by apache.

the class AbstractParserProxy method parseCDRForServices.

/**
	   * Extract Service metadata from a ComponentDefinitionRegistry. When doing SCA modelling, we
	   * need to suppress anonymous services. We don't want to do that when we're modelling for 
	   * provisioning dependencies. 
	   * @param cdr                       ComponentDefinitionRegistry
	   * @param suppressAnonymousServices Unnamed services will not be returned if this is true
	   * @return List<WrappedServiceMetadata>
	   */
private List<ExportedService> parseCDRForServices(ComponentDefinitionRegistry cdr, boolean suppressAnonymousServices) {
    _logger.debug(LOG_ENTRY, "parseCDRForServices", new Object[] { cdr, suppressAnonymousServices });
    List<ExportedService> result = new ArrayList<ExportedService>();
    for (ComponentMetadata compMetadata : findAllComponents(cdr)) {
        if (compMetadata instanceof ServiceMetadata) {
            ServiceMetadata serviceMetadata = (ServiceMetadata) compMetadata;
            String serviceName;
            int ranking;
            Collection<String> interfaces = new ArrayList<String>();
            Map<String, Object> serviceProps = new HashMap<String, Object>();
            ranking = serviceMetadata.getRanking();
            for (Object i : serviceMetadata.getInterfaces()) {
                interfaces.add((String) i);
            }
            // get the service properties
            List<MapEntry> props = serviceMetadata.getServiceProperties();
            for (MapEntry entry : props) {
                String key = ((ValueMetadata) entry.getKey()).getStringValue();
                Metadata value = entry.getValue();
                if (value instanceof CollectionMetadata) {
                    processMultiValueProperty(serviceProps, key, value);
                } else {
                    serviceProps.put(key, ((ValueMetadata) entry.getValue()).getStringValue());
                }
            }
            // serviceName: use the service id unless that's not set, 
            // in which case we use the bean id. 
            serviceName = serviceMetadata.getId();
            // If the Service references a Bean, export the bean id as a service property
            // as per 121.6.5 p669 of the blueprint 1.0 specification
            Target t = serviceMetadata.getServiceComponent();
            String targetId = null;
            if (t instanceof RefMetadata) {
                targetId = ((RefMetadata) t).getComponentId();
            } else if (t instanceof BeanMetadata) {
                targetId = ((BeanMetadata) t).getId();
            }
            // or auto-generated for an anonymous service. This must ALWAYS be set. 
            if (targetId != null && !targetId.startsWith(".")) {
                // Don't set this for anonymous inner components
                serviceProps.put("osgi.service.blueprint.compname", targetId);
                if (serviceName == null || serviceName.equals("") || serviceName.startsWith(".")) {
                    serviceName = targetId;
                }
            }
            if (serviceName != null && serviceName.startsWith("."))
                serviceName = null;
            // If suppressAnonymous services, do not expose services that have no name
            if (!suppressAnonymousServices || (serviceName != null)) {
                ExportedService wsm = _modellingManager.getExportedService(serviceName, ranking, interfaces, serviceProps);
                result.add(wsm);
            }
        }
    }
    _logger.debug(LOG_EXIT, "parseAllServiceElements", new Object[] { result });
    return result;
}
Also used : CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) MapEntry(org.osgi.service.blueprint.reflect.MapEntry) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) HashMap(java.util.HashMap) ExportedService(org.apache.aries.application.modelling.ExportedService) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) ArrayList(java.util.ArrayList) CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) Metadata(org.osgi.service.blueprint.reflect.Metadata) WrappedServiceMetadata(org.apache.aries.application.modelling.WrappedServiceMetadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) ServiceReferenceMetadata(org.osgi.service.blueprint.reflect.ServiceReferenceMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) MapMetadata(org.osgi.service.blueprint.reflect.MapMetadata) ReferenceListMetadata(org.osgi.service.blueprint.reflect.ReferenceListMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) Target(org.osgi.service.blueprint.reflect.Target) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) WrappedServiceMetadata(org.apache.aries.application.modelling.WrappedServiceMetadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata)

Example 5 with BeanMetadata

use of org.osgi.service.blueprint.reflect.BeanMetadata in project aries by apache.

the class ParserTest method testParseComponent.

public void testParseComponent() throws Exception {
    ComponentDefinitionRegistry registry = parse("/test-simple-component.xml");
    assertNotNull(registry);
    ComponentMetadata component = registry.getComponentDefinition("pojoA");
    assertNotNull(component);
    assertEquals("pojoA", component.getId());
    assertTrue(component instanceof BeanMetadata);
    BeanMetadata local = (BeanMetadata) component;
    List<String> deps = local.getDependsOn();
    assertNotNull(deps);
    assertEquals(2, deps.size());
    assertTrue(deps.contains("pojoB"));
    assertTrue(deps.contains("pojoC"));
    assertEquals("org.apache.aries.blueprint.pojos.PojoA", local.getClassName());
    List<BeanArgument> params = local.getArguments();
    assertNotNull(params);
    assertEquals(6, params.size());
    BeanArgument param = params.get(0);
    assertNotNull(param);
    assertEquals(-1, param.getIndex());
    assertNull(param.getValueType());
    assertNotNull(param.getValue());
    assertTrue(param.getValue() instanceof ValueMetadata);
    assertEquals("val0", ((ValueMetadata) param.getValue()).getStringValue());
    assertNull(((ValueMetadata) param.getValue()).getType());
    param = params.get(1);
    assertNotNull(param);
    assertEquals(-1, param.getIndex());
    assertNull(param.getValueType());
    assertNotNull(param.getValue());
    assertTrue(param.getValue() instanceof RefMetadata);
    assertEquals("val1", ((RefMetadata) param.getValue()).getComponentId());
    param = params.get(2);
    assertNotNull(param);
    assertEquals(-1, param.getIndex());
    assertNull(param.getValueType());
    assertNotNull(param.getValue());
    assertTrue(param.getValue() instanceof NullMetadata);
    param = params.get(3);
    assertNotNull(param);
    assertEquals(-1, param.getIndex());
    assertEquals("java.lang.String", param.getValueType());
    assertNotNull(param.getValue());
    assertTrue(param.getValue() instanceof ValueMetadata);
    assertEquals("val3", ((ValueMetadata) param.getValue()).getStringValue());
    assertNull(((ValueMetadata) param.getValue()).getType());
    param = params.get(4);
    assertNotNull(param);
    assertEquals(-1, param.getIndex());
    assertNull(param.getValueType());
    assertNotNull(param.getValue());
    assertTrue(param.getValue() instanceof CollectionMetadata);
    CollectionMetadata array = (CollectionMetadata) param.getValue();
    assertNull(array.getValueType());
    assertNotNull(array.getValues());
    assertEquals(3, array.getValues().size());
    assertTrue(array.getValues().get(0) instanceof ValueMetadata);
    assertTrue(array.getValues().get(1) instanceof ComponentMetadata);
    assertTrue(array.getValues().get(2) instanceof NullMetadata);
    param = params.get(5);
    assertNotNull(param);
    assertEquals(-1, param.getIndex());
    assertNull(param.getValueType());
    assertNotNull(param.getValue());
    assertTrue(param.getValue() instanceof RefMetadata);
    assertEquals("pojoB", ((RefMetadata) param.getValue()).getComponentId());
    assertEquals(null, local.getInitMethod());
    assertEquals(null, local.getDestroyMethod());
    // test pojoB
    ComponentMetadata pojoB = registry.getComponentDefinition("pojoB");
    assertNotNull(pojoB);
    assertEquals("pojoB", pojoB.getId());
    assertTrue(pojoB instanceof BeanMetadata);
    BeanMetadata pojoBLocal = (BeanMetadata) pojoB;
    assertEquals("initPojo", pojoBLocal.getInitMethod());
    //        assertEquals("", pojoBLocal.getDestroyMethod());
    params = pojoBLocal.getArguments();
    assertNotNull(params);
    assertEquals(2, params.size());
    param = params.get(0);
    assertNotNull(param);
    assertEquals(1, param.getIndex());
    param = params.get(1);
    assertNotNull(param);
    assertEquals(0, param.getIndex());
}
Also used : BeanArgument(org.osgi.service.blueprint.reflect.BeanArgument) CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) NullMetadata(org.osgi.service.blueprint.reflect.NullMetadata)

Aggregations

BeanMetadata (org.osgi.service.blueprint.reflect.BeanMetadata)20 ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)9 CollectionMetadata (org.osgi.service.blueprint.reflect.CollectionMetadata)5 ExtendedBeanMetadata (org.apache.aries.blueprint.ExtendedBeanMetadata)4 RefMetadata (org.osgi.service.blueprint.reflect.RefMetadata)4 ValueMetadata (org.osgi.service.blueprint.reflect.ValueMetadata)4 ArrayList (java.util.ArrayList)3 MutableBeanMetadata (org.apache.aries.blueprint.mutable.MutableBeanMetadata)3 BeanArgument (org.osgi.service.blueprint.reflect.BeanArgument)3 BeanProperty (org.osgi.service.blueprint.reflect.BeanProperty)3 MapEntry (org.osgi.service.blueprint.reflect.MapEntry)3 MapMetadata (org.osgi.service.blueprint.reflect.MapMetadata)3 Metadata (org.osgi.service.blueprint.reflect.Metadata)3 ServiceMetadata (org.osgi.service.blueprint.reflect.ServiceMetadata)3 ServiceReferenceMetadata (org.osgi.service.blueprint.reflect.ServiceReferenceMetadata)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 WrappedServiceMetadata (org.apache.aries.application.modelling.WrappedServiceMetadata)2 BeanProcessor (org.apache.aries.blueprint.BeanProcessor)2 ComponentDefinitionRegistry (org.apache.aries.blueprint.ComponentDefinitionRegistry)2