Search in sources :

Example 1 with ConfigurationException

use of io.micronaut.context.exceptions.ConfigurationException in project micronaut-kafka by micronaut-projects.

the class KafkaProducerFactory method getProducer.

/**
 * Creates a new {@link KafkaProducer} for the given configuration.
 *
 * @param injectionPoint The injection point used to create the bean
 * @param producerConfiguration An optional producer configuration
 * @param <K> The key type
 * @param <V> The value type
 * @return The consumer
 */
@Bean
@Any
public <K, V> Producer<K, V> getProducer(@Nullable InjectionPoint<KafkaProducer<K, V>> injectionPoint, @Nullable @Parameter AbstractKafkaProducerConfiguration<K, V> producerConfiguration) {
    if (injectionPoint == null) {
        if (producerConfiguration != null) {
            Optional<Serializer<K>> keySerializer = producerConfiguration.getKeySerializer();
            Optional<Serializer<V>> valueSerializer = producerConfiguration.getValueSerializer();
            Properties config = producerConfiguration.getConfig();
            if (keySerializer.isPresent() && valueSerializer.isPresent()) {
                Serializer<K> ks = keySerializer.get();
                Serializer<V> vs = valueSerializer.get();
                return new KafkaProducer<>(config, ks, vs);
            } else if (keySerializer.isPresent() || valueSerializer.isPresent()) {
                throw new ConfigurationException("Both the [keySerializer] and [valueSerializer] must be set when setting either");
            } else {
                return new KafkaProducer<>(config);
            }
        } else {
            throw new ConfigurationException("No Kafka configuration specified when using direct instantiation");
        }
    }
    Argument<?> argument;
    if (injectionPoint instanceof FieldInjectionPoint) {
        argument = ((FieldInjectionPoint<?, ?>) injectionPoint).asArgument();
    } else if (injectionPoint instanceof ArgumentInjectionPoint) {
        argument = ((ArgumentInjectionPoint<?, ?>) injectionPoint).getArgument();
    } else {
        throw new ConfigurationException("Cannot directly retrieve KafkaProducer instances. Use @Inject or constructor injection");
    }
    Argument<?> k = argument.getTypeVariable("K").orElse(null);
    Argument<?> v = argument.getTypeVariable("V").orElse(null);
    if (k == null || v == null) {
        throw new ConfigurationException("@KafkaClient used on type missing generic argument values for Key and Value: " + injectionPoint);
    }
    final String id = injectionPoint.getAnnotationMetadata().stringValue(KafkaClient.class).orElse(null);
    return getKafkaProducer(id, null, k, v, false);
}
Also used : KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) Properties(java.util.Properties) ArgumentInjectionPoint(io.micronaut.inject.ArgumentInjectionPoint) ConfigurationException(io.micronaut.context.exceptions.ConfigurationException) KafkaClient(io.micronaut.configuration.kafka.annotation.KafkaClient) FieldInjectionPoint(io.micronaut.inject.FieldInjectionPoint) Serializer(org.apache.kafka.common.serialization.Serializer) Any(io.micronaut.context.annotation.Any) Bean(io.micronaut.context.annotation.Bean)

Example 2 with ConfigurationException

use of io.micronaut.context.exceptions.ConfigurationException in project micronaut-mongodb by micronaut-projects.

the class MongoProcessFactory method onCreated.

@Override
public DefaultMongoConfiguration onCreated(BeanCreatedEvent<DefaultMongoConfiguration> event) {
    DefaultMongoConfiguration configuration = event.getBean();
    try {
        Optional<ConnectionString> connectionString = configuration.getConnectionString();
        startEmbeddedMongoIfPossible(connectionString.orElse(null), null);
    } catch (IOException e) {
        throw new ConfigurationException("Error starting Embedded MongoDB server: " + e.getMessage(), e);
    }
    return configuration;
}
Also used : ConfigurationException(io.micronaut.context.exceptions.ConfigurationException) IOException(java.io.IOException) ConnectionString(com.mongodb.ConnectionString) DefaultMongoConfiguration(io.micronaut.configuration.mongo.core.DefaultMongoConfiguration)

Example 3 with ConfigurationException

use of io.micronaut.context.exceptions.ConfigurationException in project micronaut-security by micronaut-projects.

the class DefaultOauthClient method authorizationRedirect.

@Override
public Publisher<MutableHttpResponse<?>> authorizationRedirect(HttpRequest<?> originating) {
    AuthorizationRequest authorizationRequest = beanContext.createBean(OauthAuthorizationRequest.class, originating, clientConfiguration);
    String authorizationEndpoint = clientConfiguration.getAuthorization().flatMap(EndpointConfiguration::getUrl).orElseThrow(() -> new ConfigurationException("Oauth client requires the authorization URL to be set in configuration"));
    if (LOG.isTraceEnabled()) {
        LOG.trace("Starting authorization code grant flow to provider [{}]. Redirecting to [{}]", getName(), authorizationEndpoint);
    }
    return Flux.just(redirectHandler.redirect(authorizationRequest, authorizationEndpoint));
}
Also used : OauthAuthorizationRequest(io.micronaut.security.oauth2.endpoint.authorization.request.OauthAuthorizationRequest) AuthorizationRequest(io.micronaut.security.oauth2.endpoint.authorization.request.AuthorizationRequest) ConfigurationException(io.micronaut.context.exceptions.ConfigurationException)

