Search in sources :

Example 1 with DataMethod

use of io.micronaut.data.intercept.annotation.DataMethod in project micronaut-coherence by micronaut-projects.

the class CoherenceDataIntroductionAdvice method intercept.

@Override
public Object intercept(MethodInvocationContext<Object, Object> context) {
    RepositoryMethodKey key = new RepositoryMethodKey(context.getTarget(), context.getExecutableMethod());
    DataInterceptor<Object, Object> dataInterceptor = interceptorMap.get(key);
    if (dataInterceptor == null) {
        String dataSourceName = context.stringValue(Repository.class).orElse(null);
        Class<?> operationsType = context.classValue(RepositoryConfiguration.class, "operations").orElse(PrimaryRepositoryOperations.class);
        Class<?> interceptorType = context.classValue(DataMethod.class, DataMethod.META_MEMBER_INTERCEPTOR).orElse(null);
        if (interceptorType != null && DataInterceptor.class.isAssignableFrom(interceptorType)) {
            DataInterceptor<Object, Object> childInterceptor = findInterceptor(dataSourceName, operationsType, interceptorType);
            interceptorMap.put(key, childInterceptor);
            return childInterceptor.intercept(key, context);
        } else {
            final AnnotationValue<DataMethod> declaredAnnotation = context.getDeclaredAnnotation(DataMethod.class);
            if (declaredAnnotation != null) {
                interceptorType = declaredAnnotation.classValue(DataMethod.META_MEMBER_INTERCEPTOR).orElse(null);
                if (interceptorType != null && DataInterceptor.class.isAssignableFrom(interceptorType)) {
                    DataInterceptor<Object, Object> childInterceptor = findInterceptor(dataSourceName, operationsType, interceptorType);
                    interceptorMap.put(key, childInterceptor);
                    return childInterceptor.intercept(key, context);
                }
            }
            final String interceptorName = context.getAnnotationMetadata().stringValue(DataMethod.class, DataMethod.META_MEMBER_INTERCEPTOR).orElse(null);
            if (interceptorName != null) {
                throw new IllegalStateException("Micronaut Data Interceptor [" + interceptorName + "] is not on" + " the classpath but required by the method: " + context.getExecutableMethod());
            }
            throw new IllegalStateException("Micronaut Data method is missing compilation time query " + "information. Ensure that the Micronaut Data annotation processors are declared in your" + " build and try again with a clean re-build.");
        }
    } else {
        return dataInterceptor.intercept(key, context);
    }
}
Also used : Repository(io.micronaut.data.annotation.Repository) DataInterceptor(io.micronaut.data.intercept.DataInterceptor) DataMethod(io.micronaut.data.intercept.annotation.DataMethod) RepositoryMethodKey(io.micronaut.data.intercept.RepositoryMethodKey) RepositoryConfiguration(io.micronaut.data.annotation.RepositoryConfiguration)

Aggregations

Repository (io.micronaut.data.annotation.Repository)1 RepositoryConfiguration (io.micronaut.data.annotation.RepositoryConfiguration)1 DataInterceptor (io.micronaut.data.intercept.DataInterceptor)1 RepositoryMethodKey (io.micronaut.data.intercept.RepositoryMethodKey)1 DataMethod (io.micronaut.data.intercept.annotation.DataMethod)1