static <T> Optional<T> getReferenceByName(BeanManager manager, String name, Class<T> type) { return Optional.of(manager.getBeans(name)) .map(manager::resolve) .map(bean -> getReference(manager, type, bean)); }
contexts.add(getReference(manager, CamelContext.class, context)); .forEach(bean -> getReference(manager, bean.getBeanClass(), bean).toString());
@Override public T create(CreationalContext<T> creationalContext) { try { CamelContext context = isNotEmpty(exporter.getCamelContextId()) ? getReferenceByName(manager, exporter.getCamelContextId(), CamelContext.class).get() : getReference(manager, CamelContext.class, this.context); Bean<?> bean = manager.resolve(manager.getBeans(exporter.getServiceRef())); if (bean == null) { throw new UnsatisfiedResolutionException("No bean with name [" + exporter.getServiceRef() + "] is deployed!"); } @SuppressWarnings("unchecked") T service = (T) manager.getReference(bean, type, manager.createCreationalContext(bean)); Endpoint endpoint = getMandatoryEndpoint(context, exporter.getUri()); try { // need to start endpoint before we create consumer startService(endpoint); Consumer consumer = endpoint.createConsumer(new BeanProcessor(service, context)); // add and start consumer context.addService(consumer, true, true); } catch (Exception cause) { throw new FailedToCreateConsumerException(endpoint, cause); } return service; } catch (Exception cause) { throw new CreationException("Error while creating instance for " + this, cause); } }
CamelContext context = isNotEmpty(proxy.getCamelContextId()) ? getReferenceByName(manager, proxy.getCamelContextId(), CamelContext.class).get() : getReference(manager, CamelContext.class, this.context);
static <T> Optional<T> getReferenceByType(BeanManager manager, Class<T> type, Annotation... qualifiers) { return Optional.of(manager.getBeans(type, qualifiers)) .map(manager::resolve) .map(bean -> getReference(manager, type, bean)); }
static <T> Set<T> getReferencesByType(BeanManager manager, Class<T> type, Annotation... qualifiers) { return manager.getBeans(type, qualifiers).stream() .map(bean -> getReference(manager, type, bean)) .collect(toSet()); }
@Override protected Map<String, CamelContext> getCamelContextMap() { BeanManager manager = cdiContainer.getBeanManager(); return manager.getBeans(CamelContext.class, ANY).stream() .map(bean -> getReference(manager, CamelContext.class, bean)) .collect(toMap(CamelContext::getName, identity())); }
@Override public <T> Map<String, T> findByTypeWithName(Class<T> type) { notNull(type, "type"); logger.trace("Looking up named beans of type [{}]", type); return manager.getBeans(type, ANY).stream() .filter(bean -> bean.getName() != null) .collect(toMap(Bean::getName, bean -> getReference(manager, type, bean))); }
private boolean addRouteToContext(Bean<?> routeBean, Bean<?> contextBean, BeanManager manager, AfterDeploymentValidation adv) { try { CamelContext context = getReference(manager, CamelContext.class, contextBean); try { Object route = getReference(manager, Object.class, routeBean); if (route instanceof RoutesBuilder) { context.addRoutes((RoutesBuilder) route); } else if (route instanceof RouteContainer) { context.addRouteDefinitions(((RouteContainer) route).getRoutes()); } else { throw new IllegalArgumentException( "Invalid routes type [" + routeBean.getBeanClass().getName() + "], " + "must be either of type RoutesBuilder or RouteContainer!"); } return true; } catch (Exception cause) { adv.addDeploymentProblem( new InjectionException( "Error adding routes of type [" + routeBean.getBeanClass().getName() + "] " + "to Camel context [" + context.getName() + "]", cause)); } } catch (Exception exception) { adv.addDeploymentProblem(exception); } return false; } }
XmlFactoryBeanInjectionTarget(BeanManager manager, AbstractCamelFactoryBean<T> factory, Bean<?> context) { super( () -> { try { if (isEmpty(factory.getCamelContextId()) && context != null) { factory.setCamelContext(getReference(manager, CamelContext.class, context)); } factory.afterPropertiesSet(); return factory.getObject(); } catch (Exception cause) { throw new CreationException(cause); } }, i -> { }, i -> { try { factory.destroy(); } catch (Exception cause) { throw new InjectionException(cause); } } ); } }