Search in sources :

Example 11 with NamespaceHandler

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;
    }
}
Also used : Bundle(org.osgi.framework.Bundle) NamespaceHandler(org.apache.aries.blueprint.NamespaceHandler)

Example 12 with NamespaceHandler

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);
}
Also used : SimpleNamespaceHandlerSet(org.apache.aries.blueprint.container.SimpleNamespaceHandlerSet) MilliLittersPropertyEditor(org.apache.xbean.blueprint.example.MilliLittersPropertyEditor) HashMap(java.util.HashMap) InputStream(java.io.InputStream) Properties(java.util.Properties) URL(java.net.URL) XBeanNamespaceHandler(org.apache.xbean.blueprint.context.impl.XBeanNamespaceHandler) QNameNamespaceHandler(org.apache.xbean.blueprint.context.impl.QNameNamespaceHandler) PropertyEditor(java.beans.PropertyEditor) MilliLittersPropertyEditor(org.apache.xbean.blueprint.example.MilliLittersPropertyEditor) QNameNamespaceHandler(org.apache.xbean.blueprint.context.impl.QNameNamespaceHandler) NamespaceHandler(org.apache.aries.blueprint.NamespaceHandler) XBeanNamespaceHandler(org.apache.xbean.blueprint.context.impl.XBeanNamespaceHandler) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 13 with NamespaceHandler

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;
}
Also used : Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) NamespaceHandler(org.apache.aries.blueprint.NamespaceHandler) ServiceReference(org.osgi.framework.ServiceReference)

Example 14 with NamespaceHandler

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);
}
Also used : NamespaceHandler(org.apache.aries.blueprint.NamespaceHandler) URI(java.net.URI) XML_NS_URI(javax.xml.XMLConstants.XML_NS_URI) W3C_XML_SCHEMA_NS_URI(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI)

Example 15 with NamespaceHandler

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;
}
Also used : Element(org.w3c.dom.Element) Metadata(org.osgi.service.blueprint.reflect.Metadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata) NonNullMetadata(org.osgi.service.blueprint.reflect.NonNullMetadata) MutableReferenceMetadata(org.apache.aries.blueprint.mutable.MutableReferenceMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) MutableRefMetadata(org.apache.aries.blueprint.mutable.MutableRefMetadata) ReferenceMetadata(org.osgi.service.blueprint.reflect.ReferenceMetadata) MutableServiceMetadata(org.apache.aries.blueprint.mutable.MutableServiceMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) MutableValueMetadata(org.apache.aries.blueprint.mutable.MutableValueMetadata) MutableValueMetadata(org.apache.aries.blueprint.mutable.MutableValueMetadata) MutableServiceMetadata(org.apache.aries.blueprint.mutable.MutableServiceMetadata) Target(org.osgi.service.blueprint.reflect.Target) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) MutableRefMetadata(org.apache.aries.blueprint.mutable.MutableRefMetadata) NonNullMetadata(org.osgi.service.blueprint.reflect.NonNullMetadata) NamespaceHandler(org.apache.aries.blueprint.NamespaceHandler)

Aggregations

NamespaceHandler (org.apache.aries.blueprint.NamespaceHandler)20 URI (java.net.URI)6 IOException (java.io.IOException)4 Bundle (org.osgi.framework.Bundle)4 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 Properties (java.util.Properties)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)3 SAXException (org.xml.sax.SAXException)3 InputStream (java.io.InputStream)2 MalformedURLException (java.net.MalformedURLException)2 URISyntaxException (java.net.URISyntaxException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 W3C_XML_SCHEMA_NS_URI (javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI)2 XML_NS_URI (javax.xml.XMLConstants.XML_NS_URI)2 ParserContext (org.apache.aries.blueprint.ParserContext)2 SimpleNamespaceHandlerSet (org.apache.aries.blueprint.container.SimpleNamespaceHandlerSet)2