Search in sources :

Example 71 with ComponentDefinitionException

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

the class CmNamespaceHandler method parsePropertyPlaceholder.

private ComponentMetadata parsePropertyPlaceholder(ParserContext context, Element element) {
    MutableBeanMetadata metadata = context.createMetadata(MutableBeanMetadata.class);
    metadata.setProcessor(true);
    metadata.setId(getId(context, element));
    metadata.setScope(BeanMetadata.SCOPE_SINGLETON);
    metadata.setRuntimeClass(CmPropertyPlaceholder.class);
    metadata.setInitMethod("init");
    metadata.setDestroyMethod("destroy");
    metadata.addProperty("blueprintContainer", createRef(context, "blueprintContainer"));
    metadata.addProperty("configAdmin", createConfigurationAdminRef(context));
    metadata.addProperty("persistentId", createValue(context, element.getAttribute(PERSISTENT_ID_ATTRIBUTE)));
    String prefix = element.hasAttribute(PLACEHOLDER_PREFIX_ATTRIBUTE) ? element.getAttribute(PLACEHOLDER_PREFIX_ATTRIBUTE) : "${";
    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 nullValue = element.hasAttribute(PLACEHOLDER_NULL_VALUE_ATTRIBUTE) ? element.getAttribute(PLACEHOLDER_NULL_VALUE_ATTRIBUTE) : null;
    if (nullValue != null) {
        metadata.addProperty("nullValue", createValue(context, nullValue));
    }
    String defaultsRef = element.hasAttribute(DEFAULTS_REF_ATTRIBUTE) ? element.getAttribute(DEFAULTS_REF_ATTRIBUTE) : null;
    if (defaultsRef != null) {
        metadata.addProperty("defaultProperties", createRef(context, defaultsRef));
    }
    String ignoreMissingLocations = extractIgnoreMissingLocations(element);
    if (ignoreMissingLocations != null) {
        metadata.addProperty("ignoreMissingLocations", createValue(context, ignoreMissingLocations));
    }
    String systemProperties = extractSystemPropertiesAttribute(element);
    if (systemProperties == null) {
        systemProperties = SYSTEM_PROPERTIES_NEVER;
    }
    metadata.addProperty("systemProperties", createValue(context, systemProperties));
    String updateStrategy = element.getAttribute(UPDATE_STRATEGY_ATTRIBUTE);
    if (updateStrategy != null) {
        metadata.addProperty("updateStrategy", createValue(context, updateStrategy));
    }
    metadata.addProperty("managedObjectManager", createRef(context, MANAGED_OBJECT_MANAGER_NAME));
    // Parse elements
    List<String> locations = new ArrayList<String>();
    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 (isCmNamespace(e.getNamespaceURI())) {
                if (nodeNameEquals(e, DEFAULT_PROPERTIES_ELEMENT)) {
                    if (defaultsRef != null) {
                        throw new ComponentDefinitionException("Only one of " + DEFAULTS_REF_ATTRIBUTE + " attribute or " + DEFAULT_PROPERTIES_ELEMENT + " element is allowed");
                    }
                    Metadata props = parseDefaultProperties(context, metadata, e);
                    metadata.addProperty("defaultProperties", props);
                }
            } else if (isExtNamespace(e.getNamespaceURI())) {
                if (nodeNameEquals(e, LOCATION_ELEMENT)) {
                    locations.add(getTextValue(e));
                }
            }
        }
    }
    if (!locations.isEmpty()) {
        metadata.addProperty("locations", createList(context, locations));
    }
    PlaceholdersUtils.validatePlaceholder(metadata, context.getComponentDefinitionRegistry());
    return metadata;
}
Also used : MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) 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) Metadata(org.osgi.service.blueprint.reflect.Metadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) MutableReferenceMetadata(org.apache.aries.blueprint.mutable.MutableReferenceMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) MutableRefMetadata(org.apache.aries.blueprint.mutable.MutableRefMetadata) IdRefMetadata(org.osgi.service.blueprint.reflect.IdRefMetadata) MutableIdRefMetadata(org.apache.aries.blueprint.mutable.MutableIdRefMetadata) MutableComponentMetadata(org.apache.aries.blueprint.mutable.MutableComponentMetadata) ReferenceMetadata(org.osgi.service.blueprint.reflect.ReferenceMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) MapMetadata(org.osgi.service.blueprint.reflect.MapMetadata) MutableCollectionMetadata(org.apache.aries.blueprint.mutable.MutableCollectionMetadata) MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) MutableValueMetadata(org.apache.aries.blueprint.mutable.MutableValueMetadata) MutableMapMetadata(org.apache.aries.blueprint.mutable.MutableMapMetadata)

Example 72 with ComponentDefinitionException

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

the class ReferenceListRecipe method internalCreate.

@Override
protected Object internalCreate() throws ComponentDefinitionException {
    try {
        if (explicitDependencies != null) {
            for (Recipe recipe : explicitDependencies) {
                recipe.create();
            }
        }
        ProvidedObject object = new ProvidedObject();
        addPartialObject(object);
        // Handle initial references
        createListeners();
        updateListeners();
        return object;
    } catch (ComponentDefinitionException t) {
        throw t;
    } catch (Throwable t) {
        throw new ComponentDefinitionException(t);
    }
}
Also used : ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) CollectionRecipe(org.apache.aries.blueprint.di.CollectionRecipe) ValueRecipe(org.apache.aries.blueprint.di.ValueRecipe) Recipe(org.apache.aries.blueprint.di.Recipe)

