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);
}
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");
}
}
}
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());
}
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");
}
}
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;
}
Aggregations