Search in sources :

Example 6 with AnnotationValue

use of io.micronaut.core.annotation.AnnotationValue in project micronaut-core by micronaut-projects.

the class AbstractEnvironmentAnnotationMetadata method findAnnotation.

@Override
@NonNull
public <T extends Annotation> Optional<AnnotationValue<T>> findAnnotation(@NonNull String annotation) {
    ArgumentUtils.requireNonNull("annotation", annotation);
    Environment env = getEnvironment();
    Optional<AnnotationValue<T>> values = environmentAnnotationMetadata.findAnnotation(annotation);
    if (env != null) {
        return values.map(av -> new EnvironmentAnnotationValue<>(env, av));
    }
    return values;
}
Also used : Environment(io.micronaut.context.env.Environment) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) NonNull(io.micronaut.core.annotation.NonNull)

Example 7 with AnnotationValue

use of io.micronaut.core.annotation.AnnotationValue in project micronaut-core by micronaut-projects.

the class AbstractEnvironmentAnnotationMetadata method getAnnotationValuesByType.

@Override
@NonNull
public <T extends Annotation> List<AnnotationValue<T>> getAnnotationValuesByType(@NonNull Class<T> annotationType) {
    ArgumentUtils.requireNonNull("annotationType", annotationType);
    Environment environment = getEnvironment();
    List<AnnotationValue<T>> values = environmentAnnotationMetadata.getAnnotationValuesByType(annotationType);
    if (environment != null) {
        return values.stream().map(entries -> new EnvironmentAnnotationValue<>(environment, entries)).collect(Collectors.toList());
    }
    return values;
}
Also used : Environment(io.micronaut.context.env.Environment) java.util(java.util) Array(java.lang.reflect.Array) Internal(io.micronaut.core.annotation.Internal) OptionalValues(io.micronaut.core.value.OptionalValues) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) NonNull(io.micronaut.core.annotation.NonNull) StringUtils(io.micronaut.core.util.StringUtils) Stream(java.util.stream.Stream) Nullable(io.micronaut.core.annotation.Nullable) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) Annotation(java.lang.annotation.Annotation) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) ArgumentUtils(io.micronaut.core.util.ArgumentUtils) PropertyPlaceholderResolver(io.micronaut.context.env.PropertyPlaceholderResolver) Argument(io.micronaut.core.type.Argument) Environment(io.micronaut.context.env.Environment) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) NonNull(io.micronaut.core.annotation.NonNull)

Example 8 with AnnotationValue

use of io.micronaut.core.annotation.AnnotationValue in project micronaut-core by micronaut-projects.

the class AnnotationMetadataHierarchy method getAnnotationValuesByName.

@Override
public <T extends Annotation> List<AnnotationValue<T>> getAnnotationValuesByName(String annotationType) {
    if (annotationType == null) {
        return Collections.emptyList();
    }
    List<AnnotationValue<T>> list = new ArrayList<>(10);
    Set<AnnotationValue<T>> uniqueValues = new HashSet<>(10);
    for (AnnotationMetadata am : hierarchy) {
        for (AnnotationValue<T> tAnnotationValue : am.<T>getAnnotationValuesByName(annotationType)) {
            if (uniqueValues.add(tAnnotationValue)) {
                list.add(tAnnotationValue);
            }
        }
    }
    return Collections.unmodifiableList(list);
}
Also used : AnnotationValue(io.micronaut.core.annotation.AnnotationValue) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata)

Example 9 with AnnotationValue

use of io.micronaut.core.annotation.AnnotationValue in project micronaut-core by micronaut-projects.

the class EnvironmentConvertibleValuesMap method get.

