|
| 1 | +package com.baeldung.cdi.extension; |
| 2 | + |
| 3 | +import org.apache.tomcat.jdbc.pool.DataSource; |
| 4 | +import org.flywaydb.core.Flyway; |
| 5 | + |
| 6 | +import javax.annotation.sql.DataSourceDefinition; |
| 7 | +import javax.enterprise.context.ApplicationScoped; |
| 8 | +import javax.enterprise.event.Observes; |
| 9 | +import javax.enterprise.inject.Any; |
| 10 | +import javax.enterprise.inject.Default; |
| 11 | +import javax.enterprise.inject.literal.InjectLiteral; |
| 12 | +import javax.enterprise.inject.spi.*; |
| 13 | +import javax.enterprise.util.AnnotationLiteral; |
| 14 | + |
| 15 | + |
| 16 | +/** |
| 17 | + * Flyway is now under CDI container like: |
| 18 | + * |
| 19 | + * @ApplicationScoped |
| 20 | + * @FlywayType public class Flyway{ |
| 21 | + * @Inject setDataSource(DataSource dataSource){ |
| 22 | + * //... |
| 23 | + * } |
| 24 | + * } |
| 25 | + */ |
| 26 | + |
| 27 | +public class FlywayExtension implements Extension { |
| 28 | + |
| 29 | + DataSourceDefinition dataSourceDefinition = null; |
| 30 | + |
| 31 | + public void registerFlywayType(@Observes BeforeBeanDiscovery bbdEvent) { |
| 32 | + bbdEvent.addAnnotatedType(Flyway.class, Flyway.class.getName()); |
| 33 | + } |
| 34 | + |
| 35 | + public void detectDataSourceDefinition(@Observes @WithAnnotations(DataSourceDefinition.class) ProcessAnnotatedType<?> patEvent) { |
| 36 | + AnnotatedType at = patEvent.getAnnotatedType(); |
| 37 | + dataSourceDefinition = at.getAnnotation(DataSourceDefinition.class); |
| 38 | + } |
| 39 | + |
| 40 | + public void processAnnotatedType(@Observes ProcessAnnotatedType<Flyway> patEvent) { |
| 41 | + patEvent.configureAnnotatedType() |
| 42 | + //Add Scope |
| 43 | + .add(ApplicationScoped.Literal.INSTANCE) |
| 44 | + //Add Qualifier |
| 45 | + .add(new AnnotationLiteral<FlywayType>() { |
| 46 | + }) |
| 47 | + //Decorate setDataSource(DataSource dataSource){} with @Inject |
| 48 | + .filterMethods(annotatedMethod -> { |
| 49 | + return annotatedMethod.getParameters().size() == 1 && |
| 50 | + annotatedMethod.getParameters().get(0).getBaseType().equals(javax.sql.DataSource.class); |
| 51 | + }) |
| 52 | + .findFirst().get().add(InjectLiteral.INSTANCE); |
| 53 | + } |
| 54 | + |
| 55 | + void afterBeanDiscovery(@Observes AfterBeanDiscovery abdEvent, BeanManager bm) { |
| 56 | + abdEvent.addBean() |
| 57 | + .types(javax.sql.DataSource.class, DataSource.class) |
| 58 | + .qualifiers(new AnnotationLiteral<Default>() {}, new AnnotationLiteral<Any>() {}) |
| 59 | + .scope(ApplicationScoped.class) |
| 60 | + .name(DataSource.class.getName()) |
| 61 | + .beanClass(DataSource.class) |
| 62 | + .createWith(creationalContext -> { |
| 63 | + DataSource instance = new DataSource(); |
| 64 | + instance.setUrl(dataSourceDefinition.url()); |
| 65 | + instance.setDriverClassName(dataSourceDefinition.className()); |
| 66 | + return instance; |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + void runFlywayMigration(@Observes AfterDeploymentValidation adv, BeanManager manager) { |
| 71 | + Flyway flyway = manager.createInstance().select(Flyway.class, new AnnotationLiteral<FlywayType>() {}).get(); |
| 72 | + flyway.migrate(); |
| 73 | + } |
| 74 | +} |
0 commit comments