use of org.osgi.service.blueprint.container.ComponentDefinitionException in project aries by apache.
the class BlueprintRepository method validate.
public void validate() {
for (Recipe recipe : getAllRecipes()) {
// Check that references are satisfied
String ref = null;
if (recipe instanceof RefRecipe) {
ref = ((RefRecipe) recipe).getIdRef();
} else if (recipe instanceof IdRefRecipe) {
ref = ((IdRefRecipe) recipe).getIdRef();
}
if (ref != null && getRecipe(ref) == null) {
throw new ComponentDefinitionException("Unresolved ref/idref to component: " + ref);
}
// Check service
if (recipe instanceof ServiceRecipe) {
Recipe r = ((ServiceRecipe) recipe).getServiceRecipe();
if (r instanceof RefRecipe) {
r = getRecipe(((RefRecipe) r).getIdRef());
}
if (r instanceof ServiceRecipe) {
throw new ComponentDefinitionException("The target for a <service> element must not be <service> element");
}
if (r instanceof ReferenceListRecipe) {
throw new ComponentDefinitionException("The target for a <service> element must not be <reference-list> element");
}
CollectionRecipe listeners = ((ServiceRecipe) recipe).getListenersRecipe();
for (Recipe lr : listeners.getDependencies()) {
// The listener recipe is a bean recipe with the listener being set in a property
for (Recipe l : lr.getDependencies()) {
if (l instanceof RefRecipe) {
l = getRecipe(((RefRecipe) l).getIdRef());
}
if (l instanceof ServiceRecipe) {
throw new ComponentDefinitionException("The target for a <registration-listener> element must not be <service> element");
}
if (l instanceof ReferenceListRecipe) {
throw new ComponentDefinitionException("The target for a <registration-listener> element must not be <reference-list> element");
}
}
}
}
// Check references
if (recipe instanceof AbstractServiceReferenceRecipe) {
CollectionRecipe listeners = ((AbstractServiceReferenceRecipe) recipe).getListenersRecipe();
for (Recipe lr : listeners.getDependencies()) {
// The listener recipe is a bean recipe with the listener being set in a property
for (Recipe l : lr.getDependencies()) {
if (l instanceof RefRecipe) {
l = getRecipe(((RefRecipe) l).getIdRef());
}
if (l instanceof ServiceRecipe) {
throw new ComponentDefinitionException("The target for a <reference-listener> element must not be <service> element");
}
if (l instanceof ReferenceListRecipe) {
throw new ComponentDefinitionException("The target for a <reference-listener> element must not be <reference-list> element");
}
}
}
}
}
}
use of org.osgi.service.blueprint.container.ComponentDefinitionException in project aries by apache.
the class BlueprintRepository method destroyInstance.
public void destroyInstance(String name) {
ExecutionContext oldContext = ExecutionContext.Holder.setContext(this);
try {
Map<Recipe, Future<Object>> toDestroy = new LinkedHashMap<Recipe, Future<Object>>();
doGetInstancesToDestroy(toDestroy, name);
for (Map.Entry<Recipe, Future<Object>> entry : toDestroy.entrySet()) {
try {
Recipe recipe = entry.getKey();
Future<Object> future = entry.getValue();
Object instance = future.get();
if (instance != null) {
recipe.destroy(instance);
}
} catch (Exception e) {
throw new ComponentDefinitionException("Error destroying instance", e);
}
}
} finally {
ExecutionContext.Holder.setContext(oldContext);
}
}
use of org.osgi.service.blueprint.container.ComponentDefinitionException in project aries by apache.
the class ArrayRecipe method internalCreate.
protected Object internalCreate() throws ComponentDefinitionException {
ReifiedType type;
if (this.type instanceof Class) {
type = new ReifiedType((Class) this.type);
} else if (this.type instanceof String) {
type = loadType((String) this.type);
} else {
type = new ReifiedType(Object.class);
}
// create array instance
Object array;
try {
array = Array.newInstance(type.getRawClass(), list.size());
} catch (Exception e) {
throw new ComponentDefinitionException("Error while creating array instance: " + type);
}
int index = 0;
for (Recipe recipe : list) {
Object value;
if (recipe != null) {
try {
value = convert(recipe.create(), type);
} catch (Exception e) {
throw new ComponentDefinitionException("Unable to convert value " + recipe + " to type " + type, e);
}
} else {
value = null;
}
Array.set(array, index, value);
index++;
}
return array;
}
use of org.osgi.service.blueprint.container.ComponentDefinitionException in project aries by apache.
the class CollectionRecipe method internalCreate.
protected Object internalCreate() throws ComponentDefinitionException {
Class type = getCollection(collectionTypeClass);
if (!ReflectionUtils.hasDefaultConstructor(type)) {
throw new ComponentDefinitionException("Type does not have a default constructor " + type.getName());
}
// create collection instance
Object o;
try {
o = type.newInstance();
} catch (Exception e) {
throw new ComponentDefinitionException("Error while creating collection instance: " + type.getName());
}
if (!(o instanceof Collection)) {
throw new ComponentDefinitionException("Specified collection type does not implement the Collection interface: " + type.getName());
}
Collection instance = (Collection) o;
ReifiedType defaultConversionType = loadType(defaultValueType);
Type conversionType = null;
for (Recipe recipe : list) {
Object value;
if (recipe != null) {
try {
conversionType = defaultConversionType.getRawClass();
if (recipe instanceof ValueRecipe) {
conversionType = ((ValueRecipe) recipe).getValueType();
}
value = convert(recipe.create(), conversionType);
} catch (Exception e) {
throw new ComponentDefinitionException("Unable to convert value " + recipe + " to type " + conversionType, e);
}
} else {
value = null;
}
instance.add(value);
}
return instance;
}
use of org.osgi.service.blueprint.container.ComponentDefinitionException in project aries by apache.
the class ValueRecipe method internalCreate.
@Override
protected Object internalCreate() throws ComponentDefinitionException {
try {
Type type = getValueType();
Object v = null;
if (value instanceof ExtendedValueMetadata) {
v = ((ExtendedValueMetadata) value).getValue();
}
if (v == null) {
v = value.getStringValue();
}
return convert(v, type);
} catch (Exception e) {
throw new ComponentDefinitionException(e);
}
}
Aggregations