@Override
public <T> Optional<T> get(CharSequence name, ArgumentConversionContext<T> conversionContext) {
    V value = map.get(name);
    if (value instanceof AnnotationClassValue) {
        AnnotationClassValue acv = (AnnotationClassValue) value;
        return environment.convert(acv, conversionContext);
    } else if (value instanceof CharSequence) {
        PropertyPlaceholderResolver placeholderResolver = environment.getPlaceholderResolver();
        String str = doResolveIfNecessary((CharSequence) value, placeholderResolver);
        return environment.convert(str, conversionContext);
    } else if (value instanceof String[]) {
        PropertyPlaceholderResolver placeholderResolver = environment.getPlaceholderResolver();
        String[] resolved = Arrays.stream((String[]) value).flatMap(val -> {
            try {
                String[] values = placeholderResolver.resolveRequiredPlaceholder(val, String[].class);
                return Arrays.stream(values);
            } catch (ConfigurationException e) {
                return Stream.of(doResolveIfNecessary(val, placeholderResolver));
            }
        }).toArray(String[]::new);
        return environment.convert(resolved, conversionContext);
    } else if (value instanceof io.micronaut.core.annotation.AnnotationValue[]) {
        io.micronaut.core.annotation.AnnotationValue[] annotationValues = (io.micronaut.core.annotation.AnnotationValue[]) value;
        io.micronaut.core.annotation.AnnotationValue[] b = new AnnotationValue[annotationValues.length];
        for (int i = 0; i < annotationValues.length; i++) {
            io.micronaut.core.annotation.AnnotationValue annotationValue = annotationValues[i];
            b[i] = new EnvironmentAnnotationValue(environment, annotationValue);
        }
        return environment.convert(b, conversionContext);
    } else if (value instanceof io.micronaut.core.annotation.AnnotationValue) {
        io.micronaut.core.annotation.AnnotationValue av = (io.micronaut.core.annotation.AnnotationValue) value;
        av = new EnvironmentAnnotationValue(environment, av);
        return environment.convert(av, conversionContext);
    } else {
        return super.get(name, conversionContext);
    }
}
Also used : Environment(io.micronaut.context.env.Environment) ConfigurationException(io.micronaut.context.exceptions.ConfigurationException) ConvertibleValues(io.micronaut.core.convert.value.ConvertibleValues) Arrays(java.util.Arrays) ConvertibleValuesMap(io.micronaut.core.convert.value.ConvertibleValuesMap) ArgumentConversionContext(io.micronaut.core.convert.ArgumentConversionContext) Collection(java.util.Collection) Internal(io.micronaut.core.annotation.Internal) Collectors(java.util.stream.Collectors) ConversionContext(io.micronaut.core.convert.ConversionContext) Stream(java.util.stream.Stream) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) Map(java.util.Map) Optional(java.util.Optional) PropertyPlaceholderResolver(io.micronaut.context.env.PropertyPlaceholderResolver) Argument(io.micronaut.core.type.Argument) AnnotationClassValue(io.micronaut.core.annotation.AnnotationClassValue) PropertyPlaceholderResolver(io.micronaut.context.env.PropertyPlaceholderResolver) AnnotationClassValue(io.micronaut.core.annotation.AnnotationClassValue) ConfigurationException(io.micronaut.context.exceptions.ConfigurationException) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) AnnotationValue(io.micronaut.core.annotation.AnnotationValue)

Example 10 with AnnotationValue

use of io.micronaut.core.annotation.AnnotationValue in project micronaut-core by micronaut-projects.

the class DefaultRetryInterceptor method intercept.

