Search in sources :

Example 51 with ComponentDefinitionException

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

the class WiringTest method testRecursive.

public void testRecursive() throws Exception {
    BlueprintRepository repository = createBlueprintContainer().getRepository();
    try {
        repository.create("recursiveConstructor");
        fail("Did not throw exception");
    } catch (ComponentDefinitionException e) {
        if (e.getCause() instanceof CircularDependencyException) {
        // that's what we expect
        } else {
            fail("Did not throw expected exception");
            throw e;
        }
    }
    PojoRecursive pojo;
    pojo = (PojoRecursive) repository.create("recursiveSetter");
    assertNotNull(pojo);
    pojo = (PojoRecursive) repository.create("recursiveInitMethod");
    assertNotNull(pojo);
}
Also used : ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) BlueprintRepository(org.apache.aries.blueprint.container.BlueprintRepository) CircularDependencyException(org.apache.aries.blueprint.di.CircularDependencyException) PojoRecursive(org.apache.aries.blueprint.pojos.PojoRecursive)

Example 52 with ComponentDefinitionException

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

the class BlueprintContainerImpl method processTypeConverters.

private void processTypeConverters() throws Exception {
    List<String> typeConverters = new ArrayList<String>();
    for (Target target : componentDefinitionRegistry.getTypeConverters()) {
        if (target instanceof ComponentMetadata) {
            typeConverters.add(((ComponentMetadata) target).getId());
        } else if (target instanceof RefMetadata) {
            typeConverters.add(((RefMetadata) target).getComponentId());
        } else {
            throw new ComponentDefinitionException("Unexpected metadata for type converter: " + target);
        }
    }
    Map<String, Object> objects = repository.createAll(typeConverters, Arrays.<Class<?>>asList(Converter.class));
    for (String name : typeConverters) {
        Object obj = objects.get(name);
        if (obj instanceof Converter) {
            converter.registerConverter((Converter) obj);
        } else {
            throw new ComponentDefinitionException("Type converter " + obj + " does not implement the " + Converter.class.getName() + " interface");
        }
    }
}
Also used : Target(org.osgi.service.blueprint.reflect.Target) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) ArrayList(java.util.ArrayList) Converter(org.osgi.service.blueprint.container.Converter) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata)

Example 53 with ComponentDefinitionException

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

the class BeanRecipeTest method protectedClassAccess.

@Test
public void protectedClassAccess() throws Exception {
    BlueprintContainerImpl container = new BlueprintContainerImpl(null, null, null, null, null, null, null, null, null, null);
    BeanRecipe recipe = new BeanRecipe("a", container, null, false);
    recipe.setFactoryComponent(new PassThroughRecipe("factory", new Factory().create()));
    recipe.setFactoryMethod("getA");
    ExecutionContext.Holder.setContext(new BlueprintRepository(container));
    assertNotNull(recipe.create());
    recipe = new BeanRecipe("b", container, null, false);
    recipe.setFactoryComponent(new PassThroughRecipe("factory", new Factory().create()));
    recipe.setFactoryMethod("getB");
    ExecutionContext.Holder.setContext(new BlueprintRepository(container));
    assertNotNull(recipe.create());
    recipe = new BeanRecipe("c", container, null, false);
    recipe.setFactoryComponent(new PassThroughRecipe("factory", new Factory().create()));
    recipe.setFactoryMethod("getC");
    ExecutionContext.Holder.setContext(new BlueprintRepository(container));
    assertNotNull(recipe.create());
    recipe = new BeanRecipe("d", container, null, false);
    recipe.setFactoryComponent(new PassThroughRecipe("factory", new Factory().create()));
    recipe.setFactoryMethod("getD");
    ExecutionContext.Holder.setContext(new BlueprintRepository(container));
    try {
        assertNotNull(recipe.create());
        fail("Should have thrown an exception");
    } catch (ComponentDefinitionException e) {
    // ok
    }
    recipe = new BeanRecipe("a", container, null, false);
    recipe.setFactoryComponent(new PassThroughRecipe("factory", new Factory()));
    recipe.setFactoryMethod("create");
    recipe.setProperty("a", "a");
    ExecutionContext.Holder.setContext(new BlueprintRepository(container));
    assertNotNull(recipe.create());
    recipe = new BeanRecipe("b", container, null, false);
    recipe.setFactoryComponent(new PassThroughRecipe("factory", new Factory()));
    recipe.setFactoryMethod("create");
    recipe.setProperty("b", "b");
    ExecutionContext.Holder.setContext(new BlueprintRepository(container));
    assertNotNull(recipe.create());
    recipe = new BeanRecipe("c", container, null, false);
    recipe.setFactoryComponent(new PassThroughRecipe("factory", new Factory()));
    recipe.setFactoryMethod("create");
    recipe.setProperty("c", "c");
    ExecutionContext.Holder.setContext(new BlueprintRepository(container));
    assertNotNull(recipe.create());
    recipe = new BeanRecipe("d", container, null, false);
    recipe.setFactoryComponent(new PassThroughRecipe("factory", new Factory()));
    recipe.setFactoryMethod("create");
    recipe.setProperty("d", "d");
    ExecutionContext.Holder.setContext(new BlueprintRepository(container));
    try {
        assertNotNull(recipe.create());
        fail("Should have thrown an exception");
    } catch (ComponentDefinitionException e) {
    // ok
    }
    recipe = new BeanRecipe("a", container, null, false);
    recipe.setFactoryComponent(new PassThroughRecipe("factory", new Factory()));
    recipe.setFactoryMethod("create");
    recipe.setInitMethod("init");
    ExecutionContext.Holder.setContext(new BlueprintRepository(container));
    assertNotNull(recipe.create());
}
Also used : ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) PassThroughRecipe(org.apache.aries.blueprint.di.PassThroughRecipe) Test(org.junit.Test)

