use of jakarta.enterprise.inject.spi.BeanManager in project helidon by oracle.
the class GrpcServiceBuilder method addServiceMethod.
/**
* Add a method to the {@link ServiceDescriptor.Builder}.
* <p>
* The method configuration will be determined by the annotations present on the
* method and the method signature.
*
* @param builder the {@link ServiceDescriptor.Builder} to add the method to
* @param method the {@link AnnotatedMethod} representing the method to add
* @param beanManager the {@link jakarta.enterprise.inject.spi.BeanManager} to use
* to look-up CDI beans.
*/
@SuppressWarnings("unchecked")
private void addServiceMethod(ServiceDescriptor.Builder builder, AnnotatedMethod method, BeanManager beanManager) {
GrpcMethod annotation = method.firstAnnotationOrMetaAnnotation(GrpcMethod.class);
String name = determineMethodName(method, annotation);
Supplier<?> instanceSupplier = instanceSupplier();
MethodHandler handler = handlerSuppliers().stream().filter(supplier -> supplier.supplies(method)).findFirst().map(supplier -> supplier.get(name, method, instanceSupplier)).orElseThrow(() -> new IllegalArgumentException("Cannot locate a method handler supplier for method " + method));
Class<?> requestType = handler.getRequestType();
Class<?> responseType = handler.getResponseType();
List<ServerInterceptor> interceptors = lookupMethodInterceptors(beanManager, method);
GrpcInterceptors grpcInterceptors = method.getAnnotation(GrpcInterceptors.class);
if (grpcInterceptors != null) {
for (Class<?> interceptorClass : grpcInterceptors.value()) {
ServerInterceptor interceptor = resolveInterceptor(beanManager, interceptorClass);
if (interceptor != null) {
interceptors.add(interceptor);
}
}
}
AnnotatedMethodConfigurer configurer = new AnnotatedMethodConfigurer(method, requestType, responseType, interceptors);
switch(annotation.type()) {
case UNARY:
builder.unary(name, handler, configurer);
break;
case CLIENT_STREAMING:
builder.clientStreaming(name, handler, configurer);
break;
case SERVER_STREAMING:
builder.serverStreaming(name, handler, configurer);
break;
case BIDI_STREAMING:
builder.bidirectional(name, handler, configurer);
break;
case UNKNOWN:
default:
LOGGER.log(Level.SEVERE, () -> "Unrecognized method type " + annotation.type());
}
}
use of jakarta.enterprise.inject.spi.BeanManager in project helidon by oracle.
the class SchedulingCdiExtension method invoke.
void invoke(@Observes @Priority(PLATFORM_AFTER + 4000) @Initialized(ApplicationScoped.class) Object event, BeanManager beanManager) {
ScheduledThreadPoolSupplier scheduledThreadPoolSupplier = ScheduledThreadPoolSupplier.builder().threadNamePrefix(schedulingConfig.get("thread-name-prefix").asString().orElse("scheduled-")).config(schedulingConfig).build();
for (AnnotatedMethod<?> am : methods) {
Class<?> aClass = am.getDeclaringType().getJavaClass();
Bean<?> bean = beans.get(am);
Object beanInstance = lookup(bean, beanManager);
ScheduledExecutorService executorService = scheduledThreadPoolSupplier.get();
executors.add(executorService);
Method method = am.getJavaMember();
if (!method.trySetAccessible()) {
throw new DeploymentException(String.format("Scheduled method %s#%s is not accessible!", method.getDeclaringClass().getName(), method.getName()));
}
if (am.isAnnotationPresent(FixedRate.class) && am.isAnnotationPresent(Scheduled.class)) {
throw new DeploymentException(String.format("Scheduled method %s#%s can have only one scheduling annotation.", method.getDeclaringClass().getName(), method.getName()));
}
Config methodConfig = config.get(aClass.getName() + "." + method.getName() + ".schedule");
if (am.isAnnotationPresent(FixedRate.class)) {
FixedRate annotation = am.getAnnotation(FixedRate.class);
long initialDelay = methodConfig.get("initial-delay").asLong().orElseGet(annotation::initialDelay);
long delay = methodConfig.get("delay").asLong().orElseGet(annotation::value);
TimeUnit timeUnit = methodConfig.get("time-unit").asString().map(TimeUnit::valueOf).orElseGet(annotation::timeUnit);
Task task = Scheduling.fixedRateBuilder().executor(executorService).initialDelay(initialDelay).delay(delay).timeUnit(timeUnit).task(inv -> invokeWithOptionalParam(beanInstance, method, inv)).build();
LOGGER.log(Level.FINE, () -> String.format("Method %s#%s scheduled to be executed %s", aClass.getSimpleName(), method.getName(), task.description()));
} else if (am.isAnnotationPresent(Scheduled.class)) {
Scheduled annotation = am.getAnnotation(Scheduled.class);
String cron = methodConfig.get("cron").asString().orElseGet(() -> resolvePlaceholders(annotation.value(), config));
boolean concurrent = methodConfig.get("concurrent").asBoolean().orElseGet(annotation::concurrentExecution);
Task task = Scheduling.cronBuilder().executor(executorService).concurrentExecution(concurrent).expression(cron).task(inv -> invokeWithOptionalParam(beanInstance, method, inv)).build();
LOGGER.log(Level.FINE, () -> String.format("Method %s#%s scheduled to be executed %s", aClass.getSimpleName(), method.getName(), task.description()));
}
}
}
use of jakarta.enterprise.inject.spi.BeanManager in project helidon by oracle.
the class HelidonTestNGListener method injectTestToCdi.
/*
* Helper method to inject TestNG initialized classes into CDI Container.
*/
private <T> void injectTestToCdi(Object bean, final Class<T> clazz) {
BeanManager beanManager = CDI.current().getBeanManager();
AnnotatedType<T> annotatedType = beanManager.createAnnotatedType(clazz);
InjectionTargetFactory<T> injectionTargetFactory = beanManager.getInjectionTargetFactory(annotatedType);
InjectionTarget<T> injectionTarget = injectionTargetFactory.createInjectionTarget(null);
CreationalContext<T> creationalContext = beanManager.createCreationalContext(null);
injectionTarget.inject((T) bean, creationalContext);
}
use of jakarta.enterprise.inject.spi.BeanManager in project helidon by oracle.
the class AnnotatedServiceTest method descriptor.
private static ServiceDescriptor descriptor(Class<?> cls) {
BeanManager beanManager = mock(BeanManager.class);
Instance instance = mock(Instance.class);
when(beanManager.createInstance()).thenReturn(instance);
GrpcServiceBuilder builder = GrpcServiceBuilder.create(cls, beanManager);
return builder.build();
}
use of jakarta.enterprise.inject.spi.BeanManager in project helidon by oracle.
the class InterceptorsTest method shouldUseSpecificServiceInterceptorBean.
@Test
public void shouldUseSpecificServiceInterceptorBean() {
BeanManager beanManager = weld.getBeanManager();
GrpcServiceBuilder builder = GrpcServiceBuilder.create(InterceptedServiceFive.class, beanManager);
ServiceDescriptor descriptor = builder.build();
PriorityBag<ServerInterceptor> interceptors = descriptor.interceptors();
boolean hasInterceptorOne = interceptors.stream().anyMatch(interceptor -> ServerInterceptorOne.class.isAssignableFrom(interceptor.getClass()));
boolean hasInterceptorTwo = interceptors.stream().anyMatch(interceptor -> ServerInterceptorTwo.class.isAssignableFrom(interceptor.getClass()));
assertThat(hasInterceptorOne, is(true));
assertThat(hasInterceptorTwo, is(true));
assertThat(sizeOf(descriptor.method("foo").interceptors()), is(0));
}
Aggregations