io.micronaut.context.BeanContext Java Examples

The following examples show how to use io.micronaut.context.BeanContext. 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: KafkaConsumerProcessor.java    From micronaut-kafka with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new processor using the given {@link ExecutorService} to schedule consumers on.
 *
 * @param executorService              The executor service
 * @param applicationConfiguration     The application configuration
 * @param beanContext                  The bean context
 * @param defaultConsumerConfiguration The default consumer config
 * @param binderRegistry               The {@link ConsumerRecordBinderRegistry}
 * @param batchBinderRegistry          The {@link BatchConsumerRecordsBinderRegistry}
 * @param serdeRegistry                The {@link org.apache.kafka.common.serialization.Serde} registry
 * @param producerRegistry             The {@link ProducerRegistry}
 * @param exceptionHandler             The exception handler to use
 */
public KafkaConsumerProcessor(
        @Named(TaskExecutors.MESSAGE_CONSUMER) ExecutorService executorService,
        ApplicationConfiguration applicationConfiguration,
        BeanContext beanContext,
        AbstractKafkaConsumerConfiguration defaultConsumerConfiguration,
        ConsumerRecordBinderRegistry binderRegistry,
        BatchConsumerRecordsBinderRegistry batchBinderRegistry,
        SerdeRegistry serdeRegistry,
        ProducerRegistry producerRegistry,
        KafkaListenerExceptionHandler exceptionHandler) {
    this.executorService = executorService;
    this.applicationConfiguration = applicationConfiguration;
    this.beanContext = beanContext;
    this.defaultConsumerConfiguration = defaultConsumerConfiguration;
    this.binderRegistry = binderRegistry;
    this.batchBinderRegistry = batchBinderRegistry;
    this.serdeRegistry = serdeRegistry;
    this.executorScheduler = Schedulers.from(executorService);
    this.producerRegistry = producerRegistry;
    this.exceptionHandler = exceptionHandler;
}
 
Example #2
Source File: KafkaClientIntroductionAdvice.java    From micronaut-kafka with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the introduction advice for the given arguments.
 *
 * @param beanContext       The bean context.
 * @param serdeRegistry     The serde registry
 * @param conversionService The conversion service
 */
public KafkaClientIntroductionAdvice(
        BeanContext beanContext,
        SerdeRegistry serdeRegistry,
        ConversionService<?> conversionService) {
    this.beanContext = beanContext;
    this.serdeRegistry = serdeRegistry;
    this.conversionService = conversionService;
}
 
Example #3
Source File: KafkaClientScope.java    From micronaut-kafka with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a new client scope.
 *
 * @param beanContext The bean context
 * @param serdeRegistry The serde registry
 */
public KafkaClientScope(
        BeanContext beanContext,
        SerdeRegistry serdeRegistry) {
    this.beanContext = beanContext;
    this.serdeRegistry = serdeRegistry;
}
 
Example #4
Source File: DefaultJdbcRepositoryOperations.java    From micronaut-data with Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor.
 *
 * @param dataSourceName        The data source name
 * @param dataSource            The datasource
 * @param transactionOperations The JDBC operations for the data source
 * @param executorService       The executor service
 * @param beanContext           The bean context
 * @param codecs                The codecs
 * @param dateTimeProvider      The dateTimeProvider
 */
protected DefaultJdbcRepositoryOperations(@Parameter String dataSourceName,
                                          DataSource dataSource,
                                          @Parameter TransactionOperations<Connection> transactionOperations,
                                          @Named("io") @Nullable ExecutorService executorService,
                                          BeanContext beanContext,
                                          List<MediaTypeCodec> codecs,
                                          @NonNull DateTimeProvider dateTimeProvider) {
    super(
            new ColumnNameResultSetReader(),
            new ColumnIndexResultSetReader(),
            new JdbcQueryStatement(),
            codecs,
            dateTimeProvider
    );
    ArgumentUtils.requireNonNull("dataSource", dataSource);
    ArgumentUtils.requireNonNull("transactionOperations", transactionOperations);
    this.dataSource = dataSource;
    this.transactionOperations = transactionOperations;
    this.executorService = executorService;
    Collection<BeanDefinition<GenericRepository>> beanDefinitions = beanContext.getBeanDefinitions(GenericRepository.class, Qualifiers.byStereotype(Repository.class));
    for (BeanDefinition<GenericRepository> beanDefinition : beanDefinitions) {
        String targetDs = beanDefinition.stringValue(Repository.class).orElse("default");
        if (targetDs.equalsIgnoreCase(dataSourceName)) {
            Dialect dialect = beanDefinition.enumValue(JdbcRepository.class, "dialect", Dialect.class).orElseGet(() -> beanDefinition.enumValue(JdbcRepository.class, "dialectName", Dialect.class).orElse(Dialect.ANSI));
            dialects.put(beanDefinition.getBeanType(), dialect);
            QueryBuilder qb = queryBuilders.get(dialect);
            if (qb == null) {
                queryBuilders.put(dialect, new SqlQueryBuilder(dialect));
            }
        }
    }
}
 