Example 54 with ComponentDefinitionException

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

the class BeanRecipe method setProperty.

private void setProperty(Object instance, Class clazz, String propertyName, Object propertyValue) {
    String[] names = propertyName.split("\\.");
    for (int i = 0; i < names.length - 1; i++) {
        PropertyDescriptor pd = getPropertyDescriptor(clazz, names[i]);
        if (pd.allowsGet()) {
            try {
                instance = pd.get(instance, blueprintContainer);
            } catch (Exception e) {
                throw new ComponentDefinitionException("Error getting property: " + names[i] + " on bean " + getName() + " when setting property " + propertyName + " on class " + clazz.getName(), getRealCause(e));
            }
            if (instance == null) {
                throw new ComponentDefinitionException("Error setting compound property " + propertyName + " on bean " + getName() + ". Property " + names[i] + " is null");
            }
            clazz = instance.getClass();
        } else {
            throw new ComponentDefinitionException("No getter for " + names[i] + " property on bean " + getName() + " when setting property " + propertyName + " on class " + clazz.getName());
        }
    }
    // Instantiate value
    if (propertyValue instanceof Recipe) {
        propertyValue = ((Recipe) propertyValue).create();
    }
    final PropertyDescriptor pd = getPropertyDescriptor(clazz, names[names.length - 1]);
    if (pd.allowsSet()) {
        try {
            pd.set(instance, propertyValue, blueprintContainer);
        } catch (Exception e) {
            throw new ComponentDefinitionException("Error setting property: " + pd, getRealCause(e));
        }
    } else {
        throw new ComponentDefinitionException("No setter for " + names[names.length - 1] + " property");
    }
}
Also used : PropertyDescriptor(org.apache.aries.blueprint.utils.ReflectionUtils.PropertyDescriptor) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) Recipe(org.apache.aries.blueprint.di.Recipe) AbstractRecipe(org.apache.aries.blueprint.di.AbstractRecipe) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnableToProxyException(org.apache.aries.proxy.UnableToProxyException)

Example 55 with ComponentDefinitionException

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

the class BeanRecipe method getInstance.

