use of org.osgi.service.blueprint.reflect.Metadata 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.Metadata in project aries by apache.
the class AbstractParserProxy method processMultiValueProperty.
private void processMultiValueProperty(Map<String, Object> serviceProps, String key, Metadata value) {
List<Metadata> values = ((CollectionMetadata) value).getValues();
Class<?> collectionClass = ((CollectionMetadata) value).getCollectionClass();
Object collectionValue;
if (Collection.class.isAssignableFrom(collectionClass)) {
Collection<String> theseValues = getCollectionFromClass(collectionClass);
for (Metadata m : values) {
theseValues.add(((ValueMetadata) m).getStringValue());
}
collectionValue = theseValues;
} else {
String[] theseValues = new String[values.size()];
for (int i = 0; i < values.size(); i++) {
Metadata m = values.get(i);
theseValues[i] = ((ValueMetadata) m).getStringValue();
}
collectionValue = theseValues;
}
serviceProps.put(key, collectionValue);
}
use of org.osgi.service.blueprint.reflect.Metadata in project aries by apache.
the class SpringOsgiNamespaceHandler method parseInlinedTarget.
private Target parseInlinedTarget(ParserContext context, ComponentMetadata metadata, Element element) {
Target listenerComponent;
if (BLUEPRINT_NAMESPACE.equals(element.getNamespaceURI()) && BEAN_ELEMENT.equals(element.getLocalName())) {
listenerComponent = context.parseElement(BeanMetadata.class, metadata, element);
} else {
NamespaceHandler handler = context.getNamespaceHandler(URI.create(element.getNamespaceURI()));
if (handler == null) {
throw new IllegalStateException("No NamespaceHandler found for " + element.getNamespaceURI());
}
Metadata md = handler.parse(element, context);
if (!(md instanceof Target)) {
throw new IllegalStateException("NamespaceHandler did not return a Target instance but " + md);
}
listenerComponent = (Target) md;
}
return listenerComponent;
}
use of org.osgi.service.blueprint.reflect.Metadata in project aries by apache.
the class Parser method parseCollection.
private CollectionMetadata parseCollection(Class collectionType, Element element, ComponentMetadata enclosingComponent) {
// Parse attributes
String valueType = element.hasAttribute(VALUE_TYPE_ATTRIBUTE) ? element.getAttribute(VALUE_TYPE_ATTRIBUTE) : null;
// Parse elements
List<Metadata> list = new ArrayList<Metadata>();
NodeList nl = element.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element) {
Metadata val = parseValueGroup((Element) node, enclosingComponent, null, true);
list.add(val);
}
}
return new CollectionMetadataImpl(collectionType, valueType, list);
}
use of org.osgi.service.blueprint.reflect.Metadata in project aries by apache.
the class Parser method loadComponents.
private void loadComponents(Document doc) {
defaultTimeout = TIMEOUT_DEFAULT;
defaultAvailability = AVAILABILITY_DEFAULT;
defaultActivation = ACTIVATION_DEFAULT;
Element root = doc.getDocumentElement();
if (!isBlueprintNamespace(root.getNamespaceURI()) || !nodeNameEquals(root, BLUEPRINT_ELEMENT)) {
throw new ComponentDefinitionException("Root element must be {" + BLUEPRINT_NAMESPACE + "}" + BLUEPRINT_ELEMENT + " element");
}
// Parse global attributes
if (root.hasAttribute(DEFAULT_ACTIVATION_ATTRIBUTE)) {
defaultActivation = root.getAttribute(DEFAULT_ACTIVATION_ATTRIBUTE);
}
if (root.hasAttribute(DEFAULT_TIMEOUT_ATTRIBUTE)) {
defaultTimeout = root.getAttribute(DEFAULT_TIMEOUT_ATTRIBUTE);
}
if (root.hasAttribute(DEFAULT_AVAILABILITY_ATTRIBUTE)) {
defaultAvailability = root.getAttribute(DEFAULT_AVAILABILITY_ATTRIBUTE);
}
// Parse custom attributes
handleCustomAttributes(root.getAttributes(), null);
// Parse elements
// Break into 2 loops to ensure we scan the blueprint elements before
// This is needed so that when we process the custom element, we know
// the component definition registry has populated all blueprint components.
NodeList nl = root.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element) {
Element element = (Element) node;
String namespaceUri = element.getNamespaceURI();
if (isBlueprintNamespace(namespaceUri)) {
parseBlueprintElement(element);
}
}
}
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element) {
Element element = (Element) node;
String namespaceUri = element.getNamespaceURI();
if (!isBlueprintNamespace(namespaceUri)) {
Metadata component = parseCustomElement(element, null);
if (component != null) {
if (!(component instanceof ComponentMetadata)) {
throw new ComponentDefinitionException("Expected a ComponentMetadata to be returned when parsing element " + element.getNodeName());
}
registry.registerComponentDefinition((ComponentMetadata) component);
}
}
}
}
}
Aggregations