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