Search in sources :

Example 11 with BeanMetadata

use of org.osgi.service.blueprint.reflect.BeanMetadata in project aries by apache.

the class BlueprintContainerImpl method instantiateEagerComponents.

protected void instantiateEagerComponents() {
    List<String> components = new ArrayList<String>();
    for (String name : componentDefinitionRegistry.getComponentDefinitionNames()) {
        ComponentMetadata component = componentDefinitionRegistry.getComponentDefinition(name);
        boolean eager = component.getActivation() == ComponentMetadata.ACTIVATION_EAGER;
        if (component instanceof BeanMetadata) {
            BeanMetadata local = (BeanMetadata) component;
            eager &= MetadataUtil.isSingletonScope(local);
        }
        if (eager) {
            components.add(name);
        }
    }
    LOGGER.debug("Instantiating components: {}", components);
    try {
        repository.createAll(components);
    } catch (ComponentDefinitionException e) {
        throw e;
    } catch (Throwable t) {
        throw new ComponentDefinitionException("Unable to instantiate components", t);
    }
}
Also used : ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) ExtendedBeanMetadata(org.apache.aries.blueprint.ExtendedBeanMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) ArrayList(java.util.ArrayList) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata)

Example 12 with BeanMetadata

use of org.osgi.service.blueprint.reflect.BeanMetadata in project aries by apache.

the class BeanRecipe method runBeanProcPostInit.

private Object runBeanProcPostInit(Object obj) {
    String beanName = getName();
    BeanMetadata beanData = (BeanMetadata) blueprintContainer.getComponentDefinitionRegistry().getComponentDefinition(beanName);
    List<BeanProcessor> processors = blueprintContainer.getProcessors(BeanProcessor.class);
    //The start link of the chain, that provides the 
    //original, unprocessed bean to the head of the chain.
    BeanProcessor.BeanCreator initialBeanCreator = new BeanProcessor.BeanCreator() {

        public Object getBean() {
            Object obj = getInstance();
            //getinit, getdestroy, addpartial object don't need calling again.
            //however, property injection does.
            setProperties(obj);
            //as this is the post init chain, new beans need to go thru 
            //the pre-init chain, and then have init called, before 
            //being passed along the post-init chain.
            obj = runBeanProcPreInit(obj);
            runBeanProcInit(getInitMethod(obj), obj);
            return obj;
        }
    };
    BeanProcessor.BeanCreator currentCreator = initialBeanCreator;
    for (BeanProcessor processor : processors) {
        obj = processor.afterInit(obj, getName(), currentCreator, beanData);
        currentCreator = new BeanCreatorChain(currentCreator, processor, beanData, beanName, BeanCreatorChain.ChainType.After);
    }
    return obj;
}
Also used : BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) BeanProcessor(org.apache.aries.blueprint.BeanProcessor)

Example 13 with BeanMetadata

use of org.osgi.service.blueprint.reflect.BeanMetadata 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 14 with BeanMetadata

use of org.osgi.service.blueprint.reflect.BeanMetadata in project opennms by OpenNMS.

the class OnmsVaadinUIFactory method validate.

/**
     * Verify that the current UIFactory is set up correctly.
     */
private void validate() {
    // Verify that the uiBean is a subclass of UI
    final Object instance = m_blueprintContainer.getComponentInstance(m_uiBeanId);
    if (!(instance instanceof UI)) {
        throw new IllegalStateException("The bean with id " + m_uiBeanId + " must be of type " + com.vaadin.ui.UI.class);
    }
    // Verify that the scope is prototype and NOT singleton
    final ComponentMetadata componentMetadata = Objects.requireNonNull(m_blueprintContainer.getComponentMetadata(m_uiBeanId));
    if (!(componentMetadata instanceof BeanMetadata)) {
        throw new IllegalStateException("The referenced id is not a bean");
    }
    if (!BeanMetadata.SCOPE_PROTOTYPE.equals(((BeanMetadata) componentMetadata).getScope())) {
        throw new IllegalStateException("The scope of the defined bean with id " + m_uiBeanId + " must be " + BeanMetadata.SCOPE_PROTOTYPE + " but is " + BeanMetadata.SCOPE_SINGLETON);
    }
}
Also used : UI(com.vaadin.ui.UI) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata)

Example 15 with BeanMetadata

use of org.osgi.service.blueprint.reflect.BeanMetadata in project aries by apache.

the class SpringBeanProcessor method getProcessors.

private <T> List<T> getProcessors(Class<T> type) {
    List<T> processors = new ArrayList<T>();
    if (!creatingProcessor) {
        creatingProcessor = true;
        for (BeanMetadata bean : blueprintContainer.getMetadata(BeanMetadata.class)) {
            Class clazz = null;
            if (bean instanceof ExtendedBeanMetadata) {
                clazz = ((ExtendedBeanMetadata) bean).getRuntimeClass();
            }
            if (clazz == null && bean.getClassName() != null) {
                try {
                    clazz = bundleContext.getBundle().loadClass(bean.getClassName());
                } catch (ClassNotFoundException e) {
                }
            }
            if (clazz == null) {
                continue;
            }
            if (type.isAssignableFrom(clazz)) {
                Object p = blueprintContainer.getComponentInstance(bean.getId());
                processors.add(type.cast(p));
            }
        }
        creatingProcessor = false;
    }
    return processors;
}
Also used : ExtendedBeanMetadata(org.apache.aries.blueprint.ExtendedBeanMetadata) ExtendedBeanMetadata(org.apache.aries.blueprint.ExtendedBeanMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) ArrayList(java.util.ArrayList)

Aggregations

BeanMetadata (org.osgi.service.blueprint.reflect.BeanMetadata)20 ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)9 CollectionMetadata (org.osgi.service.blueprint.reflect.CollectionMetadata)5 ExtendedBeanMetadata (org.apache.aries.blueprint.ExtendedBeanMetadata)4 RefMetadata (org.osgi.service.blueprint.reflect.RefMetadata)4 ValueMetadata (org.osgi.service.blueprint.reflect.ValueMetadata)4 ArrayList (java.util.ArrayList)3 MutableBeanMetadata (org.apache.aries.blueprint.mutable.MutableBeanMetadata)3 BeanArgument (org.osgi.service.blueprint.reflect.BeanArgument)3 BeanProperty (org.osgi.service.blueprint.reflect.BeanProperty)3 MapEntry (org.osgi.service.blueprint.reflect.MapEntry)3 MapMetadata (org.osgi.service.blueprint.reflect.MapMetadata)3 Metadata (org.osgi.service.blueprint.reflect.Metadata)3 ServiceMetadata (org.osgi.service.blueprint.reflect.ServiceMetadata)3 ServiceReferenceMetadata (org.osgi.service.blueprint.reflect.ServiceReferenceMetadata)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 WrappedServiceMetadata (org.apache.aries.application.modelling.WrappedServiceMetadata)2 BeanProcessor (org.apache.aries.blueprint.BeanProcessor)2 ComponentDefinitionRegistry (org.apache.aries.blueprint.ComponentDefinitionRegistry)2