Example #5
Source File: TransactionalConnectionInterceptor.java    From micronaut-data with Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor.
 *
 * @param beanContext The bean context
 * @param qualifier   The qualifier
 */
@Internal
TransactionalConnectionInterceptor(BeanContext beanContext, Qualifier<DataSource> qualifier) {
    DataSource dataSource = beanContext.getBean(DataSource.class, qualifier);
    if (dataSource instanceof DelegatingDataSource) {
        dataSource = ((DelegatingDataSource) dataSource).getTargetDataSource();
    }
    this.dataSource = dataSource;
}
 
Example #6
Source File: RequiresSingleCandidateCondition.java    From micronaut-spring with Apache License 2.0 5 votes vote down vote up
@Override
public boolean matches(ConditionContext context) {
    final Class<?> type = context.getComponent().findAnnotation(RequiresSingleCandidate.class).flatMap(ann -> ann.getValue(Class.class)).orElse(null);
    if (type != null) {
        final BeanContext beanContext = context.getBeanContext();
        return beanContext.findBeanDefinition(type).isPresent();
    }
    return true;
}
 
Example #7
Source File: HibernatePresenceCondition.java    From micronaut-sql with Apache License 2.0 4 votes vote down vote up
@Override
public boolean matches(ConditionContext context) {
    BeanContext beanContext = context.getBeanContext();
    return !ClassUtils.isPresent("io.micronaut.configuration.hibernate.jpa.HibernateTransactionManagerFactory", beanContext.getClassLoader());
}
 
Example #8
Source File: EntitiesInPackageCondition.java    From micronaut-sql with Apache License 2.0 4 votes vote down vote up
@Override
public boolean matches(ConditionContext context) {
    final AnnotationMetadataProvider component = context.getComponent();
    if (component instanceof BeanDefinition) {
        BeanDefinition<?> definition = (BeanDefinition<?>) component;
        final BeanContext beanContext = context.getBeanContext();
        if (beanContext instanceof ApplicationContext) {

            final Optional<String> name = definition instanceof NameResolver ? ((NameResolver) definition).resolveName() : Optional.empty();
            final Qualifier<JpaConfiguration> q = Qualifiers.byName(name.orElse("default"));
            final Optional<JpaConfiguration> jpaConfiguration = beanContext.findBean(JpaConfiguration.class, q);
            JpaConfiguration.EntityScanConfiguration entityScanConfig = jpaConfiguration.map(JpaConfiguration::getEntityScanConfiguration).orElse(null);

            if (entityScanConfig == null) {
                return false;
            }

            boolean isClasspathScanEnabled = entityScanConfig.isClasspath();
            String[] packagesToScan = entityScanConfig.getPackages();
            boolean hasEntitiesOnClassPath = false;
            boolean hasIntrospections = false;
            if (isClasspathScanEnabled) {
                final Environment environment = ((ApplicationContext) beanContext).getEnvironment();
                if (ArrayUtils.isNotEmpty(packagesToScan)) {
                    hasEntitiesOnClassPath = environment.scan(Entity.class, packagesToScan).findAny().isPresent();
                } else {
                    hasEntitiesOnClassPath = environment.scan(Entity.class).findAny().isPresent();
                }
            } else {
                if (ArrayUtils.isNotEmpty(packagesToScan)) {
                    hasIntrospections = !BeanIntrospector.SHARED
                            .findIntrospections(Entity.class, packagesToScan).isEmpty();
                } else {
                    hasIntrospections = !BeanIntrospector.SHARED
                            .findIntrospections(Entity.class).isEmpty();
                }
            }

            return hasEntitiesOnClassPath || hasIntrospections;
        }
    }
    return true;
}
 
