io.micronaut.context.Qualifier Java Examples

The following examples show how to use io.micronaut.context.Qualifier. 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: DataIntroductionAdvice.java    From micronaut-data with Apache License 2.0 5 votes vote down vote up
private @NonNull
DataInterceptor<Object, Object> findInterceptor(
        @Nullable String dataSourceName,
        @NonNull Class<?> operationsType,
        @NonNull Class<?> interceptorType) {
    DataInterceptor interceptor;
    if (!RepositoryOperations.class.isAssignableFrom(operationsType)) {
        throw new IllegalArgumentException("Repository type must be an instance of RepositoryOperations!");
    }

    RepositoryOperations datastore;
    try {
        if (dataSourceName != null) {
            Qualifier qualifier = Qualifiers.byName(dataSourceName);
            datastore = (RepositoryOperations) beanLocator.getBean(operationsType, qualifier);
        } else {
            datastore = (RepositoryOperations) beanLocator.getBean(operationsType);
        }
    } catch (NoSuchBeanException e) {
        throw new ConfigurationException("No backing RepositoryOperations configured for repository. Check your configuration and try again", e);
    }
    BeanIntrospection<Object> introspection = BeanIntrospector.SHARED.findIntrospections(ref -> interceptorType.isAssignableFrom(ref.getBeanType())).stream().findFirst().orElseThrow(() ->
            new DataAccessException("No Data interceptor found for type: " + interceptorType)
    );
    if (introspection.getConstructorArguments().length == 0) {
        interceptor = (DataInterceptor) introspection.instantiate();
    } else {
        interceptor = (DataInterceptor) introspection.instantiate(datastore);
    }
    return interceptor;
}
 
Example #2
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 #3
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 #4
Source File: MicronautBeanProcessor.java    From micronaut-spring with Apache License 2.0 4 votes vote down vote up
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    if (environment != null) {
        String[] profiles = getProfiles();
        micronautContext = new DefaultApplicationContext(profiles) {
            DefaultEnvironment env = new DefaultEnvironment(() -> Arrays.asList(profiles)) {
                @Override
                public io.micronaut.context.env.Environment start() {
                    return this;
                }

                @Override
                public io.micronaut.context.env.Environment stop() {
                    return this;
                }

                @Override
                public boolean containsProperty(String name) {
                    return environment.containsProperty(name);
                }

                @Override
                public boolean containsProperties(String name) {
                    return environment.containsProperty(name);
                }

                @Override
                public <T> Optional<T> getProperty(String name, ArgumentConversionContext<T> conversionContext) {
                    return Optional.ofNullable(environment.getProperty(name, conversionContext.getArgument().getType()));
                }
            };

            @Override
            public io.micronaut.context.env.Environment getEnvironment() {
                return env;
            }
        };
    } else {
        micronautContext = new DefaultApplicationContext();
    }
    micronautContext.start();

    micronautBeanQualifierTypes
            .forEach(micronautBeanQualifierType -> {
        Qualifier<Object> micronautBeanQualifier;
        if (micronautBeanQualifierType.isAnnotation()) {
            micronautBeanQualifier = Qualifiers.byStereotype((Class<? extends Annotation>) micronautBeanQualifierType);
        } else {
            micronautBeanQualifier = Qualifiers.byType(micronautBeanQualifierType);
        }
        micronautContext.getBeanDefinitions(micronautBeanQualifier)
                .forEach(definition -> {
                    final BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder
                            .rootBeanDefinition(MicronautSpringBeanFactory.class.getName());
                    beanDefinitionBuilder.addPropertyValue(MICRONAUT_BEAN_TYPE_PROPERTY_NAME, definition.getBeanType());
                    beanDefinitionBuilder.addPropertyValue(MICRONAUT_CONTEXT_PROPERTY_NAME, micronautContext);
                    beanDefinitionBuilder.addPropertyValue(MICRONAUT_SINGLETON_PROPERTY_NAME, definition.isSingleton());
                    ((DefaultListableBeanFactory) beanFactory).registerBeanDefinition(definition.getName(), beanDefinitionBuilder.getBeanDefinition());
                });
    });
}
 
Example #5
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);
}