private Object getInstance() throws ComponentDefinitionException {
    Object instance;
    // Instanciate arguments
    List<Object> args = new ArrayList<Object>();
    List<ReifiedType> argTypes = new ArrayList<ReifiedType>();
    if (arguments != null) {
        for (int i = 0; i < arguments.size(); i++) {
            Object arg = arguments.get(i);
            if (arg instanceof Recipe) {
                args.add(((Recipe) arg).create());
            } else {
                args.add(arg);
            }
            if (this.argTypes != null) {
                argTypes.add(this.argTypes.get(i) != null ? loadType(this.argTypes.get(i)) : null);
            }
        }
    }
    if (factory != null) {
        // look for instance method on factory object
        Object factoryObj = factory.create();
        /* BLUEPRINT-NOOSGI
            if (factoryObj instanceof ReferenceRecipe.ServiceProxyWrapper) {
                try {
                    factoryObj = ((ReferenceRecipe.ServiceProxyWrapper) factoryObj).convert(new ReifiedType(Object.class));
                } catch (Exception e) {
                    throw new ComponentDefinitionException("Error when instantiating bean " + getName() + " of class " + getType(), getRealCause(e));
                }
            } else*/
        if (factoryObj instanceof UnwrapperedBeanHolder) {
            factoryObj = wrap((UnwrapperedBeanHolder) factoryObj, Object.class);
        }
        // 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();
                instance = invoke(match.getKey(), factoryObj, match.getValue().toArray());
            } catch (Throwable e) {
                throw new ComponentDefinitionException("Error when instantiating bean " + getName() + " of class " + getType(), getRealCause(e));
            }
        } else if (matches.size() == 0) {
            throw new ComponentDefinitionException("Unable to find a matching factory method " + factoryMethod + " on class " + factoryObj.getClass().getName() + " for arguments " + args + " when instanciating bean " + getName());
        } else {
            throw new ComponentDefinitionException("Multiple matching factory methods " + factoryMethod + " found on class " + factoryObj.getClass().getName() + " for arguments " + args + " when instanciating bean " + getName() + ": " + matches.keySet());
        }
    } else if (factoryMethod != null) {
        // Map of matching methods
        Map<Method, List<Object>> matches = findMatchingMethods(getType(), factoryMethod, false, args, argTypes);
        if (matches.size() == 1) {
            try {
                Map.Entry<Method, List<Object>> match = matches.entrySet().iterator().next();
                instance = invoke(match.getKey(), null, match.getValue().toArray());
            } catch (Throwable e) {
                throw new ComponentDefinitionException("Error when instanciating bean " + getName() + " of class " + getType(), getRealCause(e));
            }
        } else if (matches.size() == 0) {
            throw new ComponentDefinitionException("Unable to find a matching factory method " + factoryMethod + " on class " + getType().getName() + " for arguments " + args + " when instanciating bean " + getName());
        } else {
            throw new ComponentDefinitionException("Multiple matching factory methods " + factoryMethod + " found on class " + getType().getName() + " for arguments " + args + " when instanciating bean " + getName() + ": " + matches.keySet());
        }
    } else {
        if (getType() == null) {
            throw new ComponentDefinitionException("No factoryMethod nor class is defined for this bean");
        }
        // Map of matching constructors
        Map<Constructor, List<Object>> matches = findMatchingConstructors(getType(), args, argTypes);
        if (matches.size() == 1) {
            try {
                Map.Entry<Constructor, List<Object>> match = matches.entrySet().iterator().next();
                instance = newInstance(match.getKey(), match.getValue().toArray());
            } catch (Throwable e) {
                throw new ComponentDefinitionException("Error when instanciating bean " + getName() + " of class " + getType(), getRealCause(e));
            }
        } else if (matches.size() == 0) {
            throw new ComponentDefinitionException("Unable to find a matching constructor on class " + getType().getName() + " for arguments " + args + " when instanciating bean " + getName());
        } else {
            throw new ComponentDefinitionException("Multiple matching constructors found on class " + getType().getName() + " for arguments " + args + " when instanciating bean " + getName() + ": " + matches.keySet());
        }
    }
    return instance;
}
Also used : ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) Recipe(org.apache.aries.blueprint.di.Recipe) AbstractRecipe(org.apache.aries.blueprint.di.AbstractRecipe) ReifiedType(org.osgi.service.blueprint.container.ReifiedType) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) 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)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