Example 73 with ComponentDefinitionException

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

the class ServiceRecipe method createService.

private void createService() {
    try {
        if (service == null) {
            LOGGER.debug("Creating service instance");
            // We can't use the BlueprintRepository because we don't know what interfaces
            // to use yet! We have to be a bit smarter.
            ExecutionContext old = ExecutionContext.Holder.setContext(blueprintContainer.getRepository());
            try {
                Object o = serviceRecipe.create();
                if (o instanceof Convertible) {
                    o = blueprintContainer.getRepository().convert(o, new ReifiedType(Object.class));
                    validateClasses(o);
                } else if (o instanceof UnwrapperedBeanHolder) {
                    UnwrapperedBeanHolder holder = (UnwrapperedBeanHolder) o;
                    if (holder.unwrapperedBean instanceof ServiceFactory) {
                        // If a service factory is used, make sure the proxy classes implement this
                        // interface so that later on, internalGetService will create the real
                        // service from it.
                        LOGGER.debug("{} implements ServiceFactory, creating proxy that also implements this", holder.unwrapperedBean);
                        Collection<Class<?>> cls = getClassesForProxying(holder.unwrapperedBean);
                        cls.add(blueprintContainer.loadClass("org.osgi.framework.ServiceFactory"));
                        o = BeanRecipe.wrap(holder, cls);
                    } else {
                        validateClasses(holder.unwrapperedBean);
                        o = BeanRecipe.wrap(holder, getClassesForProxying(holder.unwrapperedBean));
                    }
                } else if (!(o instanceof ServiceFactory)) {
                    validateClasses(o);
                }
                service = o;
            } catch (Exception e) {
                LOGGER.error("Error retrieving service from " + this, e);
                throw new ComponentDefinitionException(e);
            } finally {
                ExecutionContext.Holder.setContext(old);
            }
            LOGGER.debug("Service created: {}", service);
        }
        // When the service is first requested, we need to create listeners and call them
        if (!initialServiceRegistration && listeners == null) {
            LOGGER.debug("Creating listeners");
            if (listenersRecipe != null) {
                listeners = (List) createRecipe(listenersRecipe);
            } else {
                listeners = Collections.emptyList();
            }
            LOGGER.debug("Listeners created: {}", listeners);
            if (registration.get() != null) {
                LOGGER.debug("Calling listeners for initial service registration");
                for (ServiceListener listener : listeners) {
                    listener.register(service, registrationProperties);
                }
            } else {
                LOGGER.debug("Calling listeners for initial service unregistration");
                for (ServiceListener listener : listeners) {
                    listener.unregister(service, registrationProperties);
                }
            }
        }
    } catch (RuntimeException e) {
        LOGGER.error("Error retrieving service from " + this, e);
        throw e;
    }
}
Also used : ExecutionContext(org.apache.aries.blueprint.di.ExecutionContext) Convertible(org.apache.aries.blueprint.container.AggregateConverter.Convertible) ServiceListener(org.apache.aries.blueprint.utils.ServiceListener) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) ServiceFactory(org.osgi.framework.ServiceFactory) ReifiedType(org.osgi.service.blueprint.container.ReifiedType) Collection(java.util.Collection) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) UnwrapperedBeanHolder(org.apache.aries.blueprint.container.BeanRecipe.UnwrapperedBeanHolder)

Example 74 with ComponentDefinitionException

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

the class BeanRecipe method createProxyBean.

private Object createProxyBean(ReferenceRecipe rr) {
    try {
        VoidableCallable vc = new VoidableCallable();
        rr.addVoidableChild(vc);
        return blueprintContainer.getProxyManager().createDelegatingProxy(blueprintContainer.getBundleContext().getBundle(), rr.getProxyChildBeanClasses(), vc, vc.call());
    } catch (UnableToProxyException e) {
        throw new ComponentDefinitionException(e);
    }
}
Also used : ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) UnableToProxyException(org.apache.aries.proxy.UnableToProxyException)

Example 75 with ComponentDefinitionException

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

the class BeanRecipe method getInstanceFromFactory.

private Object getInstanceFromFactory(List<Object> args, List<ReifiedType> argTypes) {
    Object factoryObj = getFactoryObj();
    // Map of matching methods
    Map<Method, List<Object>> matches = findMatchingMethods(factoryObj.getClass(), factoryMethod, true, args, argTypes);
    if (matches.size() == 1) {
        try {
            Map.Entry<Method, List<Object>> match = matches.entrySet().iterator().next();
            return invoke(match.getKey(), factoryObj, match.getValue().toArray());
        } catch (Throwable e) {
            throw wrapAsCompDefEx(e);
        }
    } else if (matches.size() == 0) {
        throw new ComponentDefinitionException("Unable to find a matching factory method " + factoryMethod + " on class " + factoryObj.getClass().getName() + " for arguments " + argsToString(args) + " when instanciating bean " + getName());
    } else {
        throw new ComponentDefinitionException("Multiple matching factory methods " + factoryMethod + " found on class " + factoryObj.getClass().getName() + " for arguments " + argsToString(args) + " when instanciating bean " + getName() + ": " + matches.keySet());
    }
}
Also used : ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

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