Search in sources :

Example 1 with NameResolver

use of io.micronaut.core.naming.NameResolver in project micronaut-liquibase by micronaut-projects.

the class LiquibaseMigrationRunner method onCreated.

@Override
public DataSource onCreated(BeanCreatedEvent<DataSource> event) {
    DataSource dataSource = event.getBean();
    if (event.getBeanDefinition() instanceof NameResolver) {
        ((NameResolver) event.getBeanDefinition()).resolveName().ifPresent(name -> {
            applicationContext.findBean(LiquibaseConfigurationProperties.class, Qualifiers.byName(name)).ifPresent(liquibaseConfig -> {
                DataSource unwrappedDataSource = dataSourceResolver.resolve(dataSource);
                run(liquibaseConfig, unwrappedDataSource);
            });
        });
    }
    return dataSource;
}
Also used : NameResolver(io.micronaut.core.naming.NameResolver) DataSource(javax.sql.DataSource)

Example 2 with NameResolver

use of io.micronaut.core.naming.NameResolver in project micronaut-core by micronaut-projects.

the class NameQualifier method reduce.

@Override
public <BT extends BeanType<T>> Stream<BT> reduce(Class<T> beanType, Stream<BT> candidates) {
    check("beanType", beanType).notNull();
    check("candidates", candidates).notNull();
    return candidates.filter(candidate -> {
        if (!QualifierUtils.matchType(beanType, candidate)) {
            return false;
        }
        if (QualifierUtils.matchAny(beanType, candidate)) {
            return true;
        }
        AnnotationMetadata annotationMetadata = candidate.getAnnotationMetadata();
        // here we resolved the declared Qualifier of the bean
        String thisName = annotationMetadata.findDeclaredAnnotation(AnnotationUtil.NAMED).flatMap(AnnotationValue::stringValue).orElse(null);
        if (thisName == null && candidate instanceof BeanDefinition) {
            Qualifier<?> qualifier = ((BeanDefinition<?>) candidate).getDeclaredQualifier();
            if (qualifier != null && qualifier.contains((Qualifier) this)) {
                return true;
            }
        }
        if (thisName == null) {
            if (candidate instanceof NameResolver) {
                Optional<String> resolvedName = ((NameResolver) candidate).resolveName();
                thisName = resolvedName.orElse(candidate.getBeanType().getSimpleName());
            } else {
                thisName = candidate.getBeanType().getSimpleName();
            }
        }
        return thisName.equalsIgnoreCase(name) || thisName.equalsIgnoreCase(name + beanType.getSimpleName());
    });
}
Also used : Qualifier(io.micronaut.context.Qualifier) BeanDefinition(io.micronaut.inject.BeanDefinition) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) NameResolver(io.micronaut.core.naming.NameResolver)

Example 3 with NameResolver

use of io.micronaut.core.naming.NameResolver in project micronaut-sql by micronaut-projects.

the class EntitiesInPackageCondition method matches.

@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;
}
Also used : Entity(javax.persistence.Entity) BeanDefinition(io.micronaut.inject.BeanDefinition) BeanContext(io.micronaut.context.BeanContext) ApplicationContext(io.micronaut.context.ApplicationContext) Environment(io.micronaut.context.env.Environment) AnnotationMetadataProvider(io.micronaut.core.annotation.AnnotationMetadataProvider) NameResolver(io.micronaut.core.naming.NameResolver) JpaConfiguration(io.micronaut.configuration.hibernate.jpa.JpaConfiguration)

Aggregations

NameResolver (io.micronaut.core.naming.NameResolver)3 BeanDefinition (io.micronaut.inject.BeanDefinition)2 JpaConfiguration (io.micronaut.configuration.hibernate.jpa.JpaConfiguration)1 ApplicationContext (io.micronaut.context.ApplicationContext)1 BeanContext (io.micronaut.context.BeanContext)1 Qualifier (io.micronaut.context.Qualifier)1 Environment (io.micronaut.context.env.Environment)1 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)1 AnnotationMetadataProvider (io.micronaut.core.annotation.AnnotationMetadataProvider)1 Entity (javax.persistence.Entity)1 DataSource (javax.sql.DataSource)1