Search in sources :

Example 1 with ComponentDefinitionException

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

the class NamespaceHandler method parsePropertyPlaceholder.

public ComponentMetadata parsePropertyPlaceholder(Element element, ParserContext context) {
    MutableBeanMetadata metadata = context.createMetadata(MutableBeanMetadata.class);
    metadata.setProcessor(true);
    metadata.setId(getId(context, element));
    metadata.setScope(BeanMetadata.SCOPE_SINGLETON);
    metadata.setRuntimeClass(EncryptablePropertyPlaceholder.class);
    metadata.setInitMethod("init");
    String prefix = element.hasAttribute(PLACEHOLDER_PREFIX_ATTRIBUTE) ? element.getAttribute(PLACEHOLDER_PREFIX_ATTRIBUTE) : "ENC(";
    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 encryptorRef = element.hasAttribute("encryptor-ref") ? element.getAttribute("encryptor-ref") : null;
    if (encryptorRef != null) {
        metadata.addProperty("encryptor", createRef(context, encryptorRef));
    }
    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 (JASYPT_NAMESPACE_1_0.equals(e.getNamespaceURI())) {
                String name = e.getLocalName() != null ? e.getLocalName() : e.getNodeName();
                if (ENCRYPTOR_ELEMENT.equals(name)) {
                    if (encryptorRef != null) {
                        throw new ComponentDefinitionException("Only one of " + ENCRYPTOR_REF_ATTRIBUTE + " attribute or " + ENCRYPTOR_ELEMENT + " element is allowed");
                    }
                    BeanMetadata encryptor = context.parseElement(BeanMetadata.class, metadata, e);
                    metadata.addProperty("encryptor", encryptor);
                }
            }
        }
    }
    PlaceholdersUtils.validatePlaceholder(metadata, context.getComponentDefinitionRegistry());
    return metadata;
}
Also used : MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element)

Example 2 with ComponentDefinitionException

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

the class NamespaceHandler method parseCommand.

private void parseCommand(Element element, ParserContext context) {
    MutableBeanMetadata command = context.createMetadata(MutableBeanMetadata.class);
    command.setRuntimeClass(BlueprintCommand.class);
    command.addProperty(BLUEPRINT_CONTAINER, createRef(context, BLUEPRINT_CONTAINER));
    command.addProperty(BLUEPRINT_CONVERTER, createRef(context, BLUEPRINT_CONVERTER));
    NodeList children = element.getChildNodes();
    MutableBeanMetadata action = null;
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child instanceof Element) {
            Element childElement = (Element) child;
            if (nodeNameEquals(childElement, ACTION)) {
                action = parseAction(context, command, childElement);
                action.setId(getName());
                context.getComponentDefinitionRegistry().registerComponentDefinition(action);
                command.addProperty(ACTION_ID, createIdRef(context, action.getId()));
            } else if (nodeNameEquals(childElement, COMPLETERS)) {
                command.addProperty(COMPLETERS, parseCompleters(context, command, childElement));
            } else if (nodeNameEquals(childElement, OPTIONAL_COMPLETERS)) {
                command.addProperty(OPTIONAL_COMPLETERS_PROPERTY, parseOptionalCompleters(context, command, childElement));
            } else {
                throw new ComponentDefinitionException("Bad xml syntax: unknown element '" + childElement.getNodeName() + "'");
            }
        }
    }
    MutableServiceMetadata commandService = context.createMetadata(MutableServiceMetadata.class);
    commandService.setActivation(MutableServiceMetadata.ACTIVATION_LAZY);
    commandService.setId(getName());
    commandService.setAutoExport(ServiceMetadata.AUTO_EXPORT_INTERFACES);
    commandService.setServiceComponent(command);
    String scope;
    String function;
    if (SHELL_NAMESPACE_1_0_0.equals(element.getNamespaceURI())) {
        String location = element.getAttribute(NAME);
        location = location.replace('/', ':');
        if (location.lastIndexOf(':') >= 0) {
            scope = location.substring(0, location.lastIndexOf(':'));
            function = location.substring(location.lastIndexOf(':') + 1);
        } else {
            scope = "";
            function = location;
        }
    } else {
        try {
            Class actionClass = getBundle(context).loadClass(action.getClassName());
            scope = getScope(actionClass);
            function = getName(actionClass);
        } catch (Throwable e) {
            throw new ComponentDefinitionException("Unable to introspect action " + action.getClassName(), e);
        }
    }
    commandService.addServiceProperty(createStringValue(context, "osgi.command.scope"), createStringValue(context, scope));
    commandService.addServiceProperty(createStringValue(context, "osgi.command.function"), createStringValue(context, function));
    context.getComponentDefinitionRegistry().registerComponentDefinition(commandService);
    String subShellName = null;
    if (scope != null && !scope.isEmpty()) {
        // if it's shell 1.0.0 schema and scope is contained in the descriptor itself
        subShellName = ".subshell." + scope;
    }
    if (subShellName == null || !context.getComponentDefinitionRegistry().containsComponentDefinition(subShellName)) {
        // if the scope is unknown or if the scope has not been defined before
        createSubShell(context, scope, subShellName);
    }
}
Also used : MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) MutableServiceMetadata(org.apache.aries.blueprint.mutable.MutableServiceMetadata) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element)

