Search in sources :

Example 16 with ComponentDefinitionException

use of org.osgi.service.blueprint.container.ComponentDefinitionException in project aries by apache.

the class Parser method parseMapEntry.

private MapEntry parseMapEntry(Element element, ComponentMetadata enclosingComponent, String keyType, String valueType) {
    // Parse attributes
    String key = element.hasAttribute(KEY_ATTRIBUTE) ? element.getAttribute(KEY_ATTRIBUTE) : null;
    String keyRef = element.hasAttribute(KEY_REF_ATTRIBUTE) ? element.getAttribute(KEY_REF_ATTRIBUTE) : null;
    String value = element.hasAttribute(VALUE_ATTRIBUTE) ? element.getAttribute(VALUE_ATTRIBUTE) : null;
    String valueRef = element.hasAttribute(VALUE_REF_ATTRIBUTE) ? element.getAttribute(VALUE_REF_ATTRIBUTE) : null;
    // Parse elements
    NonNullMetadata keyValue = null;
    Metadata valValue = null;
    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 (nodeNameEquals(e, KEY_ELEMENT)) {
                keyValue = parseMapKeyEntry(e, enclosingComponent, keyType);
            } else {
                valValue = parseValueGroup(e, enclosingComponent, valueType, true);
            }
        }
    }
    // Check key
    if (keyValue != null && (key != null || keyRef != null) || (keyValue == null && key == null && keyRef == null)) {
        throw new ComponentDefinitionException("Only and only one of " + KEY_ATTRIBUTE + " attribute, " + KEY_REF_ATTRIBUTE + " attribute or " + KEY_ELEMENT + " element must be set");
    } else if (keyValue == null && key != null) {
        keyValue = new ValueMetadataImpl(key, keyType);
    } else if (keyValue == null) /*&& keyRef != null*/
    {
        keyValue = new RefMetadataImpl(keyRef);
    }
    // Check value
    if (valValue != null && (value != null || valueRef != null) || (valValue == null && value == null && valueRef == null)) {
        throw new ComponentDefinitionException("Only and only one of " + VALUE_ATTRIBUTE + " attribute, " + VALUE_REF_ATTRIBUTE + " attribute or sub element must be set");
    } else if (valValue == null && value != null) {
        valValue = new ValueMetadataImpl(value, valueType);
    } else if (valValue == null) /*&& valueRef != null*/
    {
        valValue = new RefMetadataImpl(valueRef);
    }
    return new MapEntryImpl(keyValue, valValue);
}
Also used : ValueMetadataImpl(org.apache.aries.blueprint.reflect.ValueMetadataImpl) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) MapEntryImpl(org.apache.aries.blueprint.reflect.MapEntryImpl) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata) NonNullMetadata(org.osgi.service.blueprint.reflect.NonNullMetadata) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) ServiceReferenceMetadata(org.osgi.service.blueprint.reflect.ServiceReferenceMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) NullMetadata(org.osgi.service.blueprint.reflect.NullMetadata) Metadata(org.osgi.service.blueprint.reflect.Metadata) PropsMetadata(org.osgi.service.blueprint.reflect.PropsMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) IdRefMetadata(org.osgi.service.blueprint.reflect.IdRefMetadata) ReferenceMetadata(org.osgi.service.blueprint.reflect.ReferenceMetadata) MapMetadata(org.osgi.service.blueprint.reflect.MapMetadata) ReferenceListMetadata(org.osgi.service.blueprint.reflect.ReferenceListMetadata) NonNullMetadata(org.osgi.service.blueprint.reflect.NonNullMetadata) RefMetadataImpl(org.apache.aries.blueprint.reflect.RefMetadataImpl) IdRefMetadataImpl(org.apache.aries.blueprint.reflect.IdRefMetadataImpl)

Example 17 with ComponentDefinitionException

use of org.osgi.service.blueprint.container.ComponentDefinitionException in project aries by apache.

the class Parser method validatePsvi.

