Search in sources :

Example 1 with BootstrapContextCompatible

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

the class DefaultEventLoopGroupRegistry method defaultEventLoopGroup.

/**
 * Constructs an event loop group with default Configuration.
 *
 * @param threadFactory The default Netty thread factory
 * @return The event loop group
 */
@Singleton
@Requires(missingProperty = EventLoopGroupConfiguration.DEFAULT_LOOP)
@Primary
@BootstrapContextCompatible
@Bean(typed = { EventLoopGroup.class })
protected EventLoopGroup defaultEventLoopGroup(@Named(NettyThreadFactory.NAME) ThreadFactory threadFactory) {
    EventLoopGroupConfiguration configuration = new DefaultEventLoopGroupConfiguration();
    EventLoopGroup eventLoopGroup = eventLoopGroupFactory.createEventLoopGroup(configuration, threadFactory);
    eventLoopGroups.put(eventLoopGroup, configuration);
    return eventLoopGroup;
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Requires(io.micronaut.context.annotation.Requires) BootstrapContextCompatible(io.micronaut.context.annotation.BootstrapContextCompatible) Singleton(jakarta.inject.Singleton) Primary(io.micronaut.context.annotation.Primary) EachBean(io.micronaut.context.annotation.EachBean) Bean(io.micronaut.context.annotation.Bean)

Example 2 with BootstrapContextCompatible

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

the class ObjectMapperFactory method objectMapper.

/**
 * Builds the core Jackson {@link ObjectMapper} from the optional configuration and {@link JsonFactory}.
 *
 * @param jacksonConfiguration The configuration
 * @param jsonFactory          The JSON factory
 * @return The {@link ObjectMapper}
 */
@Singleton
@Primary
@Named("json")
@BootstrapContextCompatible
public ObjectMapper objectMapper(@Nullable JacksonConfiguration jacksonConfiguration, @Nullable JsonFactory jsonFactory) {
    ObjectMapper objectMapper = new ObjectMapper(jsonFactory, null, new DefaultDeserializationContext.Impl(new ResilientBeanDeserializerFactory(new DeserializerFactoryConfig())));
    final boolean hasConfiguration = jacksonConfiguration != null;
    if (!hasConfiguration || jacksonConfiguration.isModuleScan()) {
        objectMapper.findAndRegisterModules();
    }
    objectMapper.registerModules(jacksonModules);
    SimpleModule module = new SimpleModule(MICRONAUT_MODULE);
    for (JsonSerializer serializer : serializers) {
        Class<? extends JsonSerializer> type = serializer.getClass();
        Type annotation = type.getAnnotation(Type.class);
        if (annotation != null) {
            Class[] value = annotation.value();
            for (Class aClass : value) {
                module.addSerializer(aClass, serializer);
            }
        } else {
            Optional<Class> targetType = GenericTypeUtils.resolveSuperGenericTypeArgument(type);
            if (targetType.isPresent()) {
                module.addSerializer(targetType.get(), serializer);
            } else {
                module.addSerializer(serializer);
            }
        }
    }
    for (JsonDeserializer deserializer : deserializers) {
        Class<? extends JsonDeserializer> type = deserializer.getClass();
        Type annotation = type.getAnnotation(Type.class);
        if (annotation != null) {
            Class[] value = annotation.value();
            for (Class aClass : value) {
                module.addDeserializer(aClass, deserializer);
            }
        } else {
            Optional<Class> targetType = GenericTypeUtils.resolveSuperGenericTypeArgument(type);
            targetType.ifPresent(aClass -> module.addDeserializer(aClass, deserializer));
        }
    }
    if (hasConfiguration && jacksonConfiguration.isTrimStrings()) {
        module.addDeserializer(String.class, new StringDeserializer() {

            @Override
            public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
                String value = super.deserialize(p, ctxt);
                return StringUtils.trimToNull(value);
            }
        });
    }
    for (KeyDeserializer keyDeserializer : keyDeserializers) {
        Class<? extends KeyDeserializer> type = keyDeserializer.getClass();
        Type annotation = type.getAnnotation(Type.class);
        if (annotation != null) {
            Class[] value = annotation.value();
            for (Class clazz : value) {
                module.addKeyDeserializer(clazz, keyDeserializer);
            }
        }
    }
    objectMapper.registerModule(module);
    for (BeanSerializerModifier beanSerializerModifier : beanSerializerModifiers) {
        objectMapper.setSerializerFactory(objectMapper.getSerializerFactory().withSerializerModifier(beanSerializerModifier));
    }
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
    objectMapper.configure(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS, true);
    if (hasConfiguration) {
        ObjectMapper.DefaultTyping defaultTyping = jacksonConfiguration.getDefaultTyping();
        if (defaultTyping != null) {
            objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(), defaultTyping);
        }
        JsonInclude.Include include = jacksonConfiguration.getSerializationInclusion();
        if (include != null) {
            objectMapper.setSerializationInclusion(include);
        }
        String dateFormat = jacksonConfiguration.getDateFormat();
        if (dateFormat != null) {
            objectMapper.setDateFormat(new SimpleDateFormat(dateFormat));
        }
        Locale locale = jacksonConfiguration.getLocale();
        if (locale != null) {
            objectMapper.setLocale(locale);
        }
        TimeZone timeZone = jacksonConfiguration.getTimeZone();
        if (timeZone != null) {
            objectMapper.setTimeZone(timeZone);
        }
        PropertyNamingStrategy propertyNamingStrategy = jacksonConfiguration.getPropertyNamingStrategy();
        if (propertyNamingStrategy != null) {
            objectMapper.setPropertyNamingStrategy(propertyNamingStrategy);
        }
        jacksonConfiguration.getSerializationSettings().forEach(objectMapper::configure);
        jacksonConfiguration.getDeserializationSettings().forEach(objectMapper::configure);
        jacksonConfiguration.getMapperSettings().forEach(objectMapper::configure);
        jacksonConfiguration.getParserSettings().forEach(objectMapper::configure);
        jacksonConfiguration.getGeneratorSettings().forEach(objectMapper::configure);
    }
    return objectMapper;
}
Also used : Locale(java.util.Locale) DefaultDeserializationContext(com.fasterxml.jackson.databind.deser.DefaultDeserializationContext) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) KeyDeserializer(com.fasterxml.jackson.databind.KeyDeserializer) DefaultDeserializationContext(com.fasterxml.jackson.databind.deser.DefaultDeserializationContext) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BeanSerializerModifier(com.fasterxml.jackson.databind.ser.BeanSerializerModifier) JsonParser(com.fasterxml.jackson.core.JsonParser) DeserializerFactoryConfig(com.fasterxml.jackson.databind.cfg.DeserializerFactoryConfig) StringDeserializer(com.fasterxml.jackson.databind.deser.std.StringDeserializer) IOException(java.io.IOException) Type(io.micronaut.context.annotation.Type) TimeZone(java.util.TimeZone) PropertyNamingStrategy(com.fasterxml.jackson.databind.PropertyNamingStrategy) SimpleDateFormat(java.text.SimpleDateFormat) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) Named(jakarta.inject.Named) BootstrapContextCompatible(io.micronaut.context.annotation.BootstrapContextCompatible) Singleton(jakarta.inject.Singleton) Primary(io.micronaut.context.annotation.Primary)

