private void sucess(T result) { complete(result, null); }
@Inject public AsyncReferenceImpl(InjectionPoint injectionPoint, Vertx vertx, BeanManager beanManager) { this.isDone = new AtomicBoolean(false); this.future = new VertxCompletableFuture<>(vertx); this.creationalContext = beanManager.createCreationalContext(null); ParameterizedType parameterizedType = (ParameterizedType) injectionPoint.getType(); Type requiredType = parameterizedType.getActualTypeArguments()[0]; // First check if there is a relevant async producer method available List<AsyncProducerMetadata> foundMetadata = beanManager.getExtension(VertxExtension.class).getAsyncProducerMetadata(requiredType, injectionPoint.getQualifiers()); if (foundMetadata.size() > 1) { failure(new AmbiguousResolutionException( "Ambiguous async producer methods for type " + requiredType + " with qualifiers " + injectionPoint.getQualifiers())); } else if (foundMetadata.size() == 1) { // Use the produced CompletionStage initWithCompletionStage(foundMetadata.get(0), beanManager); } else { // Use Vertx worker thread initWithWorker(injectionPoint, vertx, beanManager, requiredType); } }
@SuppressWarnings("unchecked") private void initWithCompletionStage(AsyncProducerMetadata metadata, BeanManager beanManager) { Bean<CompletionStage<T>> bean = (Bean<CompletionStage<T>>) beanManager .resolve(beanManager.getBeans(metadata.producerType, metadata.qualifiers.toArray(new Annotation[] {}))); Object possibleStage = beanManager.getReference(bean, metadata.producerType, creationalContext); if (possibleStage instanceof CompletionStage) { ((CompletionStage<T>) possibleStage).whenComplete((result, throwable) -> { if (throwable != null) { failure(throwable); } else { sucess(result); } }); } else { throw new IllegalStateException("The contextual reference of " + bean + " is not a CompletionStage"); } }
@Inject public AsyncReferenceImpl(InjectionPoint injectionPoint, Vertx vertx, BeanManager beanManager, @Any WeldInstance<Object> instance) { this.isDone = new AtomicBoolean(false); this.future = new VertxCompletableFuture<>(vertx); this.instance = instance; ParameterizedType parameterizedType = (ParameterizedType) injectionPoint.getType(); Type requiredType = parameterizedType.getActualTypeArguments()[0]; Annotation[] qualifiers = injectionPoint.getQualifiers().toArray(new Annotation[] {}); // First check if there is a relevant async producer method available WeldInstance<Object> completionStage = instance.select(new ParameterizedTypeImpl(CompletionStage.class, requiredType), qualifiers); if (completionStage.isAmbiguous()) { failure(new AmbiguousResolutionException( "Ambiguous async producer methods for type " + requiredType + " with qualifiers " + injectionPoint.getQualifiers())); } else if (!completionStage.isUnsatisfied()) { // Use the produced CompletionStage initWithCompletionStage(completionStage.getHandler()); } else { // Use Vertx worker thread initWithWorker(requiredType, qualifiers, vertx, beanManager); } }
@SuppressWarnings("unchecked") private void initWithCompletionStage(Handler<Object> completionStage) { Object possibleStage = completionStage.get(); if (possibleStage instanceof CompletionStage) { ((CompletionStage<T>) possibleStage).whenComplete((result, throwable) -> { if (throwable != null) { failure(throwable); } else { sucess(result); } }); } else { throw new IllegalStateException("The contextual reference of " + completionStage.getBean() + " is not a CompletionStage"); } }
@SuppressWarnings("unchecked") private void initWithWorker(InjectionPoint injectionPoint, Vertx vertx, BeanManager beanManager, Type requiredType) { vertx.<T> executeBlocking((f -> { Set<Bean<?>> beans = beanManager.getBeans(requiredType, injectionPoint.getQualifiers().toArray(new Annotation[] {})); if (beans.isEmpty()) { f.fail(BeanManagerLogger.LOG.injectionPointHasUnsatisfiedDependencies(injectionPoint.getQualifiers(), requiredType, "")); return; } Bean<T> bean = (Bean<T>) beanManager.resolve(beans); T beanInstance = (T) beanManager.getReference(bean, requiredType, creationalContext); if (beanManager.isNormalScope(bean.getScope()) && beanInstance instanceof TargetInstanceProxy) { // Initialize normal scoped bean instance eagerly ((TargetInstanceProxy<?>) beanInstance).getTargetInstance(); } f.complete(beanInstance); }), (r) -> { if (r.succeeded()) { sucess(r.result()); } else { failure(r.cause()); } }); }
private void failure(Throwable cause) { complete(null, cause); }
@SuppressWarnings("unchecked") private void initWithWorker(Type requiredType, Annotation[] qualifiers, Vertx vertx, BeanManager beanManager) { vertx.<Object> executeBlocking((f -> { WeldInstance<Object> asyncInstance = instance.select(requiredType, qualifiers); if (asyncInstance.isUnsatisfied()) { f.fail(BeanManagerLogger.LOG.injectionPointHasUnsatisfiedDependencies(Arrays.toString(qualifiers), requiredType, "")); return; } else if (asyncInstance.isAmbiguous()) { f.fail(BeanManagerLogger.LOG.injectionPointHasAmbiguousDependencies(Arrays.toString(qualifiers), requiredType, "")); return; } Handler<Object> handler = asyncInstance.getHandler(); Object beanInstance = handler.get(); if (beanManager.isNormalScope(handler.getBean().getScope()) && beanInstance instanceof TargetInstanceProxy) { // Initialize normal scoped bean instance eagerly ((TargetInstanceProxy<?>) beanInstance).getTargetInstance(); } f.complete(beanInstance); }), (r) -> { if (r.succeeded()) { sucess((T) r.result()); } else { failure(r.cause()); } }); }
private void sucess(T result) { complete(result, null); }
private void failure(Throwable cause) { complete(null, cause); }