use of org.osgi.service.blueprint.reflect.RefMetadata in project aries by apache.
the class RecipeBuilder method getValue.
private Recipe getValue(Metadata v, Object groupingType) {
if (v instanceof NullMetadata) {
return null;
} else if (v instanceof ComponentMetadata) {
return createRecipe((ComponentMetadata) v);
} else if (v instanceof ValueMetadata) {
ValueMetadata stringValue = (ValueMetadata) v;
Object type = stringValue.getType();
type = (type == null) ? groupingType : type;
ValueRecipe vr = new ValueRecipe(getName(null), stringValue, type);
return vr;
} else if (v instanceof RefMetadata) {
// TODO: make it work with property-placeholders?
String componentName = ((RefMetadata) v).getComponentId();
RefRecipe rr = new RefRecipe(getName(null), componentName);
return rr;
} else if (v instanceof CollectionMetadata) {
CollectionMetadata collectionMetadata = (CollectionMetadata) v;
Class<?> cl = collectionMetadata.getCollectionClass();
String type = collectionMetadata.getValueType();
if (cl == Object[].class) {
ArrayRecipe ar = new ArrayRecipe(getName(null), type);
for (Metadata lv : collectionMetadata.getValues()) {
ar.add(getValue(lv, type));
}
return ar;
} else {
CollectionRecipe cr = new CollectionRecipe(getName(null), cl != null ? cl : ArrayList.class, type);
for (Metadata lv : collectionMetadata.getValues()) {
cr.add(getValue(lv, type));
}
return cr;
}
} else if (v instanceof MapMetadata) {
return createMapRecipe((MapMetadata) v);
} else if (v instanceof PropsMetadata) {
PropsMetadata mapValue = (PropsMetadata) v;
MapRecipe mr = new MapRecipe(getName(null), Properties.class, String.class, String.class);
for (MapEntry entry : mapValue.getEntries()) {
Recipe key = getValue(entry.getKey(), String.class);
Recipe val = getValue(entry.getValue(), String.class);
mr.put(key, val);
}
return mr;
} else if (v instanceof IdRefMetadata) {
// TODO: make it work with property-placeholders?
String componentName = ((IdRefMetadata) v).getComponentId();
IdRefRecipe rnr = new IdRefRecipe(getName(null), componentName);
return rnr;
} else {
throw new IllegalStateException("Unsupported value: " + (v != null ? v.getClass().getName() : "null"));
}
}
use of org.osgi.service.blueprint.reflect.RefMetadata 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;
}
use of org.osgi.service.blueprint.reflect.RefMetadata 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());
}
use of org.osgi.service.blueprint.reflect.RefMetadata in project aries by apache.
the class BlueprintContainerImpl method processTypeConverters.
private void processTypeConverters() throws Exception {
List<String> typeConverters = new ArrayList<String>();
for (Target target : componentDefinitionRegistry.getTypeConverters()) {
if (target instanceof ComponentMetadata) {
typeConverters.add(((ComponentMetadata) target).getId());
} else if (target instanceof RefMetadata) {
typeConverters.add(((RefMetadata) target).getComponentId());
} else {
throw new ComponentDefinitionException("Unexpected metadata for type converter: " + target);
}
}
Map<String, Object> objects = repository.createAll(typeConverters, Arrays.<Class<?>>asList(Converter.class));
for (String name : typeConverters) {
Object obj = objects.get(name);
if (obj instanceof Converter) {
converter.registerConverter((Converter) obj);
} else {
throw new ComponentDefinitionException("Type converter " + obj + " does not implement the " + Converter.class.getName() + " interface");
}
}
}
use of org.osgi.service.blueprint.reflect.RefMetadata in project aries by apache.
the class NSHandlerOne method decorate.
//process attributes
public ComponentMetadata decorate(Node node, ComponentMetadata component, ParserContext context) {
//and RefMetadata.
if (component != null && component instanceof MutableBeanMetadata) {
MutableBeanMetadata mbm = (MutableBeanMetadata) component;
Attr a = (Attr) node;
Element bean = a.getOwnerElement();
String propname = bean.getAttributeNS(NSURI, ATTRIB_ONE);
//if this were not a test, we might attempt to ensure this ref existed
String passthruref = bean.getAttributeNS(NSURI, ATTRIB_TWO);
MutableRefMetadata ref = (MutableRefMetadata) context.createMetadata(RefMetadata.class);
ref.setComponentId(passthruref);
mbm.addProperty(propname, ref);
}
return component;
}
Aggregations