Example 3 with BootstrapContextCompatible

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

the class DefaultEventLoopGroupRegistry method eventLoopGroup.

/**
 * Constructs an event loop group for each configuration.
 *
 * @param configuration The configuration
 * @return The event loop group
 */
@EachBean(EventLoopGroupConfiguration.class)
@Bean
@BootstrapContextCompatible
protected EventLoopGroup eventLoopGroup(EventLoopGroupConfiguration configuration) {
    final String executor = configuration.getExecutorName().orElse(null);
    EventLoopGroup eventLoopGroup;
    if (executor != null) {
        eventLoopGroup = beanLocator.findBean(Executor.class, Qualifiers.byName(executor)).map(executorService -> eventLoopGroupFactory.createEventLoopGroup(configuration.getNumThreads(), executorService, configuration.getIoRatio().orElse(null))).orElseThrow(() -> new ConfigurationException("No executor service configured for name: " + executor));
    } else {
        ThreadFactory threadFactory = beanLocator.findBean(ThreadFactory.class, Qualifiers.byName(configuration.getName())).orElseGet(() -> new DefaultThreadFactory(configuration.getName() + "-" + DefaultThreadFactory.toPoolName(NioEventLoopGroup.class)));
        eventLoopGroup = eventLoopGroupFactory.createEventLoopGroup(configuration, threadFactory);
    }
    eventLoopGroups.put(eventLoopGroup, configuration);
    return eventLoopGroup;
}
Also used : DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Executor(java.util.concurrent.Executor) ConfigurationException(io.micronaut.context.exceptions.ConfigurationException) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) BootstrapContextCompatible(io.micronaut.context.annotation.BootstrapContextCompatible) EachBean(io.micronaut.context.annotation.EachBean) EachBean(io.micronaut.context.annotation.EachBean) Bean(io.micronaut.context.annotation.Bean)

Aggregations

BootstrapContextCompatible (io.micronaut.context.annotation.BootstrapContextCompatible)3 Bean (io.micronaut.context.annotation.Bean)2 EachBean (io.micronaut.context.annotation.EachBean)2 Primary (io.micronaut.context.annotation.Primary)2 EventLoopGroup (io.netty.channel.EventLoopGroup)2 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)2 Singleton (jakarta.inject.Singleton)2 JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)1 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)1 JsonSerializer (com.fasterxml.jackson.databind.JsonSerializer)1 KeyDeserializer (com.fasterxml.jackson.databind.KeyDeserializer)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 PropertyNamingStrategy (com.fasterxml.jackson.databind.PropertyNamingStrategy)1 DeserializerFactoryConfig (com.fasterxml.jackson.databind.cfg.DeserializerFactoryConfig)1 DefaultDeserializationContext (com.fasterxml.jackson.databind.deser.DefaultDeserializationContext)1 StringDeserializer (com.fasterxml.jackson.databind.deser.std.StringDeserializer)1 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)1 BeanSerializerModifier (com.fasterxml.jackson.databind.ser.BeanSerializerModifier)1