Example 3 with ComponentDefinitionException

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

the class CamelNamespaceHandler method parseSecureRandomParametersNode.

private Metadata parseSecureRandomParametersNode(Element element, ParserContext context) {
    LOG.trace("Parsing SecureRandomParameters {}", element);
    // now parse the key store parameters with JAXB
    Binder<Node> binder;
    try {
        binder = getJaxbContext().createBinder();
    } catch (JAXBException e) {
        throw new ComponentDefinitionException("Failed to create the JAXB binder : " + e, e);
    }
    Object value = parseUsingJaxb(element, context, binder);
    if (!(value instanceof SecureRandomParametersFactoryBean)) {
        throw new ComponentDefinitionException("Expected an instance of " + SecureRandomParametersFactoryBean.class);
    }
    SecureRandomParametersFactoryBean srfb = (SecureRandomParametersFactoryBean) value;
    String id = srfb.getId();
    MutablePassThroughMetadata factory = context.createMetadata(MutablePassThroughMetadata.class);
    factory.setId(".camelBlueprint.passThrough." + id);
    factory.setObject(new PassThroughCallable<Object>(srfb));
    MutableBeanMetadata factory2 = context.createMetadata(MutableBeanMetadata.class);
    factory2.setId(".camelBlueprint.factory." + id);
    factory2.setFactoryComponent(factory);
    factory2.setFactoryMethod("call");
    factory2.setInitMethod("afterPropertiesSet");
    factory2.setDestroyMethod("destroy");
    factory2.addProperty("blueprintContainer", createRef(context, "blueprintContainer"));
    MutableBeanMetadata ctx = context.createMetadata(MutableBeanMetadata.class);
    ctx.setId(id);
    ctx.setRuntimeClass(SecureRandomParameters.class);
    ctx.setFactoryComponent(factory2);
    ctx.setFactoryMethod("getObject");
    // must be lazy as we want CamelContext to be activated first
    ctx.setActivation(ACTIVATION_LAZY);
    LOG.trace("Parsing SecureRandomParameters done, returning {}", ctx);
    return ctx;
}
Also used : MutablePassThroughMetadata(org.apache.aries.blueprint.mutable.MutablePassThroughMetadata) MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) ExpressionNode(org.apache.camel.model.ExpressionNode) Node(org.w3c.dom.Node) JAXBException(javax.xml.bind.JAXBException) SecureRandomParametersFactoryBean(org.apache.camel.util.blueprint.SecureRandomParametersFactoryBean)

Example 4 with ComponentDefinitionException

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

the class CamelNamespaceHandler method parseSSLContextParametersNode.

private Metadata parseSSLContextParametersNode(Element element, ParserContext context) {
    LOG.trace("Parsing SSLContextParameters {}", element);
    // now parse the key store parameters with JAXB
    Binder<Node> binder;
    try {
        binder = getJaxbContext().createBinder();
    } catch (JAXBException e) {
        throw new ComponentDefinitionException("Failed to create the JAXB binder : " + e, e);
    }
    Object value = parseUsingJaxb(element, context, binder);
    if (!(value instanceof SSLContextParametersFactoryBean)) {
        throw new ComponentDefinitionException("Expected an instance of " + SSLContextParametersFactoryBean.class);
    }
    SSLContextParametersFactoryBean scpfb = (SSLContextParametersFactoryBean) value;
    String id = scpfb.getId();
    MutablePassThroughMetadata factory = context.createMetadata(MutablePassThroughMetadata.class);
    factory.setId(".camelBlueprint.passThrough." + id);
    factory.setObject(new PassThroughCallable<Object>(scpfb));
    MutableBeanMetadata factory2 = context.createMetadata(MutableBeanMetadata.class);
    factory2.setId(".camelBlueprint.factory." + id);
    factory2.setFactoryComponent(factory);
    factory2.setFactoryMethod("call");
    factory2.setInitMethod("afterPropertiesSet");
    factory2.setDestroyMethod("destroy");
    factory2.addProperty("blueprintContainer", createRef(context, "blueprintContainer"));
    MutableBeanMetadata ctx = context.createMetadata(MutableBeanMetadata.class);
    ctx.setId(id);
    ctx.setRuntimeClass(SSLContextParameters.class);
    ctx.setFactoryComponent(factory2);
    ctx.setFactoryMethod("getObject");
    // must be lazy as we want CamelContext to be activated first
    ctx.setActivation(ACTIVATION_LAZY);
    LOG.trace("Parsing SSLContextParameters done, returning {}", ctx);
    return ctx;
}
Also used : MutablePassThroughMetadata(org.apache.aries.blueprint.mutable.MutablePassThroughMetadata) MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) ExpressionNode(org.apache.camel.model.ExpressionNode) Node(org.w3c.dom.Node) JAXBException(javax.xml.bind.JAXBException) SSLContextParametersFactoryBean(org.apache.camel.util.blueprint.SSLContextParametersFactoryBean)

