Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset shared ConversionService instance state on context restart #7635

Merged
merged 5 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.Unpooled;
import jakarta.annotation.PostConstruct;
import jakarta.inject.Singleton;

/**
Expand All @@ -43,16 +44,6 @@ public class NettyByteBufferFactory implements ByteBufferFactory<ByteBufAllocato

private final ByteBufAllocator allocator;

static {
ConversionService.SHARED.addConverter(ByteBuf.class, ByteBuffer.class, DEFAULT::wrap);
ConversionService.SHARED.addConverter(ByteBuffer.class, ByteBuf.class, byteBuffer -> {
if (byteBuffer instanceof NettyByteBuffer) {
return (ByteBuf) byteBuffer.asNativeBuffer();
}
throw new IllegalArgumentException("Unconvertible buffer type " + byteBuffer);
});
}

/**
* Default constructor.
*/
Expand All @@ -67,6 +58,17 @@ public NettyByteBufferFactory(ByteBufAllocator allocator) {
this.allocator = allocator;
}

@PostConstruct
final void register(ConversionService<?> conversionService) {
conversionService.addConverter(ByteBuf.class, ByteBuffer.class, DEFAULT::wrap);
conversionService.addConverter(ByteBuffer.class, ByteBuf.class, byteBuffer -> {
if (byteBuffer instanceof NettyByteBuffer) {
return (ByteBuf) byteBuffer.asNativeBuffer();
}
throw new IllegalArgumentException("Unconvertible buffer type " + byteBuffer);
});
}

@Override
public ByteBufAllocator getNativeAllocator() {
return allocator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.micronaut.core.annotation.AnnotationClassValue;
import io.micronaut.core.annotation.AnnotationMetadata;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.convert.converters.MultiValuesConverterFactory;
import io.micronaut.core.convert.exceptions.ConversionErrorException;
import io.micronaut.core.convert.format.Format;
Expand All @@ -26,6 +27,7 @@
import io.micronaut.core.convert.value.ConvertibleValuesMap;
import io.micronaut.core.io.IOUtils;
import io.micronaut.core.io.buffer.ReferenceCounted;
import io.micronaut.core.io.service.SoftServiceLoader;
import io.micronaut.core.naming.NameUtils;
import io.micronaut.core.reflect.ClassUtils;
import io.micronaut.core.reflect.ReflectionUtils;
Expand Down Expand Up @@ -56,7 +58,24 @@
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Currency;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.Properties;
import java.util.StringJoiner;
import java.util.TimeZone;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

Expand Down Expand Up @@ -176,6 +195,19 @@ public <S, T> DefaultConversionService addConverter(Class<S> sourceType, Class<T
return this;
}


/**
* Reset internal state.
*
* @since 3.5.3
*/
@Internal
public void reset() {
typeConverters.clear();
converterCache.clear();
registerDefaultConverters();
}

/**
* Default Converters.
*/
Expand Down Expand Up @@ -924,6 +956,14 @@ protected void registerDefaultConverters() {
new MultiValuesConverterFactory.MapToMultiValuesConverter(this));
addConverter(Object.class, io.micronaut.core.convert.value.ConvertibleMultiValues.class,
new MultiValuesConverterFactory.ObjectToMultiValuesConverter(this));

Collection<TypeConverterRegistrar> registrars = new ArrayList<>();
SoftServiceLoader.load(TypeConverterRegistrar.class)
.disableFork()
.collectAll(registrars);
for (TypeConverterRegistrar registrar : registrars) {
registrar.register(this);
}
}