public void validatePsvi(Schema schema) {
    try {
        // In order to support validation with the built-in xml parser
        // from the JDK, we can't use Validator.validate(source, result)
        // as it fails with an exception, see
        //   https://issues.apache.org/jira/browse/XERCESJ-1212
        // This was fixed in xerces 2.9.0 years ago but still is not
        // included in my JDK.
        List<String> locations = new ArrayList<String>();
        for (Document doc : documents) {
            locations.add(doc.getDocumentURI());
        }
        List<Document> validated = new ArrayList<Document>();
        for (String location : locations) {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            factory.setSchema(schema);
            DocumentBuilder builder = factory.newDocumentBuilder();
            InputSource inputSource = new InputSource(location);
            Document doc = builder.parse(inputSource);
            validated.add(doc);
        }
        this.documents.clear();
        this.documents.addAll(validated);
    } catch (Exception e) {
        throw new ComponentDefinitionException("Unable to validate xml", e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException)

Example 18 with ComponentDefinitionException

use of org.osgi.service.blueprint.container.ComponentDefinitionException in project aries by apache.

the class Parser method parseServiceListener.

private ReferenceListener parseServiceListener(Element element, ComponentMetadata enclosingComponent) {
    ReferenceListenerImpl listener = new ReferenceListenerImpl();
    Metadata listenerComponent = null;
    // Parse attributes
    if (element.hasAttribute(REF_ATTRIBUTE)) {
        listenerComponent = new RefMetadataImpl(element.getAttribute(REF_ATTRIBUTE));
    }
    String bindMethodName = null;
    String unbindMethodName = null;
    if (element.hasAttribute(BIND_METHOD_ATTRIBUTE)) {
        bindMethodName = element.getAttribute(BIND_METHOD_ATTRIBUTE);
        listener.setBindMethod(bindMethodName);
    }
    if (element.hasAttribute(UNBIND_METHOD_ATTRIBUTE)) {
        unbindMethodName = element.getAttribute(UNBIND_METHOD_ATTRIBUTE);
        listener.setUnbindMethod(unbindMethodName);
    }
    if (bindMethodName == null && unbindMethodName == null) {
        throw new ComponentDefinitionException("One of " + BIND_METHOD_ATTRIBUTE + " or " + UNBIND_METHOD_ATTRIBUTE + " must be set");
    }
    // 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, REF_ELEMENT)) {
                    if (listenerComponent != null) {
                        throw new ComponentDefinitionException("Only one of " + REF_ATTRIBUTE + " attribute, " + REF_ELEMENT + ", " + BLUEPRINT_ELEMENT + ", " + REFERENCE_ELEMENT + ", " + SERVICE_ELEMENT + " or custom 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");
                    }
                    listenerComponent = new RefMetadataImpl(component);
                } else if (nodeNameEquals(e, BEAN_ELEMENT)) {
                    if (listenerComponent != null) {
                        throw new ComponentDefinitionException("Only one of " + REF_ATTRIBUTE + " attribute, " + REF_ELEMENT + ", " + BLUEPRINT_ELEMENT + ", " + REFERENCE_ELEMENT + ", " + SERVICE_ELEMENT + " or custom element can be set");
                    }
                    listenerComponent = parseBeanMetadata(e, false);
                } else if (nodeNameEquals(e, REFERENCE_ELEMENT)) {
                    if (listenerComponent != null) {
                        throw new ComponentDefinitionException("Only one of " + REF_ATTRIBUTE + " attribute, " + REF_ELEMENT + ", " + BLUEPRINT_ELEMENT + ", " + REFERENCE_ELEMENT + ", " + SERVICE_ELEMENT + " or custom element can be set");
                    }
                    listenerComponent = parseReference(e, false);
                } else if (nodeNameEquals(e, SERVICE_ELEMENT)) {
                    if (listenerComponent != null) {
                        throw new ComponentDefinitionException("Only one of " + REF_ATTRIBUTE + " attribute, " + REF_ELEMENT + ", " + BLUEPRINT_ELEMENT + ", " + REFERENCE_ELEMENT + ", " + SERVICE_ELEMENT + " or custom element can be set");
                    }
                    listenerComponent = parseService(e, false);
                }
            } else {
                if (listenerComponent != null) {
                    throw new ComponentDefinitionException("Only one of " + REF_ATTRIBUTE + " attribute, " + REF_ELEMENT + ", " + BLUEPRINT_ELEMENT + ", " + REFERENCE_ELEMENT + ", " + SERVICE_ELEMENT + " or custom element can be set");
                }
                listenerComponent = parseCustomElement(e, enclosingComponent);
            }
        }
    }
    if (listenerComponent == null) {
        throw new ComponentDefinitionException("One of " + REF_ATTRIBUTE + " attribute, " + REF_ELEMENT + ", " + BLUEPRINT_ELEMENT + ", " + REFERENCE_ELEMENT + ", " + SERVICE_ELEMENT + " or custom element must be set");
    }
    listener.setListenerComponent((Target) listenerComponent);
    return listener;
}
Also used : ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) ReferenceListenerImpl(org.apache.aries.blueprint.reflect.ReferenceListenerImpl) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata) NonNullMetadata(org.osgi.service.blueprint.reflect.NonNullMetadata) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) ServiceReferenceMetadata(org.osgi.service.blueprint.reflect.ServiceReferenceMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) NullMetadata(org.osgi.service.blueprint.reflect.NullMetadata) Metadata(org.osgi.service.blueprint.reflect.Metadata) PropsMetadata(org.osgi.service.blueprint.reflect.PropsMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) IdRefMetadata(org.osgi.service.blueprint.reflect.IdRefMetadata) ReferenceMetadata(org.osgi.service.blueprint.reflect.ReferenceMetadata) MapMetadata(org.osgi.service.blueprint.reflect.MapMetadata) ReferenceListMetadata(org.osgi.service.blueprint.reflect.ReferenceListMetadata) RefMetadataImpl(org.apache.aries.blueprint.reflect.RefMetadataImpl) IdRefMetadataImpl(org.apache.aries.blueprint.reflect.IdRefMetadataImpl)