Example 4 with ConfigurationException

use of io.micronaut.context.exceptions.ConfigurationException in project micronaut-jms by micronaut-projects.

the class JMSConnectionFactoryBeanProcessor method process.

@Override
public void process(BeanDefinition<?> beanDefinition, BeanContext context) {
    final Object candidate = context.getBean(beanDefinition);
    Assert.isTrue(candidate instanceof ConnectionFactory, () -> "@JMSConnectionFactory can only be applied to a bean of type javax.jms.ConnectionFactory. " + "Provided class was " + candidate.getClass().getName());
    final ConnectionFactory connectionFactory = (ConnectionFactory) candidate;
    final String name = beanDefinition.stringValue(JMSConnectionFactory.class).orElseThrow(() -> new ConfigurationException("@JMSConnectionFactory must specify a name for the bean."));
    context.registerSingleton(JMSConnectionPool.class, new JMSConnectionPool(connectionFactory, properties.getInitialPoolSize(), properties.getMaxPoolSize()), Qualifiers.byName(name));
    logger.debug("created JMSConnectionPool bean '{}' for ConnectionFactory {}", name, connectionFactory.getClass().getName());
}
Also used : JMSConnectionFactory(io.micronaut.jms.annotations.JMSConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) JMSConnectionPool(io.micronaut.jms.pool.JMSConnectionPool) ConfigurationException(io.micronaut.context.exceptions.ConfigurationException) JMSConnectionFactory(io.micronaut.jms.annotations.JMSConnectionFactory)

Example 5 with ConfigurationException

use of io.micronaut.context.exceptions.ConfigurationException in project micronaut-kafka by micronaut-projects.

the class KafkaConsumerFactory method createConsumer.

/**
 * Creates a new {@link KafkaConsumer} for the given configuration.
 *
 * @param consumerConfiguration The consumer configuration
 * @param <K> The key type
 * @param <V> The value type
 * @return The consumer
 */
@Prototype
public <K, V> Consumer<K, V> createConsumer(@Parameter AbstractKafkaConsumerConfiguration<K, V> consumerConfiguration) {
    Optional<Deserializer<K>> keyDeserializer = consumerConfiguration.getKeyDeserializer();
    Optional<Deserializer<V>> valueDeserializer = consumerConfiguration.getValueDeserializer();
    Properties config = consumerConfiguration.getConfig();
    if (keyDeserializer.isPresent() && valueDeserializer.isPresent()) {
        return new KafkaConsumer<>(config, keyDeserializer.get(), valueDeserializer.get());
    } else if (keyDeserializer.isPresent() || valueDeserializer.isPresent()) {
        throw new ConfigurationException("Both the [keyDeserializer] and [valueDeserializer] must be set when setting either");
    } else {
        return new KafkaConsumer<>(config);
    }
}
Also used : ConfigurationException(io.micronaut.context.exceptions.ConfigurationException) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) Properties(java.util.Properties) Prototype(io.micronaut.context.annotation.Prototype)

Aggregations

ConfigurationException (io.micronaut.context.exceptions.ConfigurationException)7 JMSConnectionPool (io.micronaut.jms.pool.JMSConnectionPool)2 Singleton (jakarta.inject.Singleton)2 Properties (java.util.Properties)2 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)1 ConnectionString (com.mongodb.ConnectionString)1 MethodInterceptor (io.micronaut.aop.MethodInterceptor)1 MethodInvocationContext (io.micronaut.aop.MethodInvocationContext)1 KafkaClient (io.micronaut.configuration.kafka.annotation.KafkaClient)1 DefaultMongoConfiguration (io.micronaut.configuration.mongo.core.DefaultMongoConfiguration)1 BeanContext (io.micronaut.context.BeanContext)1 Any (io.micronaut.context.annotation.Any)1 Bean (io.micronaut.context.annotation.Bean)1 Primary (io.micronaut.context.annotation.Primary)1 Prototype (io.micronaut.context.annotation.Prototype)1 Requires (io.micronaut.context.annotation.Requires)1 ArgumentInjectionPoint (io.micronaut.inject.ArgumentInjectionPoint)1 ExecutableMethod (io.micronaut.inject.ExecutableMethod)1 FieldInjectionPoint (io.micronaut.inject.FieldInjectionPoint)1 Qualifiers (io.micronaut.inject.qualifiers.Qualifiers)1