/**
Expand Down
19 changes: 10 additions & 9 deletions http/src/main/java/io/micronaut/http/MediaType.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.TypeHint;
import io.micronaut.core.convert.ArgumentConversionContext;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.convert.ImmutableArgumentConversionContext;
import io.micronaut.core.naming.NameUtils;
import io.micronaut.core.type.Argument;
Expand All @@ -37,7 +36,16 @@
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;

/**
Expand Down Expand Up @@ -394,13 +402,6 @@ public class MediaType implements CharSequence {
private BigDecimal qualityNumberField = BigDecimal.ONE;

static {
ConversionService.SHARED.addConverter(CharSequence.class, MediaType.class, charSequence -> {
if (StringUtils.isNotEmpty(charSequence)) {
return of(charSequence.toString());
}
return null;
}
);
textTypePatterns.add(Pattern.compile("^text/.*$"));
textTypePatterns.add(Pattern.compile("^.*\\+json$"));
textTypePatterns.add(Pattern.compile("^.*\\+text$"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2017-2022 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.http;

import io.micronaut.core.annotation.Internal;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.convert.TypeConverterRegistrar;
import io.micronaut.core.util.StringUtils;

/**
* The media type converters registrar.
*
* @author Denis Stepanov
* @since 3.6.0
*/
@Internal
public final class MediaTypeConvertersRegistrar implements TypeConverterRegistrar {

@Override
public void register(ConversionService<?> conversionService) {
conversionService.addConverter(CharSequence.class, MediaType.class, charSequence -> {
if (StringUtils.isNotEmpty(charSequence)) {
return MediaType.of(charSequence.toString());
}
return null;
}
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.micronaut.http.MediaTypeConvertersRegistrar
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import io.micronaut.core.annotation.Order;
import io.micronaut.core.annotation.UsedByGeneratedCode;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.convert.DefaultConversionService;
import io.micronaut.core.convert.TypeConverter;
import io.micronaut.core.convert.TypeConverterRegistrar;
import io.micronaut.core.convert.value.MutableConvertibleValues;
Expand Down Expand Up @@ -333,6 +334,8 @@ public synchronized BeanContext start() {
if (!isRunning()) {

if (initializing.compareAndSet(false, true)) {
// Reset possibly modified shared context
((DefaultConversionService) ConversionService.SHARED).reset();
if (LOG.isDebugEnabled()) {
LOG.debug("Starting BeanContext");
}
Expand Down Expand Up @@ -419,7 +422,7 @@ public synchronized BeanContext stop() {
beanCreationEventListeners = null;
beanPreDestroyEventListeners = null;
beanDestroyedEventListeners = null;

((DefaultConversionService) ConversionService.SHARED).reset();
terminating.set(false);
running.set(false);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2017-2022 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.inject.annotation;

import io.micronaut.core.annotation.Internal;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.convert.TypeConverterRegistrar;
import io.micronaut.core.reflect.ClassUtils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
* Registers annotation converters.
*
* @author Denis Stepanov
* @since 3.6.0
*/
@Internal
public final class AnnotationConvertersRegistrar implements TypeConverterRegistrar {

@Override
public void register(ConversionService<?> conversionService) {
conversionService.addConverter(io.micronaut.core.annotation.AnnotationValue.class, Annotation.class, (object, targetType, context) -> {
Optional<Class> annotationClass = ClassUtils.forName(object.getAnnotationName(), targetType.getClassLoader());
return annotationClass.map(aClass -> AnnotationMetadataSupport.buildAnnotation(aClass, object));
});

conversionService.addConverter(io.micronaut.core.annotation.AnnotationValue[].class, Object[].class, (object, targetType, context) -> {
List result = new ArrayList();
Class annotationClass = null;
for (io.micronaut.core.annotation.AnnotationValue annotationValue : object) {
if (annotationClass == null) {
// all annotations will be on the same type
Optional<Class> aClass = ClassUtils.forName(annotationValue.getAnnotationName(), targetType.getClassLoader());
if (!aClass.isPresent()) {
break;
}
annotationClass = aClass.get();
}
Annotation annotation = AnnotationMetadataSupport.buildAnnotation(annotationClass, annotationValue);
result.add(annotation);
}
if (!result.isEmpty()) {
return Optional.of(result.toArray((Object[]) Array.newInstance(annotationClass, result.size())));
}
return Optional.empty();
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,45 @@
package io.micronaut.inject.annotation;

import io.micronaut.context.exceptions.ConfigurationException;
import io.micronaut.core.annotation.*;
import io.micronaut.core.annotation.AnnotationClassValue;
import io.micronaut.core.annotation.AnnotationMetadata;
import io.micronaut.core.annotation.AnnotationUtil;
import io.micronaut.core.annotation.AnnotationValue;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.annotation.UsedByGeneratedCode;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.reflect.ClassUtils;
import io.micronaut.core.reflect.ReflectionUtils;
import io.micronaut.core.type.Argument;
import io.micronaut.core.util.ArgumentUtils;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.core.util.StringUtils;
import io.micronaut.core.value.OptionalValues;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.inject.ast.ClassElement;

import java.lang.annotation.Annotation;
import java.lang.annotation.Repeatable;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Array;
import java.util.*;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
Expand All @@ -53,34 +73,6 @@
@Internal
public class DefaultAnnotationMetadata extends AbstractAnnotationMetadata implements AnnotationMetadata, Cloneable, EnvironmentAnnotationMetadata {

static {
ConversionService.SHARED.addConverter(io.micronaut.core.annotation.AnnotationValue.class, Annotation.class, (object, targetType, context) -> {
Optional<Class> annotationClass = ClassUtils.forName(object.getAnnotationName(), targetType.getClassLoader());
return annotationClass.map(aClass -> AnnotationMetadataSupport.buildAnnotation(aClass, object));
});

ConversionService.SHARED.addConverter(io.micronaut.core.annotation.AnnotationValue[].class, Object[].class, (object, targetType, context) -> {
List result = new ArrayList();
Class annotationClass = null;
for (io.micronaut.core.annotation.AnnotationValue annotationValue : object) {
if (annotationClass == null) {
// all annotations will be on the same type
Optional<Class> aClass = ClassUtils.forName(annotationValue.getAnnotationName(), targetType.getClassLoader());
if (!aClass.isPresent()) {
break;
}
annotationClass = aClass.get();
}
Annotation annotation = AnnotationMetadataSupport.buildAnnotation(annotationClass, annotationValue);
result.add(annotation);
}
if (!result.isEmpty()) {
return Optional.of(result.toArray((Object[]) Array.newInstance(annotationClass, result.size())));
}
return Optional.empty();
});
}

@Nullable
Map<String, Map<CharSequence, Object>> declaredAnnotations;
@Nullable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.micronaut.inject.annotation.AnnotationConvertersRegistrar
Loading