@Nullable
@Override
public Object intercept(MethodInvocationContext<Object, Object> context) {
    Optional<AnnotationValue<Retryable>> opt = context.findAnnotation(Retryable.class);
    if (!opt.isPresent()) {
        return context.proceed();
    }
    AnnotationValue<Retryable> retry = opt.get();
    boolean isCircuitBreaker = context.hasStereotype(CircuitBreaker.class);
    MutableRetryState retryState;
    AnnotationRetryStateBuilder retryStateBuilder = new AnnotationRetryStateBuilder(context);
    if (isCircuitBreaker) {
        long timeout = context.getValue(CircuitBreaker.class, "reset", Duration.class).map(Duration::toMillis).orElse(Duration.ofSeconds(DEFAULT_CIRCUIT_BREAKER_TIMEOUT_IN_MILLIS).toMillis());
        retryState = circuitContexts.computeIfAbsent(context.getExecutableMethod(), method -> new CircuitBreakerRetry(timeout, retryStateBuilder, context, eventPublisher));
    } else {
        retryState = (MutableRetryState) retryStateBuilder.build();
    }
    MutableConvertibleValues<Object> attrs = context.getAttributes();
    attrs.put(RetryState.class.getName(), retry);
    InterceptedMethod interceptedMethod = InterceptedMethod.of(context);
    try {
        retryState.open();
        // Retry method call before we have actual Publisher/CompletionStage result
        Object result = retrySync(context, retryState, interceptedMethod);
        switch(interceptedMethod.resultType()) {
            case PUBLISHER:
                Flux<Object> reactiveSequence = Flux.from((Publisher<?>) result);
                return interceptedMethod.handleResult(reactiveSequence.onErrorResume(retryFlowable(context, retryState, reactiveSequence)).doOnNext(o -> retryState.close(null)));
            case COMPLETION_STAGE:
                CompletableFuture<Object> newFuture = new CompletableFuture<>();
                Supplier<CompletionStage<?>> retrySupplier = () -> interceptedMethod.interceptResultAsCompletionStage(this);
                ((CompletionStage<?>) result).whenComplete(retryCompletable(context, retryState, newFuture, retrySupplier));
                return interceptedMethod.handleResult(newFuture);
            case SYNCHRONOUS:
                retryState.close(null);
                return result;
            default:
                return interceptedMethod.unsupported();
        }
    } catch (Exception e) {
        return interceptedMethod.handleException(e);
    }
}
Also used : RetryState(io.micronaut.retry.RetryState) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) InterceptedMethod(io.micronaut.aop.InterceptedMethod) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ExecutableMethod(io.micronaut.inject.ExecutableMethod) TaskExecutors(io.micronaut.scheduling.TaskExecutors) MethodInterceptor(io.micronaut.aop.MethodInterceptor) Nullable(io.micronaut.core.annotation.Nullable) Duration(java.time.Duration) Map(java.util.Map) Retryable(io.micronaut.retry.annotation.Retryable) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) BiConsumer(java.util.function.BiConsumer) MutableConvertibleValues(io.micronaut.core.convert.value.MutableConvertibleValues) ExecutorService(java.util.concurrent.ExecutorService) RetryEvent(io.micronaut.retry.event.RetryEvent) Logger(org.slf4j.Logger) Publisher(org.reactivestreams.Publisher) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ApplicationEventPublisher(io.micronaut.context.event.ApplicationEventPublisher) Singleton(jakarta.inject.Singleton) CircuitBreaker(io.micronaut.retry.annotation.CircuitBreaker) TimeUnit(java.util.concurrent.TimeUnit) Flux(reactor.core.publisher.Flux) ChronoUnit(java.time.temporal.ChronoUnit) CompletionStage(java.util.concurrent.CompletionStage) MethodInvocationContext(io.micronaut.aop.MethodInvocationContext) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) Optional(java.util.Optional) InterceptPhase(io.micronaut.aop.InterceptPhase) Named(jakarta.inject.Named) InterceptedMethod(io.micronaut.aop.InterceptedMethod) CompletableFuture(java.util.concurrent.CompletableFuture) Retryable(io.micronaut.retry.annotation.Retryable) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) CompletionStage(java.util.concurrent.CompletionStage) RetryState(io.micronaut.retry.RetryState) Nullable(io.micronaut.core.annotation.Nullable)

Aggregations

AnnotationValue (io.micronaut.core.annotation.AnnotationValue)49 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)19 Argument (io.micronaut.core.type.Argument)18 ExecutableMethod (io.micronaut.inject.ExecutableMethod)18 StringUtils (io.micronaut.core.util.StringUtils)17 Optional (java.util.Optional)17 NonNull (io.micronaut.core.annotation.NonNull)14 Map (java.util.Map)14 Singleton (jakarta.inject.Singleton)13 List (java.util.List)13 Logger (org.slf4j.Logger)13 LoggerFactory (org.slf4j.LoggerFactory)13 InterceptedMethod (io.micronaut.aop.InterceptedMethod)12 Nullable (io.micronaut.core.annotation.Nullable)12 ConversionService (io.micronaut.core.convert.ConversionService)11 BeanContext (io.micronaut.context.BeanContext)10 Qualifiers (io.micronaut.inject.qualifiers.Qualifiers)10 Annotation (java.lang.annotation.Annotation)10 MethodInterceptor (io.micronaut.aop.MethodInterceptor)9 Internal (io.micronaut.core.annotation.Internal)9