use of org.apache.aries.blueprint.NamespaceHandler in project aries by apache.
the class CmNamespaceHandler method getSchemaLocation.
public URL getSchemaLocation(String namespace) {
if (BLUEPRINT_CM_NAMESPACE_1_3.equals(namespace)) {
return getClass().getResource("blueprint-cm-1.3.0.xsd");
} else if (BLUEPRINT_CM_NAMESPACE_1_2.equals(namespace)) {
return getClass().getResource("blueprint-cm-1.2.0.xsd");
} else if (BLUEPRINT_CM_NAMESPACE_1_1.equals(namespace)) {
return getClass().getResource("blueprint-cm-1.1.0.xsd");
} else if (BLUEPRINT_CM_NAMESPACE_1_0.equals(namespace)) {
return getClass().getResource("blueprint-cm-1.0.0.xsd");
} else if (namespace.startsWith("http://aries.apache.org/blueprint/xmlns/blueprint-ext")) {
try {
Class<?> extNsHandlerClazz;
Bundle extBundle = FrameworkUtil.getBundle(PlaceholdersUtils.class);
if (extBundle == null) {
// we may not be in OSGi environment
extNsHandlerClazz = getClass().getClassLoader().loadClass("org.apache.aries.blueprint.ext.impl.ExtNamespaceHandler");
} else {
extNsHandlerClazz = extBundle.loadClass("org.apache.aries.blueprint.ext.impl.ExtNamespaceHandler");
}
return ((NamespaceHandler) extNsHandlerClazz.newInstance()).getSchemaLocation(namespace);
} catch (Throwable t) {
LOGGER.warn("Could not locate ext namespace schema", t);
return null;
}
} else {
return null;
}
}
use of org.apache.aries.blueprint.NamespaceHandler in project geronimo-xbean by apache.
the class BlueprintTestSupport method parse.
protected static BlueprintContainerImpl parse(String plan, String schema) throws Exception {
Properties properties = new Properties();
URL propUrl = BlueprintTestSupport.class.getClassLoader().getResource(schema);
InputStream in = propUrl.openStream();
try {
properties.load(in);
} finally {
in.close();
}
Set<Class> classes = new HashSet<Class>();
ClassLoader cl = BlueprintTestSupport.class.getClassLoader();
for (Map.Entry entry : properties.entrySet()) {
String key = (String) entry.getKey();
if (!key.contains(".")) {
String className = (String) entry.getValue();
Class clazz = cl.loadClass(className);
classes.add(clazz);
}
}
classes.add(QName.class);
Map<String, Class<? extends PropertyEditor>> propertyEditors = new HashMap<String, Class<? extends PropertyEditor>>();
propertyEditors.put(MilliLittersPropertyEditor.class.getName(), MilliLittersPropertyEditor.class);
final NamespaceHandler xbeanHandler = new XBeanNamespaceHandler(NAMESPACE_URI.toString(), BlueprintTestSupport.class.getClassLoader().getResource("restaurant.xsd"), classes, propertyEditors, properties);
final NamespaceHandler qnameHandler = new QNameNamespaceHandler();
SimpleNamespaceHandlerSet handlers = new SimpleNamespaceHandlerSet();
handlers.addNamespace(NAMESPACE_URI, xbeanHandler.getSchemaLocation(NAMESPACE_URI.toString()), xbeanHandler);
handlers.addNamespace(QNAME_URI, xbeanHandler.getSchemaLocation(NAMESPACE_URI.toString()), qnameHandler);
return parse(plan, handlers);
}
use of org.apache.aries.blueprint.NamespaceHandler in project opennms by OpenNMS.
the class BlueprintNamespaceShellCommand method execute.
@Override
public Object execute() throws Exception {
final Collection<ServiceReference<NamespaceHandler>> services = this.bundleContext.getServiceReferences(NamespaceHandler.class, null);
for (final ServiceReference<NamespaceHandler> sr : services) {
final Bundle bundle = sr.getBundle();
final Object rawNamespaces = sr.getProperty("osgi.service.blueprint.namespace");
final ArrayList<String> namespaces = new ArrayList<>();
if (rawNamespaces instanceof String) {
namespaces.add((String) rawNamespaces);
} else if (rawNamespaces instanceof Object[]) {
for (final Object namespace : (Object[]) rawNamespaces) {
namespaces.add(namespace.toString());
}
} else if (rawNamespaces instanceof String[]) {
for (final String namespace : (String[]) rawNamespaces) {
namespaces.add(namespace);
}
} else {
System.err.println("Hmm, not sure how to interpret: " + rawNamespaces);
}
System.out.println(bundle.toString());
for (final Object namespace : namespaces) {
System.out.println(" " + namespace);
}
System.out.println();
}
return null;
}
use of org.apache.aries.blueprint.NamespaceHandler in project aries by apache.
the class NamespaceHandlerRegistryImpl method unregisterHandler.
public void unregisterHandler(NamespaceHandler handler, Map properties) {
List<URI> namespaces = getNamespaces(properties);
for (URI uri : namespaces) {
CopyOnWriteArraySet<NamespaceHandler> h = handlers.get(uri);
if (!h.remove(handler)) {
continue;
}
List<NamespaceHandlerSetImpl> sets;
synchronized (this.sets) {
sets = new ArrayList<NamespaceHandlerSetImpl>(this.sets);
}
for (NamespaceHandlerSetImpl s : sets) {
s.unregisterHandler(uri, handler);
}
}
removeSchemasFor(handler);
}
use of org.apache.aries.blueprint.NamespaceHandler 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