io.micronaut.inject.BeanIdentifier Java Examples

The following examples show how to use io.micronaut.inject.BeanIdentifier. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GrpcChannelScope.java    From micronaut-grpc with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T get(
        BeanResolutionContext resolutionContext,
        BeanDefinition<T> beanDefinition,
        BeanIdentifier identifier,
        Provider<T> provider) {
    BeanResolutionContext.Segment segment = resolutionContext.getPath().currentSegment().orElseThrow(() ->
            new IllegalStateException("@GrpcChannel used in invalid location")
    );
    Argument argument = segment.getArgument();
    String value = argument.getAnnotationMetadata().getValue(GrpcChannel.class, String.class).orElse(null);
    if (StringUtils.isEmpty(value)) {
        throw new DependencyInjectionException(resolutionContext, argument, "No value specified to @GrpcChannel annotation");
    }
    if (!Channel.class.isAssignableFrom(argument.getType())) {
        throw new DependencyInjectionException(resolutionContext, argument, "@GrpcChannel used on type that is not a Channel");
    }

    if ("grpc-server".equalsIgnoreCase(value)) {
        return (T) applicationContext.getBean(ManagedChannel.class, Qualifiers.byName("grpc-server"));
    }

    if (!(provider instanceof ParametrizedProvider)) {
        throw new DependencyInjectionException(resolutionContext, argument, "GrpcChannelScope called with invalid bean provider");
    }
    value = applicationContext.resolveRequiredPlaceholders(value);
    String finalValue = value;
    return (T) channels.computeIfAbsent(new ChannelKey(identifier, value), channelKey ->
            (ManagedChannel) ((ParametrizedProvider<T>) provider).get(finalValue)
    );
}
 
Example #2
Source File: GrpcChannelScope.java    From micronaut-grpc with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> Optional<T> remove(BeanIdentifier identifier) {
    final Optional<ChannelKey> key = this.channels.keySet().stream().filter(k -> k.identifier.equals(identifier)).findFirst();
    if (key.isPresent()) {
        return key.map(channelKey -> (T) channels.remove(channelKey));
    }
    return Optional.empty();
}
 
Example #3
Source File: KafkaClientScope.java    From micronaut-kafka with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T get(BeanResolutionContext resolutionContext, BeanDefinition<T> beanDefinition, BeanIdentifier identifier, Provider<T> provider) {
    BeanResolutionContext.Segment segment = resolutionContext.getPath().currentSegment().orElseThrow(() ->
            new IllegalStateException("@KafkaClient used in invalid location")
    );
    Argument argument = segment.getArgument();
    AnnotationValue<KafkaClient> annotation = argument.findAnnotation(KafkaClient.class)
                                                      .orElseThrow(() -> new DependencyInjectionException(resolutionContext, argument, "KafkaClientScope called for injection point that is not annotated with @KafkaClient"));
    if (!Producer.class.isAssignableFrom(argument.getType())) {
        throw new DependencyInjectionException(resolutionContext, argument, "@KafkaClient used on type that is not a " + Producer.class.getName());
    }
    if (!(provider instanceof ParametrizedProvider)) {
        throw new DependencyInjectionException(resolutionContext, argument, "KafkaClientScope called with invalid bean provider");
    }

    Optional<Argument<?>> k = argument.getTypeVariable("K");
    Optional<Argument<?>> v = argument.getTypeVariable("V");

    if (!k.isPresent() || !v.isPresent()) {
        throw new DependencyInjectionException(resolutionContext, argument, "@KafkaClient used on type missing generic argument values for Key and Value");

    }

    String id = annotation.getValue(String.class).orElse(null);
    Argument<?> keyArgument = k.get();
    Argument<?> valueArgument = v.get();
    return getKafkaProducer(id, keyArgument, valueArgument);
}
 
Example #4
Source File: TransactionAwareDataSource.java    From micronaut-data with Apache License 2.0 5 votes vote down vote up
@Override
public DataSource onCreated(BeanCreatedEvent<DataSource> event) {
    final BeanIdentifier beanIdentifier = event.getBeanIdentifier();
    String name = beanIdentifier.getName();
    if (name.equalsIgnoreCase("primary")) {
        name = "default";
    }
    this.qualifier = name;
    return new DataSourceProxy(event.getBean());
}
 
Example #5
Source File: GrpcChannelScope.java    From micronaut-grpc with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T get(
        BeanResolutionContext resolutionContext,
        BeanDefinition<T> beanDefinition,
        BeanIdentifier identifier,
        Provider<T> provider) {
    BeanResolutionContext.Segment segment = resolutionContext.getPath().currentSegment().orElseThrow(() ->
            new IllegalStateException("@GrpcChannel used in invalid location")
    );
    Argument argument = segment.getArgument();
    String value = argument.getAnnotationMetadata().getValue(GrpcChannel.class, String.class).orElse(null);
    if (StringUtils.isEmpty(value)) {
        throw new DependencyInjectionException(resolutionContext, argument, "No value specified to @GrpcChannel annotation");
    }
    if (!Channel.class.isAssignableFrom(argument.getType())) {
        throw new DependencyInjectionException(resolutionContext, argument, "@GrpcChannel used on type that is not a Channel");
    }

    if ("grpc-server".equalsIgnoreCase(value)) {
        return (T) applicationContext.getBean(ManagedChannel.class, Qualifiers.byName("grpc-server"));
    }

    if (!(provider instanceof ParametrizedProvider)) {
        throw new DependencyInjectionException(resolutionContext, argument, "GrpcChannelScope called with invalid bean provider");
    }
    value = applicationContext.resolveRequiredPlaceholders(value);
    String finalValue = value;
    return (T) channels.computeIfAbsent(new ChannelKey(identifier, value), channelKey ->
            (ManagedChannel) ((ParametrizedProvider<T>) provider).get(finalValue)
    );
}
 
Example #6
Source File: GrpcChannelScope.java    From micronaut-grpc with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> Optional<T> remove(BeanIdentifier identifier) {
    final Optional<ChannelKey> key = this.channels.keySet().stream().filter(k -> k.identifier.equals(identifier)).findFirst();
    if (key.isPresent()) {
        return key.map(channelKey -> (T) channels.remove(channelKey));
    }
    return Optional.empty();
}
 
Example #7
Source File: GrpcChannelScope.java    From micronaut-grpc with Apache License 2.0 4 votes vote down vote up
public ChannelKey(BeanIdentifier identifier, String value) {
    this.identifier = identifier;
    this.value = value;
}
 
Example #8
Source File: KafkaClientScope.java    From micronaut-kafka with Apache License 2.0 4 votes vote down vote up
@Override
public <T> Optional<T> remove(BeanIdentifier identifier) {
    return Optional.empty();
}
 
Example #9
Source File: GrpcChannelScope.java    From micronaut-grpc with Apache License 2.0 4 votes vote down vote up
public ChannelKey(BeanIdentifier identifier, String value) {
    this.identifier = identifier;
    this.value = value;
}