Example 5 with ComponentDefinitionException

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

the class CamelNamespaceHandler method parseEndpointNode.

private Metadata parseEndpointNode(Element element, ParserContext context) {
    LOG.trace("Parsing Endpoint {}", element);
    // now parse the rests with JAXB
    Binder<Node> binder;
    try {
        binder = getJaxbContext().createBinder();
    } catch (JAXBException e) {
        throw new ComponentDefinitionException("Failed to create the JAXB binder : " + e, e);
    }
    Object value = parseUsingJaxb(element, context, binder);
    if (!(value instanceof CamelEndpointFactoryBean)) {
        throw new ComponentDefinitionException("Expected an instance of " + CamelEndpointFactoryBean.class);
    }
    CamelEndpointFactoryBean rcfb = (CamelEndpointFactoryBean) value;
    String id = rcfb.getId();
    MutablePassThroughMetadata factory = context.createMetadata(MutablePassThroughMetadata.class);
    factory.setId(".camelBlueprint.passThrough." + id);
    factory.setObject(new PassThroughCallable<Object>(rcfb));
    MutableBeanMetadata factory2 = context.createMetadata(MutableBeanMetadata.class);
    factory2.setId(".camelBlueprint.factory." + id);
    factory2.setFactoryComponent(factory);
    factory2.setFactoryMethod("call");
    factory2.setInitMethod("afterPropertiesSet");
    factory2.setDestroyMethod("destroy");
    factory2.addProperty("blueprintContainer", createRef(context, "blueprintContainer"));
    MutableBeanMetadata ctx = context.createMetadata(MutableBeanMetadata.class);
    ctx.setId(id);
    ctx.setRuntimeClass(Endpoint.class);
    ctx.setFactoryComponent(factory2);
    ctx.setFactoryMethod("getObject");
    // must be lazy as we want CamelContext to be activated first
    ctx.setActivation(ACTIVATION_LAZY);
    LOG.trace("Parsing endpoint done, returning {}", element, ctx);
    return ctx;
}
Also used : MutablePassThroughMetadata(org.apache.aries.blueprint.mutable.MutablePassThroughMetadata) MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) ExpressionNode(org.apache.camel.model.ExpressionNode) Node(org.w3c.dom.Node) JAXBException(javax.xml.bind.JAXBException) CamelEndpointFactoryBean(org.apache.camel.blueprint.CamelEndpointFactoryBean)

Aggregations

ComponentDefinitionException (org.osgi.service.blueprint.container.ComponentDefinitionException)80 Node (org.w3c.dom.Node)26 Element (org.w3c.dom.Element)21 MutableBeanMetadata (org.apache.aries.blueprint.mutable.MutableBeanMetadata)18 NodeList (org.w3c.dom.NodeList)18 ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)15 ArrayList (java.util.ArrayList)9 MutableReferenceMetadata (org.apache.aries.blueprint.mutable.MutableReferenceMetadata)8 RefMetadata (org.osgi.service.blueprint.reflect.RefMetadata)8 JAXBException (javax.xml.bind.JAXBException)7 MutablePassThroughMetadata (org.apache.aries.blueprint.mutable.MutablePassThroughMetadata)7 MutableServiceReferenceMetadata (org.apache.aries.blueprint.mutable.MutableServiceReferenceMetadata)7 IdRefMetadataImpl (org.apache.aries.blueprint.reflect.IdRefMetadataImpl)7 RefMetadataImpl (org.apache.aries.blueprint.reflect.RefMetadataImpl)7 ExpressionNode (org.apache.camel.model.ExpressionNode)7 BeanMetadata (org.osgi.service.blueprint.reflect.BeanMetadata)7 MapMetadata (org.osgi.service.blueprint.reflect.MapMetadata)7 Recipe (org.apache.aries.blueprint.di.Recipe)6 ComponentDefinitionRegistry (org.apache.aries.blueprint.ComponentDefinitionRegistry)5 MutableCollectionMetadata (org.apache.aries.blueprint.mutable.MutableCollectionMetadata)5