Example 19 with ComponentDefinitionException

use of org.osgi.service.blueprint.container.ComponentDefinitionException in project aries by apache.

the class AbstractServiceReferenceRecipe method start.

public void start(SatisfactionListener listener) {
    if (listener == null)
        throw new NullPointerException("satisfactionListener is null");
    if (started.compareAndSet(false, true)) {
        try {
            satisfactionListener = listener;
            satisfied.set(optional);
            // though this may not be sufficient because we don't control ordering of those events
            synchronized (tracked) {
                getBundleContextForServiceLookup().addServiceListener(this, getOsgiFilter());
                ServiceReference[] references = getBundleContextForServiceLookup().getServiceReferences((String) null, getOsgiFilter());
                tracked.setInitial(references != null ? references : new ServiceReference[0]);
            }
            tracked.trackInitial();
            satisfied.set(optional || !tracked.isEmpty());
            retrack();
            LOGGER.debug("Found initial references {} for OSGi service {}", getServiceReferences(), getOsgiFilter());
        } catch (InvalidSyntaxException e) {
            throw new ComponentDefinitionException(e);
        }
    }
}
Also used : ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Example 20 with ComponentDefinitionException

use of org.osgi.service.blueprint.container.ComponentDefinitionException in project aries by apache.

the class ReferencesTest method testWiring.

public void testWiring() throws Exception {
    ComponentDefinitionRegistryImpl registry = parse("/test-references.xml");
    ProxyManager proxyManager = new AbstractProxyManager() {

        @Override
        protected Object createNewProxy(Bundle bundle, Collection<Class<?>> classes, Callable<Object> objectCallable, InvocationListener invocationListener) throws UnableToProxyException {
            return new Object();
        }

        @Override
        protected InvocationHandler getInvocationHandler(Object o) {
            return null;
        }

        @Override
        protected boolean isProxyClass(Class<?> aClass) {
            return false;
        }
    };
    Repository repository = new TestBlueprintContainer(registry, proxyManager).getRepository();
    repository.create("refItf");
    try {
        repository.create("refClsErr");
        fail("Should have failed");
    } catch (ComponentDefinitionException e) {
    }
    repository.create("refClsOk");
}
Also used : Repository(org.apache.aries.blueprint.di.Repository) AbstractProxyManager(org.apache.aries.proxy.impl.AbstractProxyManager) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) Bundle(org.osgi.framework.Bundle) InvocationListener(org.apache.aries.proxy.InvocationListener) AbstractProxyManager(org.apache.aries.proxy.impl.AbstractProxyManager) ProxyManager(org.apache.aries.proxy.ProxyManager) Collection(java.util.Collection) ComponentDefinitionRegistryImpl(org.apache.aries.blueprint.parser.ComponentDefinitionRegistryImpl) Callable(java.util.concurrent.Callable)

Aggregations

ComponentDefinitionException (org.osgi.service.blueprint.container.ComponentDefinitionException)57 Node (org.w3c.dom.Node)23 Element (org.w3c.dom.Element)17 NodeList (org.w3c.dom.NodeList)15 ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)14 MutableBeanMetadata (org.apache.aries.blueprint.mutable.MutableBeanMetadata)12 ArrayList (java.util.ArrayList)11 BeanMetadata (org.osgi.service.blueprint.reflect.BeanMetadata)8 JAXBException (javax.xml.bind.JAXBException)7 MutablePassThroughMetadata (org.apache.aries.blueprint.mutable.MutablePassThroughMetadata)7 IdRefMetadataImpl (org.apache.aries.blueprint.reflect.IdRefMetadataImpl)7 RefMetadataImpl (org.apache.aries.blueprint.reflect.RefMetadataImpl)7 ExpressionNode (org.apache.camel.model.ExpressionNode)7 MapMetadata (org.osgi.service.blueprint.reflect.MapMetadata)7 RefMetadata (org.osgi.service.blueprint.reflect.RefMetadata)7 ExtendedBeanMetadata (org.apache.aries.blueprint.ExtendedBeanMetadata)6 CollectionMetadata (org.osgi.service.blueprint.reflect.CollectionMetadata)6 IdRefMetadata (org.osgi.service.blueprint.reflect.IdRefMetadata)6 Metadata (org.osgi.service.blueprint.reflect.Metadata)6 ReferenceMetadata (org.osgi.service.blueprint.reflect.ReferenceMetadata)6