use of org.osgi.service.blueprint.reflect.MapMetadata 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.MapMetadata in project aries by apache.
the class Parser method parseServiceProperties.
public MapMetadata parseServiceProperties(Element element, ComponentMetadata enclosingComponent) {
// TODO: need to handle this better
MapMetadata map = parseMap(element, enclosingComponent);
handleCustomElements(element, enclosingComponent);
return map;
}
use of org.osgi.service.blueprint.reflect.MapMetadata in project aries by apache.
the class BlueprintContainerImpl method getMetadata.
private <T extends ComponentMetadata> void getMetadata(Class<T> clazz, Metadata component, Collection<T> metadatas) {
if (component == null) {
return;
}
if (clazz.isInstance(component)) {
metadatas.add(clazz.cast(component));
}
if (component instanceof BeanMetadata) {
getMetadata(clazz, ((BeanMetadata) component).getFactoryComponent(), metadatas);
for (BeanArgument arg : ((BeanMetadata) component).getArguments()) {
getMetadata(clazz, arg.getValue(), metadatas);
}
for (BeanProperty prop : ((BeanMetadata) component).getProperties()) {
getMetadata(clazz, prop.getValue(), metadatas);
}
}
if (component instanceof CollectionMetadata) {
for (Metadata m : ((CollectionMetadata) component).getValues()) {
getMetadata(clazz, m, metadatas);
}
}
if (component instanceof MapMetadata) {
for (MapEntry m : ((MapMetadata) component).getEntries()) {
getMetadata(clazz, m.getKey(), metadatas);
getMetadata(clazz, m.getValue(), metadatas);
}
}
if (component instanceof PropsMetadata) {
for (MapEntry m : ((PropsMetadata) component).getEntries()) {
getMetadata(clazz, m.getKey(), metadatas);
getMetadata(clazz, m.getValue(), metadatas);
}
}
if (component instanceof ServiceReferenceMetadata) {
for (ReferenceListener l : ((ServiceReferenceMetadata) component).getReferenceListeners()) {
getMetadata(clazz, l.getListenerComponent(), metadatas);
}
}
if (component instanceof ServiceMetadata) {
getMetadata(clazz, ((ServiceMetadata) component).getServiceComponent(), metadatas);
for (MapEntry m : ((ServiceMetadata) component).getServiceProperties()) {
getMetadata(clazz, m.getKey(), metadatas);
getMetadata(clazz, m.getValue(), metadatas);
}
for (RegistrationListener l : ((ServiceMetadata) component).getRegistrationListeners()) {
getMetadata(clazz, l.getListenerComponent(), metadatas);
}
}
}
use of org.osgi.service.blueprint.reflect.MapMetadata in project aries by apache.
the class CmNamespaceHandler method parseManagedServiceFactory.
private ComponentMetadata parseManagedServiceFactory(ParserContext context, Element element) {
String id = getId(context, element);
MutableBeanMetadata factoryMetadata = context.createMetadata(MutableBeanMetadata.class);
generateIdIfNeeded(context, factoryMetadata);
factoryMetadata.addProperty("id", createValue(context, factoryMetadata.getId()));
factoryMetadata.setScope(BeanMetadata.SCOPE_SINGLETON);
factoryMetadata.setRuntimeClass(CmManagedServiceFactory.class);
factoryMetadata.setInitMethod("init");
factoryMetadata.setDestroyMethod("destroy");
factoryMetadata.addArgument(createRef(context, "blueprintContainer"), null, 0);
factoryMetadata.addProperty("factoryPid", createValue(context, element.getAttribute(FACTORY_PID_ATTRIBUTE)));
String autoExport = element.hasAttribute(AUTO_EXPORT_ATTRIBUTE) ? element.getAttribute(AUTO_EXPORT_ATTRIBUTE) : AUTO_EXPORT_DEFAULT;
if (AUTO_EXPORT_DISABLED.equals(autoExport)) {
autoExport = Integer.toString(ServiceMetadata.AUTO_EXPORT_DISABLED);
} else if (AUTO_EXPORT_INTERFACES.equals(autoExport)) {
autoExport = Integer.toString(ServiceMetadata.AUTO_EXPORT_INTERFACES);
} else if (AUTO_EXPORT_CLASS_HIERARCHY.equals(autoExport)) {
autoExport = Integer.toString(ServiceMetadata.AUTO_EXPORT_CLASS_HIERARCHY);
} else if (AUTO_EXPORT_ALL.equals(autoExport)) {
autoExport = Integer.toString(ServiceMetadata.AUTO_EXPORT_ALL_CLASSES);
} else {
throw new ComponentDefinitionException("Illegal value (" + autoExport + ") for " + AUTO_EXPORT_ATTRIBUTE + " attribute");
}
factoryMetadata.addProperty("autoExport", createValue(context, autoExport));
String ranking = element.hasAttribute(RANKING_ATTRIBUTE) ? element.getAttribute(RANKING_ATTRIBUTE) : RANKING_DEFAULT;
factoryMetadata.addProperty("ranking", createValue(context, ranking));
List<String> interfaces = null;
if (element.hasAttribute(INTERFACE_ATTRIBUTE)) {
interfaces = Collections.singletonList(element.getAttribute(INTERFACE_ATTRIBUTE));
factoryMetadata.addProperty("interfaces", createList(context, interfaces));
}
// Parse elements
List<RegistrationListener> listeners = new ArrayList<RegistrationListener>();
NodeList nl = element.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element) {
Element e = (Element) node;
if (isBlueprintNamespace(e.getNamespaceURI())) {
if (nodeNameEquals(e, INTERFACES_ELEMENT)) {
if (interfaces != null) {
throw new ComponentDefinitionException("Only one of " + INTERFACE_ATTRIBUTE + " attribute or " + INTERFACES_ELEMENT + " element must be used");
}
interfaces = parseInterfaceNames(e);
factoryMetadata.addProperty("interfaces", createList(context, interfaces));
} else if (nodeNameEquals(e, SERVICE_PROPERTIES_ELEMENT)) {
MapMetadata map = context.parseElement(MapMetadata.class, factoryMetadata, e);
factoryMetadata.addProperty("serviceProperties", map);
NodeList enl = e.getChildNodes();
for (int j = 0; j < enl.getLength(); j++) {
Node enode = enl.item(j);
if (enode instanceof Element) {
if (isCmNamespace(enode.getNamespaceURI()) && nodeNameEquals(enode, CM_PROPERTIES_ELEMENT)) {
decorateCmProperties(context, (Element) enode, factoryMetadata);
}
}
}
} else if (nodeNameEquals(e, REGISTRATION_LISTENER_ELEMENT)) {
listeners.add(context.parseElement(RegistrationListener.class, factoryMetadata, e));
}
} else if (isCmNamespace(e.getNamespaceURI())) {
if (nodeNameEquals(e, MANAGED_COMPONENT_ELEMENT)) {
MutableBeanMetadata managedComponent = context.parseElement(MutableBeanMetadata.class, null, e);
generateIdIfNeeded(context, managedComponent);
managedComponent.setScope(BeanMetadata.SCOPE_PROTOTYPE);
// destroy-method on managed-component has different signature than on regular beans
// so we'll handle it differently
String destroyMethod = managedComponent.getDestroyMethod();
if (destroyMethod != null) {
factoryMetadata.addProperty("componentDestroyMethod", createValue(context, destroyMethod));
managedComponent.setDestroyMethod(null);
}
context.getComponentDefinitionRegistry().registerComponentDefinition(managedComponent);
factoryMetadata.addProperty("managedComponentName", createIdRef(context, managedComponent.getId()));
}
}
}
}
MutableCollectionMetadata listenerCollection = context.createMetadata(MutableCollectionMetadata.class);
listenerCollection.setCollectionClass(List.class);
for (RegistrationListener listener : listeners) {
MutableBeanMetadata bean = context.createMetadata(MutableBeanMetadata.class);
bean.setRuntimeClass(ServiceListener.class);
bean.addProperty("listener", listener.getListenerComponent());
bean.addProperty("registerMethod", createValue(context, listener.getRegistrationMethod()));
bean.addProperty("unregisterMethod", createValue(context, listener.getUnregistrationMethod()));
listenerCollection.addValue(bean);
}
factoryMetadata.addProperty("listeners", listenerCollection);
context.getComponentDefinitionRegistry().registerComponentDefinition(factoryMetadata);
MutableBeanMetadata mapMetadata = context.createMetadata(MutableBeanMetadata.class);
mapMetadata.setScope(BeanMetadata.SCOPE_SINGLETON);
mapMetadata.setId(id);
mapMetadata.setFactoryComponent(createRef(context, factoryMetadata.getId()));
mapMetadata.setFactoryMethod("getServiceMap");
return mapMetadata;
}
use of org.osgi.service.blueprint.reflect.MapMetadata in project aries by apache.
the class AbstractParserProxy method traverse.
/**
* Traverse to find all nested {@link ComponentMetadata} instances
* @param metadata
* @param output
*/
private void traverse(Metadata metadata, Set<ComponentMetadata> output) {
if (metadata instanceof ComponentMetadata) {
traverseComponent((ComponentMetadata) metadata, output);
} else if (metadata instanceof CollectionMetadata) {
CollectionMetadata collection = (CollectionMetadata) metadata;
for (Metadata v : collection.getValues()) traverse(v, output);
} else if (metadata instanceof MapMetadata) {
MapMetadata map = (MapMetadata) metadata;
for (MapEntry e : map.getEntries()) {
traverse(e.getKey(), output);
traverse(e.getValue(), output);
}
}
}
Aggregations