Search in sources :

Example 1 with RepositoryOperations

use of io.micronaut.data.operations.RepositoryOperations in project micronaut-data by micronaut-projects.

the class DataIntroductionAdvice method findInterceptor.

@NonNull
private 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 -> ref.isPresent() && 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;
}
Also used : RepositoryConfiguration(io.micronaut.data.annotation.RepositoryConfiguration) EmptyResultException(io.micronaut.data.exceptions.EmptyResultException) Qualifier(io.micronaut.context.Qualifier) CompletableFuture(java.util.concurrent.CompletableFuture) Internal(io.micronaut.core.annotation.Internal) InterceptedMethod(io.micronaut.aop.InterceptedMethod) DataMethod(io.micronaut.data.intercept.annotation.DataMethod) CoroutineTxHelper(io.micronaut.transaction.interceptor.CoroutineTxHelper) MethodInterceptor(io.micronaut.aop.MethodInterceptor) Nullable(io.micronaut.core.annotation.Nullable) PrimaryRepositoryOperations(io.micronaut.data.operations.PrimaryRepositoryOperations) Map(java.util.Map) DataAccessException(io.micronaut.data.exceptions.DataAccessException) TransactionSynchronizationManager(io.micronaut.transaction.support.TransactionSynchronizationManager) BeanLocator(io.micronaut.context.BeanLocator) KotlinInterceptedMethod(io.micronaut.aop.kotlin.KotlinInterceptedMethod) ConfigurationException(io.micronaut.context.exceptions.ConfigurationException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Qualifiers(io.micronaut.inject.qualifiers.Qualifiers) Singleton(jakarta.inject.Singleton) CompletionException(java.util.concurrent.CompletionException) KotlinUtils(io.micronaut.core.util.KotlinUtils) Objects(java.util.Objects) NonNull(io.micronaut.core.annotation.NonNull) CompletionStage(java.util.concurrent.CompletionStage) RepositoryOperations(io.micronaut.data.operations.RepositoryOperations) NoSuchBeanException(io.micronaut.context.exceptions.NoSuchBeanException) MethodInvocationContext(io.micronaut.aop.MethodInvocationContext) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) BeanIntrospection(io.micronaut.core.beans.BeanIntrospection) Repository(io.micronaut.data.annotation.Repository) Inject(jakarta.inject.Inject) BeanIntrospector(io.micronaut.core.beans.BeanIntrospector) PrimaryRepositoryOperations(io.micronaut.data.operations.PrimaryRepositoryOperations) RepositoryOperations(io.micronaut.data.operations.RepositoryOperations) NoSuchBeanException(io.micronaut.context.exceptions.NoSuchBeanException) ConfigurationException(io.micronaut.context.exceptions.ConfigurationException) Qualifier(io.micronaut.context.Qualifier) DataAccessException(io.micronaut.data.exceptions.DataAccessException) NonNull(io.micronaut.core.annotation.NonNull)

Example 2 with RepositoryOperations

use of io.micronaut.data.operations.RepositoryOperations in project micronaut-coherence by micronaut-projects.

the class CoherenceDataIntroductionAdvice method findInterceptor.

@NonNull
private 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!");
    }
    Qualifier qualifier = Qualifiers.byName(dataSourceName);
    RepositoryOperations datastore;
    try {
        datastore = (RepositoryOperations) beanLocator.getBean(operationsType, qualifier);
    } catch (NoSuchBeanException e) {
        // if there is no explicit configuration, use a DefaultCoherenceRepositoryOperations implementation
        // using the default Coherence session
        datastore = new DefaultCoherenceRepositoryOperations(dataSourceName, (ApplicationContext) beanLocator, (BeanContext) beanLocator);
        ((ApplicationContext) beanLocator).registerSingleton(RepositoryOperations.class, datastore, qualifier);
    }
    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;
}
Also used : RepositoryConfiguration(io.micronaut.data.annotation.RepositoryConfiguration) BeanContext(io.micronaut.context.BeanContext) Qualifier(io.micronaut.context.Qualifier) DefaultCoherenceRepositoryOperations(io.micronaut.coherence.data.ops.DefaultCoherenceRepositoryOperations) DataInterceptor(io.micronaut.data.intercept.DataInterceptor) DataMethod(io.micronaut.data.intercept.annotation.DataMethod) ApplicationContext(io.micronaut.context.ApplicationContext) MethodInterceptor(io.micronaut.aop.MethodInterceptor) Nullable(io.micronaut.core.annotation.Nullable) PrimaryRepositoryOperations(io.micronaut.data.operations.PrimaryRepositoryOperations) Map(java.util.Map) Ordered(io.micronaut.core.order.Ordered) DataAccessException(io.micronaut.data.exceptions.DataAccessException) BeanLocator(io.micronaut.context.BeanLocator) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Qualifiers(io.micronaut.inject.qualifiers.Qualifiers) Singleton(jakarta.inject.Singleton) NonNull(io.micronaut.core.annotation.NonNull) RepositoryOperations(io.micronaut.data.operations.RepositoryOperations) NoSuchBeanException(io.micronaut.context.exceptions.NoSuchBeanException) MethodInvocationContext(io.micronaut.aop.MethodInvocationContext) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) BeanIntrospection(io.micronaut.core.beans.BeanIntrospection) Repository(io.micronaut.data.annotation.Repository) RepositoryMethodKey(io.micronaut.data.intercept.RepositoryMethodKey) BeanIntrospector(io.micronaut.core.beans.BeanIntrospector) DataInterceptor(io.micronaut.data.intercept.DataInterceptor) DefaultCoherenceRepositoryOperations(io.micronaut.coherence.data.ops.DefaultCoherenceRepositoryOperations) PrimaryRepositoryOperations(io.micronaut.data.operations.PrimaryRepositoryOperations) RepositoryOperations(io.micronaut.data.operations.RepositoryOperations) NoSuchBeanException(io.micronaut.context.exceptions.NoSuchBeanException) DefaultCoherenceRepositoryOperations(io.micronaut.coherence.data.ops.DefaultCoherenceRepositoryOperations) Qualifier(io.micronaut.context.Qualifier) DataAccessException(io.micronaut.data.exceptions.DataAccessException) NonNull(io.micronaut.core.annotation.NonNull)

Aggregations

MethodInterceptor (io.micronaut.aop.MethodInterceptor)2 MethodInvocationContext (io.micronaut.aop.MethodInvocationContext)2 BeanLocator (io.micronaut.context.BeanLocator)2 Qualifier (io.micronaut.context.Qualifier)2 NoSuchBeanException (io.micronaut.context.exceptions.NoSuchBeanException)2 AnnotationValue (io.micronaut.core.annotation.AnnotationValue)2 NonNull (io.micronaut.core.annotation.NonNull)2 Nullable (io.micronaut.core.annotation.Nullable)2 BeanIntrospection (io.micronaut.core.beans.BeanIntrospection)2 BeanIntrospector (io.micronaut.core.beans.BeanIntrospector)2 Repository (io.micronaut.data.annotation.Repository)2 RepositoryConfiguration (io.micronaut.data.annotation.RepositoryConfiguration)2 DataAccessException (io.micronaut.data.exceptions.DataAccessException)2 DataMethod (io.micronaut.data.intercept.annotation.DataMethod)2 PrimaryRepositoryOperations (io.micronaut.data.operations.PrimaryRepositoryOperations)2 RepositoryOperations (io.micronaut.data.operations.RepositoryOperations)2 Qualifiers (io.micronaut.inject.qualifiers.Qualifiers)2 Singleton (jakarta.inject.Singleton)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2