|
| 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.util.Arrays; |
| 13 | + |
| 14 | +import org.openmrs.module.fhir2.api.FhirHelperService; |
| 15 | +import org.openmrs.module.fhir2.api.FhirService; |
| 16 | +import org.openmrs.module.fhir2.api.dao.FhirDaoAop; |
| 17 | +import org.openmrs.module.fhir2.api.translators.FhirTranslator; |
| 18 | +import org.springframework.aop.TargetSource; |
| 19 | +import org.springframework.aop.framework.adapter.AdvisorAdapterRegistry; |
| 20 | +import org.springframework.aop.framework.adapter.GlobalAdvisorAdapterRegistry; |
| 21 | +import org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator; |
| 22 | +import org.springframework.beans.BeansException; |
| 23 | +import org.springframework.beans.factory.BeanFactory; |
| 24 | +import org.springframework.beans.factory.config.ConfigurableBeanFactory; |
| 25 | +import org.springframework.cache.annotation.CacheConfig; |
| 26 | +import org.springframework.cache.annotation.CacheEvict; |
| 27 | +import org.springframework.cache.annotation.CachePut; |
| 28 | +import org.springframework.cache.annotation.Cacheable; |
| 29 | +import org.springframework.core.annotation.AnnotationUtils; |
| 30 | +import org.springframework.lang.NonNull; |
| 31 | +import org.springframework.util.Assert; |
| 32 | + |
| 33 | +/** |
| 34 | + * This is a Spring Auto-proxy creator that matches beans that implement the FhirDao marker |
| 35 | + * interface, which is where authorization is checked in the FHIR2 module and sub-implementations. |
| 36 | + * This is used to ensure that our custom interceptors are applied to the classes in the FHIR2 |
| 37 | + * module, even though they do not follow our usual pattern of registering with the service |
| 38 | + * container. |
| 39 | + */ |
| 40 | +public class FhirAutoProxyCreator extends AbstractAutoProxyCreator { |
| 41 | + |
| 42 | + private static final String[] DAO_PROXIES = new String[] { "authorizationInterceptor", "cacheInterceptor", |
| 43 | + "transactionInterceptor" }; |
| 44 | + |
| 45 | + private static final String[] CACHEABLE_PROXIES = new String[] { "cacheInterceptor" }; |
| 46 | + |
| 47 | + private static volatile Object[] daoProxies = null; |
| 48 | + |
| 49 | + private static volatile Object[] cacheProxies = null; |
| 50 | + |
| 51 | + @Override |
| 52 | + protected Object[] getAdvicesAndAdvisorsForBean(@NonNull Class<?> beanClass, @NonNull String beanName, |
| 53 | + TargetSource customTargetSource) throws BeansException { |
| 54 | + if (FhirDaoAop.class.isAssignableFrom(beanClass)) { |
| 55 | + return getDaoProxies(); |
| 56 | + } else if (FhirTranslator.class.isAssignableFrom(beanClass) || FhirService.class.isAssignableFrom(beanClass) |
| 57 | + || FhirHelperService.class.isAssignableFrom(beanClass)) { |
| 58 | + boolean shouldProxy = AnnotationUtils.findAnnotation(beanClass, CacheConfig.class) != null |
| 59 | + || AnnotationUtils.findAnnotation(beanClass, Cacheable.class) != null |
| 60 | + || AnnotationUtils.findAnnotation(beanClass, CacheEvict.class) != null |
| 61 | + || AnnotationUtils.findAnnotation(beanClass, CachePut.class) != null |
| 62 | + || Arrays.stream(beanClass.getMethods()) |
| 63 | + .anyMatch((m) -> AnnotationUtils.findAnnotation(m, Cacheable.class) != null |
| 64 | + || AnnotationUtils.findAnnotation(m, CacheEvict.class) != null |
| 65 | + || AnnotationUtils.findAnnotation(m, CachePut.class) != null); |
| 66 | + |
| 67 | + if (shouldProxy) { |
| 68 | + return getCacheableProxies(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return DO_NOT_PROXY; |
| 73 | + } |
| 74 | + |
| 75 | + private Object[] getDaoProxies() { |
| 76 | + if (daoProxies == null) { |
| 77 | + synchronized (FhirAutoProxyCreator.class) { |
| 78 | + if (daoProxies == null) { |
| 79 | + Object[] proxies = new Object[DAO_PROXIES.length]; |
| 80 | + BeanFactory bf = getBeanFactory(); |
| 81 | + Assert.state(bf != null, "BeanFactory required for resolving interceptor names"); |
| 82 | + |
| 83 | + ConfigurableBeanFactory cbf = (bf instanceof ConfigurableBeanFactory ? (ConfigurableBeanFactory) bf |
| 84 | + : null); |
| 85 | + AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance(); |
| 86 | + |
| 87 | + for (int i = 0; i < DAO_PROXIES.length; i++) { |
| 88 | + String beanName = DAO_PROXIES[i]; |
| 89 | + if (cbf == null || !cbf.isCurrentlyInCreation(beanName)) { |
| 90 | + proxies[i] = advisorAdapterRegistry.wrap(bf.getBean(beanName)); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + daoProxies = proxies; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return daoProxies; |
| 100 | + } |
| 101 | + |
| 102 | + private Object[] getCacheableProxies() { |
| 103 | + if (cacheProxies == null) { |
| 104 | + synchronized (FhirAutoProxyCreator.class) { |
| 105 | + if (cacheProxies == null) { |
| 106 | + Object[] proxies = new Object[CACHEABLE_PROXIES.length]; |
| 107 | + BeanFactory bf = getBeanFactory(); |
| 108 | + Assert.state(bf != null, "BeanFactory required for resolving interceptor names"); |
| 109 | + |
| 110 | + ConfigurableBeanFactory cbf = (bf instanceof ConfigurableBeanFactory ? (ConfigurableBeanFactory) bf |
| 111 | + : null); |
| 112 | + AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance(); |
| 113 | + |
| 114 | + for (int i = 0; i < CACHEABLE_PROXIES.length; i++) { |
| 115 | + String beanName = CACHEABLE_PROXIES[i]; |
| 116 | + if (cbf == null || !cbf.isCurrentlyInCreation(beanName)) { |
| 117 | + proxies[i] = advisorAdapterRegistry.wrap(bf.getBean(beanName)); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + cacheProxies = proxies; |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return cacheProxies; |
| 127 | + } |
| 128 | +} |
0 commit comments