|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public License, |
| 3 | + * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 4 | + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under |
| 5 | + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. |
| 6 | + * |
| 7 | + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS |
| 8 | + * graphic logo is a trademark of OpenMRS Inc. |
| 9 | + */ |
| 10 | +package org.openmrs.module.fhir2.spring; |
| 11 | + |
| 12 | +import java.lang.reflect.Method; |
| 13 | +import java.util.Arrays; |
| 14 | + |
| 15 | +import org.openmrs.aop.AuthorizationAdvice; |
| 16 | +import org.openmrs.module.fhir2.api.FhirHelperService; |
| 17 | +import org.openmrs.module.fhir2.api.FhirService; |
| 18 | +import org.openmrs.module.fhir2.api.dao.FhirDaoAop; |
| 19 | +import org.openmrs.module.fhir2.api.translators.FhirTranslator; |
| 20 | +import org.springframework.aop.Advisor; |
| 21 | +import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor; |
| 22 | +import org.springframework.beans.factory.annotation.Autowired; |
| 23 | +import org.springframework.cache.annotation.CacheConfig; |
| 24 | +import org.springframework.cache.annotation.CacheEvict; |
| 25 | +import org.springframework.cache.annotation.CachePut; |
| 26 | +import org.springframework.cache.annotation.Cacheable; |
| 27 | +import org.springframework.cache.interceptor.CacheInterceptor; |
| 28 | +import org.springframework.context.annotation.Bean; |
| 29 | +import org.springframework.context.annotation.Configuration; |
| 30 | +import org.springframework.context.annotation.EnableAspectJAutoProxy; |
| 31 | +import org.springframework.core.annotation.AnnotationUtils; |
| 32 | +import org.springframework.transaction.interceptor.TransactionInterceptor; |
| 33 | + |
| 34 | +/** |
| 35 | + * This is a Spring AOP configuration that add advices to beans that implement the FhirDao marker |
| 36 | + * interface, which is where authorization is checked in the FHIR2 module and sub-implementations. |
| 37 | + * This is used to ensure that our custom interceptors are applied to the classes in the FHIR2 |
| 38 | + * module, even though they do not follow our usual pattern of registering with the service |
| 39 | + * container. |
| 40 | + */ |
| 41 | +@Configuration |
| 42 | +@EnableAspectJAutoProxy |
| 43 | +public class FhirAopConfiguration { |
| 44 | + |
| 45 | + @Bean |
| 46 | + public Advisor createFhirAuthorizationAdvisor(@Autowired AuthorizationAdvice authorizationAdvice) { |
| 47 | + return new StaticMethodMatcherPointcutAdvisor(authorizationAdvice) { |
| 48 | + |
| 49 | + @Override |
| 50 | + public boolean matches(Method method, Class<?> targetClass) { |
| 51 | + return FhirDaoAop.class.isAssignableFrom(targetClass); |
| 52 | + } |
| 53 | + }; |
| 54 | + } |
| 55 | + |
| 56 | + @Bean |
| 57 | + public Advisor createFhirTransactionAdvisor(@Autowired(required = false) TransactionInterceptor transactionInterceptor) { |
| 58 | + if (transactionInterceptor != null) { |
| 59 | + // TransactionInterceptor is not available since core 2.8 as it is done via tx:annotation-driven on all beans |
| 60 | + return new StaticMethodMatcherPointcutAdvisor(transactionInterceptor) { |
| 61 | + |
| 62 | + @Override |
| 63 | + public boolean matches(Method method, Class<?> targetClass) { |
| 64 | + return FhirDaoAop.class.isAssignableFrom(targetClass); |
| 65 | + } |
| 66 | + }; |
| 67 | + } |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Creates advisors for FHIR beans using Spring caching. It is required before OpenMRS core 2.8. |
| 73 | + * |
| 74 | + * @param cacheInterceptor the cache interceptor |
| 75 | + * @return the cache advisor |
| 76 | + */ |
| 77 | + @Bean |
| 78 | + public Advisor createFhirCacheAdvisor(@Autowired(required = false) CacheInterceptor cacheInterceptor) { |
| 79 | + if (cacheInterceptor != null) { |
| 80 | + // CacheInterceptor is not available since core 2.8 as it is done via cache:annotation-driven on all beans |
| 81 | + return new StaticMethodMatcherPointcutAdvisor(cacheInterceptor) { |
| 82 | + |
| 83 | + @Override |
| 84 | + public boolean matches(Method method, Class<?> targetClass) { |
| 85 | + return (FhirTranslator.class.isAssignableFrom(targetClass) |
| 86 | + || FhirService.class.isAssignableFrom(targetClass) |
| 87 | + || FhirHelperService.class.isAssignableFrom(targetClass)) |
| 88 | + && (AnnotationUtils.findAnnotation(targetClass, CacheConfig.class) != null |
| 89 | + || AnnotationUtils.findAnnotation(targetClass, Cacheable.class) != null |
| 90 | + || AnnotationUtils.findAnnotation(targetClass, CacheEvict.class) != null |
| 91 | + || AnnotationUtils.findAnnotation(targetClass, CachePut.class) != null |
| 92 | + || Arrays.stream(targetClass.getMethods()) |
| 93 | + .anyMatch((m) -> AnnotationUtils.findAnnotation(m, Cacheable.class) != null |
| 94 | + || AnnotationUtils.findAnnotation(m, CacheEvict.class) != null |
| 95 | + || AnnotationUtils.findAnnotation(m, CachePut.class) != null)); |
| 96 | + } |
| 97 | + }; |
| 98 | + } |
| 99 | + return null; |
| 100 | + } |
| 101 | +} |
0 commit comments