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