Example #9
Source File: TestActiveCondition.java    From micronaut-test with Apache License 2.0 4 votes vote down vote up
@Override
public boolean matches(ConditionContext context) {
    if (context.getComponent() instanceof BeanDefinition) {
        BeanDefinition<?> definition = (BeanDefinition<?>) context.getComponent();
        final BeanContext beanContext = context.getBeanContext();
        final Optional<Class<?>> declaringType = definition.getDeclaringType();

        if (beanContext instanceof ApplicationContext) {
            ApplicationContext applicationContext = (ApplicationContext) beanContext;
            final Class activeSpecClazz = applicationContext.get(ACTIVE_SPEC_CLAZZ, Class.class).orElse(null);
            final String activeSpecName = Optional.ofNullable(activeSpecClazz).map(clazz -> clazz.getPackage().getName() + "." + clazz.getSimpleName()).orElse(null);
            if (definition.isAnnotationPresent(MockBean.class) && declaringType.isPresent()) {
                final Class<?> declaringTypeClass = declaringType.get();
                String declaringTypeName = declaringTypeClass.getName();
                if (activeSpecClazz != null) {
                    if (definition.isProxy()) {
                        final String packageName = NameUtils.getPackageName(activeSpecName);
                        final String simpleName = NameUtils.getSimpleName(activeSpecName);
                        final String rootName = packageName + ".$" + simpleName;
                        return declaringTypeClass.isAssignableFrom(activeSpecClazz) || declaringTypeName.equals(rootName) || declaringTypeName.startsWith(rootName + "$");
                    } else {
                        return declaringTypeClass.isAssignableFrom(activeSpecClazz) || activeSpecName.equals(declaringTypeName) || declaringTypeName.startsWith(activeSpecName + "$");
                    }
                } else {
                    context.fail(
                            "@MockBean of type " + definition.getBeanType() + " not within scope of parent test."
                    );
                    return false;
                }
            } else {
                if (activeSpecName != null) {
                    boolean beanTypeMatches = activeSpecName.equals(definition.getBeanType().getName());
                    return beanTypeMatches || (declaringType.isPresent() && activeSpecClazz == declaringType.get());
                } else {
                    return false;
                }
            }
        } else {
            return false;
        }
    } else {
        return true;
    }
}
 
Example #10
Source File: MicronautBeanFactory.java    From micronaut-spring with Apache License 2.0 4 votes vote down vote up
/**
 * @return The backing Micronaut bean context.
 */
public BeanContext getBeanContext() {
    return beanContext;
}
 
Example #11
Source File: GrpcEmbeddedServerListener.java    From micronaut-grpc with Apache License 2.0 2 votes vote down vote up
/**
 * Default constructor.
 * @param beanContext The bean context
 */
GrpcEmbeddedServerListener(BeanContext beanContext) {
    this.beanContext = beanContext;
}
 
Example #12
Source File: TransactionalSessionInterceptor.java    From micronaut-sql with Apache License 2.0 2 votes vote down vote up
/**
 * Default constructor.
 *
 * @param beanContext The bean context
 * @param qualifier   The qualifier
 */
@Internal
TransactionalSessionInterceptor(BeanContext beanContext, Qualifier<SessionFactory> qualifier) {
    this.sessionFactory = beanContext.getBean(SessionFactory.class, qualifier);
}
 
Example #13
Source File: JsonSerdeRegistry.java    From micronaut-kafka with Apache License 2.0 2 votes vote down vote up
/**
 * Constructs a new instance.
 *
 * @param beanContext The bean context
 */
protected JsonSerdeRegistry(BeanContext beanContext) {
    this.beanContext = beanContext;
}
 
Example #14
Source File: GrpcEmbeddedServerListener.java    From micronaut-grpc with Apache License 2.0 2 votes vote down vote up
/**
 * Default constructor.
 * @param beanContext The bean context
 */
GrpcEmbeddedServerListener(BeanContext beanContext) {
    this.beanContext = beanContext;
}