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;
}
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;
}
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);
}
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);
}
}
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);
}
}
Aggregations