use of org.osgi.service.blueprint.reflect.ComponentMetadata 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.ComponentMetadata in project aries by apache.
the class Parser method parseBeanProperty.
public BeanProperty parseBeanProperty(ComponentMetadata enclosingComponent, Element element) {
String name = element.hasAttribute(NAME_ATTRIBUTE) ? element.getAttribute(NAME_ATTRIBUTE) : null;
Metadata value = parseArgumentOrPropertyValue(element, enclosingComponent);
return new BeanPropertyImpl(name, value);
}
use of org.osgi.service.blueprint.reflect.ComponentMetadata in project aries by apache.
the class BlueprintMetadata method getComponentMetadata.
public CompositeData getComponentMetadata(long containerServiceId, String componentId) throws IOException {
BlueprintContainer container = getBlueprintContainer(containerServiceId);
ComponentMetadata componentMetadata = container.getComponentMetadata(componentId);
BPMetadata metadata = Util.metadata2BPMetadata(componentMetadata);
return metadata.asCompositeData();
}
use of org.osgi.service.blueprint.reflect.ComponentMetadata in project aries by apache.
the class CmNamespaceHandler method parsePropertyPlaceholder.
private ComponentMetadata parsePropertyPlaceholder(ParserContext context, Element element) {
MutableBeanMetadata metadata = context.createMetadata(MutableBeanMetadata.class);
metadata.setProcessor(true);
metadata.setId(getId(context, element));
metadata.setScope(BeanMetadata.SCOPE_SINGLETON);
metadata.setRuntimeClass(CmPropertyPlaceholder.class);
metadata.setInitMethod("init");
metadata.setDestroyMethod("destroy");
metadata.addProperty("blueprintContainer", createRef(context, "blueprintContainer"));
metadata.addProperty("configAdmin", createConfigurationAdminRef(context));
metadata.addProperty("persistentId", createValue(context, element.getAttribute(PERSISTENT_ID_ATTRIBUTE)));
String prefix = element.hasAttribute(PLACEHOLDER_PREFIX_ATTRIBUTE) ? element.getAttribute(PLACEHOLDER_PREFIX_ATTRIBUTE) : "${";
metadata.addProperty("placeholderPrefix", createValue(context, prefix));
String suffix = element.hasAttribute(PLACEHOLDER_SUFFIX_ATTRIBUTE) ? element.getAttribute(PLACEHOLDER_SUFFIX_ATTRIBUTE) : "}";
metadata.addProperty("placeholderSuffix", createValue(context, suffix));
String defaultsRef = element.hasAttribute(DEFAULTS_REF_ATTRIBUTE) ? element.getAttribute(DEFAULTS_REF_ATTRIBUTE) : null;
if (defaultsRef != null) {
metadata.addProperty("defaultProperties", createRef(context, defaultsRef));
}
String ignoreMissingLocations = extractIgnoreMissingLocations(element);
if (ignoreMissingLocations != null) {
metadata.addProperty("ignoreMissingLocations", createValue(context, ignoreMissingLocations));
}
String systemProperties = extractSystemPropertiesAttribute(element);
if (systemProperties == null) {
systemProperties = SYSTEM_PROPERTIES_NEVER;
}
metadata.addProperty("systemProperties", createValue(context, systemProperties));
String updateStrategy = element.getAttribute(UPDATE_STRATEGY_ATTRIBUTE);
if (updateStrategy != null) {
metadata.addProperty("updateStrategy", createValue(context, updateStrategy));
}
metadata.addProperty("managedObjectManager", createRef(context, MANAGED_OBJECT_MANAGER_NAME));
// Parse elements
List<String> locations = new ArrayList<String>();
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 (isCmNamespace(e.getNamespaceURI())) {
if (nodeNameEquals(e, DEFAULT_PROPERTIES_ELEMENT)) {
if (defaultsRef != null) {
throw new ComponentDefinitionException("Only one of " + DEFAULTS_REF_ATTRIBUTE + " attribute or " + DEFAULT_PROPERTIES_ELEMENT + " element is allowed");
}
Metadata props = parseDefaultProperties(context, metadata, e);
metadata.addProperty("defaultProperties", props);
}
} else if (isExtNamespace(e.getNamespaceURI())) {
if (nodeNameEquals(e, LOCATION_ELEMENT)) {
locations.add(getTextValue(e));
}
}
}
}
if (!locations.isEmpty()) {
metadata.addProperty("locations", createList(context, locations));
}
PlaceholdersUtils.validatePlaceholder(metadata, context.getComponentDefinitionRegistry());
return metadata;
}
use of org.osgi.service.blueprint.reflect.ComponentMetadata 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");
}
}
}
Aggregations