use of org.osgi.service.blueprint.container.ComponentDefinitionException in project camel by apache.
the class CamelNamespaceHandler method parseRouteContextNode.
private Metadata parseRouteContextNode(Element element, ParserContext context) {
LOG.trace("Parsing RouteContext {}", element);
// now parse the routes with JAXB
Binder<Node> binder;
try {
binder = getJaxbContext().createBinder();
} catch (JAXBException e) {
throw new ComponentDefinitionException("Failed to create the JAXB binder : " + e, e);
}
Object value = parseUsingJaxb(element, context, binder);
if (!(value instanceof CamelRouteContextFactoryBean)) {
throw new ComponentDefinitionException("Expected an instance of " + CamelRouteContextFactoryBean.class);
}
CamelRouteContextFactoryBean rcfb = (CamelRouteContextFactoryBean) value;
String id = rcfb.getId();
MutablePassThroughMetadata factory = context.createMetadata(MutablePassThroughMetadata.class);
factory.setId(".camelBlueprint.passThrough." + id);
factory.setObject(new PassThroughCallable<Object>(rcfb));
MutableBeanMetadata factory2 = context.createMetadata(MutableBeanMetadata.class);
factory2.setId(".camelBlueprint.factory." + id);
factory2.setFactoryComponent(factory);
factory2.setFactoryMethod("call");
MutableBeanMetadata ctx = context.createMetadata(MutableBeanMetadata.class);
ctx.setId(id);
ctx.setRuntimeClass(List.class);
ctx.setFactoryComponent(factory2);
ctx.setFactoryMethod("getRoutes");
// must be lazy as we want CamelContext to be activated first
ctx.setActivation(ACTIVATION_LAZY);
// lets inject the namespaces into any namespace aware POJOs
injectNamespaces(element, binder);
LOG.trace("Parsing RouteContext done, returning {}", element, ctx);
return ctx;
}
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);
}
}
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;
}
}
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;
}
Aggregations