use of org.osgi.service.blueprint.reflect.Target in project aries by apache.
the class AbstractPropertyPlaceholder method processServiceMetadata.
protected Metadata processServiceMetadata(ServiceMetadata component) {
try {
if (component instanceof MutableServiceMetadata) {
processingStack.add("Service Component->");
((MutableServiceMetadata) component).setServiceComponent((Target) processMetadata(component.getServiceComponent()));
} else {
printWarning(component, "Service Component");
processingStack.add("Service Component->");
processMetadata(component.getServiceComponent());
}
} finally {
processingStack.removeLast();
}
List<MapEntry> entries = new ArrayList<MapEntry>(component.getServiceProperties());
if (!!!entries.isEmpty()) {
try {
if (component instanceof MutableServiceMetadata) {
processingStack.add("Service Properties->");
MutableServiceMetadata msm = (MutableServiceMetadata) component;
for (MapEntry entry : entries) {
msm.removeServiceProperty(entry);
}
for (MapEntry entry : processMapEntries(entries)) {
msm.addServiceProperty(entry);
}
} else {
printWarning(component, "Service Properties");
processingStack.add("Service Properties->");
processMapEntries(entries);
}
} finally {
processingStack.removeLast();
}
}
for (RegistrationListener listener : component.getRegistrationListeners()) {
Target listenerComponent = listener.getListenerComponent();
try {
processingStack.add("Registration Listener " + listenerComponent + "->");
if (listener instanceof MutableRegistrationListener) {
((MutableRegistrationListener) listener).setListenerComponent((Target) processMetadata(listenerComponent));
} else {
//Say that we can't change this listener, but continue processing
//If the value is mutable then we may be ok!
printWarning(listener, "Service Registration Listener");
processMetadata(listenerComponent);
}
} finally {
processingStack.removeLast();
}
}
return component;
}
use of org.osgi.service.blueprint.reflect.Target in project aries by apache.
the class AbstractPropertyPlaceholder method processServiceReferenceMetadata.
private Metadata processServiceReferenceMetadata(ServiceReferenceMetadata component) {
if (component instanceof MutableServiceReferenceMetadata) {
ValueMetadata valueMetadata = ((MutableServiceReferenceMetadata) component).getExtendedFilter();
if (valueMetadata != null) {
((MutableServiceReferenceMetadata) component).setExtendedFilter(doProcessValueMetadata(valueMetadata));
}
}
for (ReferenceListener listener : component.getReferenceListeners()) {
Target listenerComponent = listener.getListenerComponent();
try {
processingStack.add("Reference Listener " + listenerComponent + "->");
if (listener instanceof MutableReferenceListener) {
((MutableReferenceListener) listener).setListenerComponent((Target) processMetadata(listenerComponent));
} else {
//Say that we can't change this listener, but continue processing
//If the value is mutable then we may be ok!
printWarning(listener, "Reference Binding Listener");
processMetadata(listenerComponent);
}
} finally {
processingStack.removeLast();
}
}
return component;
}
use of org.osgi.service.blueprint.reflect.Target in project aries by apache.
the class Parser method parseTypeConverters.
private void parseTypeConverters(Element element) {
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;
Object target = null;
if (isBlueprintNamespace(e.getNamespaceURI())) {
if (nodeNameEquals(e, BEAN_ELEMENT)) {
target = parseBeanMetadata(e, true);
} else if (nodeNameEquals(e, REF_ELEMENT)) {
String componentName = e.getAttribute(COMPONENT_ID_ATTRIBUTE);
target = new RefMetadataImpl(componentName);
} else if (nodeNameEquals(e, REFERENCE_ELEMENT)) {
target = parseReference(e, true);
}
} else {
target = parseCustomElement(e, null);
}
if (!(target instanceof Target)) {
throw new ComponentDefinitionException("Metadata parsed for element " + e.getNodeName() + " can not be used as a type converter");
}
registry.registerTypeConverter((Target) target);
}
}
}
use of org.osgi.service.blueprint.reflect.Target in project aries by apache.
the class Parser method parseService.
private ComponentMetadata parseService(Element element, boolean topElement) {
ServiceMetadataImpl service = new ServiceMetadataImpl();
boolean hasInterfaceNameAttribute = false;
if (topElement) {
service.setId(getId(element));
service.setActivation(parseActivation(element));
} else {
service.setActivation(ComponentMetadata.ACTIVATION_LAZY);
}
if (element.hasAttribute(INTERFACE_ATTRIBUTE)) {
service.setInterfaceNames(Collections.singletonList(element.getAttribute(INTERFACE_ATTRIBUTE)));
hasInterfaceNameAttribute = true;
}
if (element.hasAttribute(REF_ATTRIBUTE)) {
service.setServiceComponent(new RefMetadataImpl(element.getAttribute(REF_ATTRIBUTE)));
}
if (element.hasAttribute(DEPENDS_ON_ATTRIBUTE)) {
service.setDependsOn(parseList(element.getAttribute(DEPENDS_ON_ATTRIBUTE)));
}
String autoExport = element.hasAttribute(AUTO_EXPORT_ATTRIBUTE) ? element.getAttribute(AUTO_EXPORT_ATTRIBUTE) : AUTO_EXPORT_DEFAULT;
if (AUTO_EXPORT_DISABLED.equals(autoExport)) {
service.setAutoExport(ServiceMetadata.AUTO_EXPORT_DISABLED);
} else if (AUTO_EXPORT_INTERFACES.equals(autoExport)) {
service.setAutoExport(ServiceMetadata.AUTO_EXPORT_INTERFACES);
} else if (AUTO_EXPORT_CLASS_HIERARCHY.equals(autoExport)) {
service.setAutoExport(ServiceMetadata.AUTO_EXPORT_CLASS_HIERARCHY);
} else if (AUTO_EXPORT_ALL.equals(autoExport)) {
service.setAutoExport(ServiceMetadata.AUTO_EXPORT_ALL_CLASSES);
} else {
throw new ComponentDefinitionException("Illegal value (" + autoExport + ") for " + AUTO_EXPORT_ATTRIBUTE + " attribute");
}
String ranking = element.hasAttribute(RANKING_ATTRIBUTE) ? element.getAttribute(RANKING_ATTRIBUTE) : RANKING_DEFAULT;
try {
service.setRanking(Integer.parseInt(ranking));
} catch (NumberFormatException e) {
throw new ComponentDefinitionException("Attribute " + RANKING_ATTRIBUTE + " must be a valid integer (was: " + ranking + ")");
}
// Parse elements
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 (hasInterfaceNameAttribute) {
throw new ComponentDefinitionException("Only one of " + INTERFACE_ATTRIBUTE + " attribute or " + INTERFACES_ELEMENT + " element must be used");
}
service.setInterfaceNames(parseInterfaceNames(e));
} else if (nodeNameEquals(e, SERVICE_PROPERTIES_ELEMENT)) {
List<MapEntry> entries = parseServiceProperties(e, service).getEntries();
service.setServiceProperties(entries);
} else if (nodeNameEquals(e, REGISTRATION_LISTENER_ELEMENT)) {
service.addRegistrationListener(parseRegistrationListener(e, service));
} else if (nodeNameEquals(e, BEAN_ELEMENT)) {
if (service.getServiceComponent() != null) {
throw new ComponentDefinitionException("Only one of " + REF_ATTRIBUTE + " attribute, " + BEAN_ELEMENT + " element, " + REFERENCE_ELEMENT + " element or " + REF_ELEMENT + " element can be set");
}
service.setServiceComponent((Target) parseBeanMetadata(e, false));
} else if (nodeNameEquals(e, REF_ELEMENT)) {
if (service.getServiceComponent() != null) {
throw new ComponentDefinitionException("Only one of " + REF_ATTRIBUTE + " attribute, " + BEAN_ELEMENT + " element, " + REFERENCE_ELEMENT + " element or " + REF_ELEMENT + " element can be set");
}
String component = e.getAttribute(COMPONENT_ID_ATTRIBUTE);
if (component == null || component.length() == 0) {
throw new ComponentDefinitionException("Element " + REF_ELEMENT + " must have a valid " + COMPONENT_ID_ATTRIBUTE + " attribute");
}
service.setServiceComponent(new RefMetadataImpl(component));
} else if (nodeNameEquals(e, REFERENCE_ELEMENT)) {
if (service.getServiceComponent() != null) {
throw new ComponentDefinitionException("Only one of " + REF_ATTRIBUTE + " attribute, " + BEAN_ELEMENT + " element, " + REFERENCE_ELEMENT + " element or " + REF_ELEMENT + " element can be set");
}
service.setServiceComponent((Target) parseReference(e, false));
}
}
}
}
// Check service
if (service.getServiceComponent() == null) {
throw new ComponentDefinitionException("One of " + REF_ATTRIBUTE + " attribute, " + BEAN_ELEMENT + " element, " + REFERENCE_ELEMENT + " element or " + REF_ELEMENT + " element must be set");
}
// Check interface
if (service.getAutoExport() == ServiceMetadata.AUTO_EXPORT_DISABLED && service.getInterfaces().isEmpty()) {
throw new ComponentDefinitionException(INTERFACE_ATTRIBUTE + " attribute or " + INTERFACES_ELEMENT + " element must be set when " + AUTO_EXPORT_ATTRIBUTE + " is set to " + AUTO_EXPORT_DISABLED);
}
// Check for non-disabled auto-exports and interfaces
if (service.getAutoExport() != ServiceMetadata.AUTO_EXPORT_DISABLED && !service.getInterfaces().isEmpty()) {
throw new ComponentDefinitionException(INTERFACE_ATTRIBUTE + " attribute or " + INTERFACES_ELEMENT + " element must not be set when " + AUTO_EXPORT_ATTRIBUTE + " is set to anything else than " + AUTO_EXPORT_DISABLED);
}
ComponentMetadata s = service;
// Parse custom attributes
s = handleCustomAttributes(element.getAttributes(), s);
// Parse custom elements;
s = handleCustomElements(element, s);
return s;
}
use of org.osgi.service.blueprint.reflect.Target in project aries by apache.
the class SpringOsgiNamespaceHandler method parseService.
private Metadata parseService(Element element, ParserContext context) {
MutableServiceMetadata metadata = context.createMetadata(MutableServiceMetadata.class);
// Parse attributes
if (element.hasAttribute(ID_ATTRIBUTE)) {
metadata.setId(element.getAttribute(ID_ATTRIBUTE));
} else {
metadata.setId(generateId(context));
}
if (nonEmpty(element.getAttribute(REF_ATTRIBUTE)) != null) {
MutableRefMetadata ref = context.createMetadata(MutableRefMetadata.class);
ref.setComponentId(element.getAttribute(REF_ATTRIBUTE));
metadata.setServiceComponent(ref);
}
metadata.setRanking(nonEmpty(element.getAttribute(RANKING_ATTRIBUTE)) != null ? Integer.parseInt(element.getAttribute(RANKING_ATTRIBUTE)) : 0);
String itf = nonEmpty(element.getAttribute(INTERFACE_ATTRIBUTE));
if (itf != null) {
metadata.addInterface(itf);
}
String[] dependsOn = StringUtils.tokenizeToStringArray(nonEmpty(element.getAttribute(DEPENDS_ON_ATTRIBUTE)), ",; ");
metadata.setDependsOn(dependsOn != null ? Arrays.asList(dependsOn) : null);
String autoExp = nonEmpty(element.getAttribute(AUTO_EXPORT_ATTRIBUTE));
if (AUTO_EXPORT_INTERFACES.equals(autoExp)) {
metadata.setAutoExport(ServiceMetadata.AUTO_EXPORT_INTERFACES);
} else if (AUTO_EXPORT_CLASS_HIERARCHY.equals(autoExp)) {
metadata.setAutoExport(ServiceMetadata.AUTO_EXPORT_CLASS_HIERARCHY);
} else if (AUTO_EXPORT_ALL_CLASSES.equals(autoExp)) {
metadata.setAutoExport(ServiceMetadata.AUTO_EXPORT_ALL_CLASSES);
} else {
metadata.setAutoExport(ServiceMetadata.AUTO_EXPORT_DISABLED);
}
// Parse child elements
for (Element child : getChildren(element)) {
if (element.getNamespaceURI().equals(child.getNamespaceURI())) {
if (INTERFACES_ELEMENT.equals(child.getLocalName())) {
List<String> itfs = parseInterfaces(child);
for (String intf : itfs) {
metadata.addInterface(intf);
}
} else if (REGISTRATION_LISTENER_ELEMENT.equals(child.getLocalName())) {
String regMethod = nonEmpty(child.getAttribute(REGISTRATION_METHOD_ATTRIBUTE));
String unregMethod = nonEmpty(child.getAttribute(UNREGISTRATION_METHOD_ATTRIBUTE));
String refStr = nonEmpty(child.getAttribute(REF_ATTRIBUTE));
Target listenerComponent = null;
if (refStr != null) {
MutableRefMetadata ref = context.createMetadata(MutableRefMetadata.class);
ref.setComponentId(refStr);
listenerComponent = ref;
}
for (Element cchild : getChildren(child)) {
if (listenerComponent != null) {
throw new IllegalArgumentException("Only one of @ref attribute or inlined bean definition element is allowed");
}
listenerComponent = parseInlinedTarget(context, metadata, cchild);
}
if (listenerComponent == null) {
throw new IllegalArgumentException("Missing @ref attribute or inlined bean definition element");
}
metadata.addRegistrationListener(listenerComponent, regMethod, unregMethod);
} else if (SERVICE_PROPERTIES_ELEMENT.equals(child.getLocalName())) {
// TODO: @key-type
for (Element e : getChildren(child)) {
if (ENTRY_ELEMENT.equals(e.getLocalName())) {
NonNullMetadata key;
Metadata val;
boolean hasKeyAttribute = e.hasAttribute(KEY_ATTRIBUTE);
boolean hasKeyRefAttribute = e.hasAttribute(KEY_REF_ATTRIBUTE);
if (hasKeyRefAttribute && !hasKeyAttribute) {
MutableRefMetadata r = context.createMetadata(MutableRefMetadata.class);
r.setComponentId(e.getAttribute(KEY_REF_ATTRIBUTE));
key = r;
} else if (hasKeyAttribute && !hasKeyRefAttribute) {
MutableValueMetadata v = context.createMetadata(MutableValueMetadata.class);
v.setStringValue(e.getAttribute(KEY_ATTRIBUTE));
key = v;
} else {
throw new IllegalStateException("Either key or key-ref must be specified");
}
// TODO: support key
boolean hasValAttribute = e.hasAttribute(VALUE_ATTRIBUTE);
boolean hasValRefAttribute = e.hasAttribute(VALUE_REF_ATTRIBUTE);
if (hasValRefAttribute && !hasValAttribute) {
MutableRefMetadata r = context.createMetadata(MutableRefMetadata.class);
r.setComponentId(e.getAttribute(VALUE_REF_ATTRIBUTE));
val = r;
} else if (hasValAttribute && !hasValRefAttribute) {
MutableValueMetadata v = context.createMetadata(MutableValueMetadata.class);
v.setStringValue(e.getAttribute(VALUE_ATTRIBUTE));
val = v;
} else {
throw new IllegalStateException("Either val or val-ref must be specified");
}
// TODO: support children elements ?
metadata.addServiceProperty(key, val);
}
}
}
} else if (BLUEPRINT_NAMESPACE.equals(child.getNamespaceURI()) && BEAN_ELEMENT.equals(child.getLocalName())) {
if (metadata.getServiceComponent() != null) {
throw new IllegalArgumentException("Only one of @ref attribute and bean element is allowed");
}
Target bean = context.parseElement(BeanMetadata.class, metadata, child);
metadata.setServiceComponent(bean);
} else {
if (metadata.getServiceComponent() != null) {
throw new IllegalArgumentException("Only one of @ref attribute or inlined bean definition element is allowed");
}
NamespaceHandler handler = context.getNamespaceHandler(URI.create(child.getNamespaceURI()));
if (handler == null) {
throw new IllegalStateException("No NamespaceHandler found for " + child.getNamespaceURI());
}
Metadata md = handler.parse(child, context);
if (!(md instanceof Target)) {
throw new IllegalStateException("NamespaceHandler did not return a Target instance but " + md);
}
metadata.setServiceComponent((Target) md);
}
}
return metadata;
}
Aggregations