|
| 1 | +/* |
| 2 | + * Copyright 2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.zonky.test.db.init; |
| 18 | + |
| 19 | +import io.zonky.test.db.context.DatabaseContext; |
| 20 | +import io.zonky.test.db.util.AopProxyUtils; |
| 21 | +import io.zonky.test.db.util.ReflectionUtils; |
| 22 | +import org.aopalliance.aop.Advice; |
| 23 | +import org.aopalliance.intercept.MethodInterceptor; |
| 24 | +import org.aopalliance.intercept.MethodInvocation; |
| 25 | +import org.springframework.aop.Advisor; |
| 26 | +import org.springframework.aop.framework.Advised; |
| 27 | +import org.springframework.aop.framework.AopInfrastructureBean; |
| 28 | +import org.springframework.aop.framework.ProxyFactory; |
| 29 | +import org.springframework.aop.support.NameMatchMethodPointcutAdvisor; |
| 30 | +import org.springframework.beans.factory.config.BeanPostProcessor; |
| 31 | +import org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer; |
| 32 | +import org.springframework.core.Ordered; |
| 33 | +import org.springframework.core.env.Environment; |
| 34 | + |
| 35 | +import javax.sql.DataSource; |
| 36 | + |
| 37 | +import static com.google.common.base.Preconditions.checkState; |
| 38 | + |
| 39 | +public class SpringScriptDatabaseExtension implements BeanPostProcessor, Ordered { |
| 40 | + |
| 41 | + private final boolean enabled; |
| 42 | + |
| 43 | + public SpringScriptDatabaseExtension(Environment environment) { |
| 44 | + this.enabled = environment.getProperty("zonky.test.database.spring.optimized-sql-init.enabled", boolean.class, true); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public int getOrder() { |
| 49 | + return Ordered.HIGHEST_PRECEDENCE + 1; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public Object postProcessBeforeInitialization(Object bean, String beanName) { |
| 54 | + if (!enabled || bean instanceof AopInfrastructureBean) { |
| 55 | + return bean; |
| 56 | + } |
| 57 | + |
| 58 | + if (bean instanceof AbstractScriptDatabaseInitializer) { |
| 59 | + AbstractScriptDatabaseInitializer initializer = (AbstractScriptDatabaseInitializer) bean; |
| 60 | + DataSource dataSource = ReflectionUtils.getField(initializer, "dataSource"); |
| 61 | + DatabaseContext context = AopProxyUtils.getDatabaseContext(dataSource); |
| 62 | + |
| 63 | + if (context != null) { |
| 64 | + if (bean instanceof Advised && !((Advised) bean).isFrozen()) { |
| 65 | + ((Advised) bean).addAdvisor(0, createAdvisor(initializer)); |
| 66 | + return bean; |
| 67 | + } else { |
| 68 | + ProxyFactory proxyFactory = new ProxyFactory(bean); |
| 69 | + proxyFactory.addAdvisor(createAdvisor(initializer)); |
| 70 | + proxyFactory.setProxyTargetClass(true); |
| 71 | + return proxyFactory.getProxy(); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return bean; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public Object postProcessAfterInitialization(Object bean, String beanName) { |
| 81 | + return bean; |
| 82 | + } |
| 83 | + |
| 84 | + protected Advisor createAdvisor(AbstractScriptDatabaseInitializer initializer) { |
| 85 | + Advice advice = new SpringScriptDatabaseExtensionInterceptor(initializer); |
| 86 | + NameMatchMethodPointcutAdvisor advisor = new NameMatchMethodPointcutAdvisor(advice); |
| 87 | + advisor.setMappedNames("afterPropertiesSet", "initializeDatabase"); |
| 88 | + return advisor; |
| 89 | + } |
| 90 | + |
| 91 | + protected static class SpringScriptDatabaseExtensionInterceptor implements MethodInterceptor { |
| 92 | + |
| 93 | + private final AbstractScriptDatabaseInitializer initializer; |
| 94 | + |
| 95 | + protected SpringScriptDatabaseExtensionInterceptor(AbstractScriptDatabaseInitializer initializer) { |
| 96 | + this.initializer = initializer; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public Object invoke(MethodInvocation invocation) throws Throwable { |
| 101 | + String methodName = invocation.getMethod().getName(); |
| 102 | + if (!"afterPropertiesSet".equals(methodName) && !"initializeDatabase".equals(methodName)) { |
| 103 | + return invocation.proceed(); |
| 104 | + } |
| 105 | + |
| 106 | + DataSource dataSource = ReflectionUtils.getField(initializer, "dataSource"); |
| 107 | + DatabaseContext context = AopProxyUtils.getDatabaseContext(dataSource); |
| 108 | + checkState(context != null, "Data source context cannot be resolved"); |
| 109 | + |
| 110 | + context.apply(new SpringScriptDatabasePreparer(initializer)); |
| 111 | + |
| 112 | + return null; |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments