Skip to content

Commit 05b7571

Browse files
committed
cdi portable extension
1 parent 4c1250c commit 05b7571

File tree

9 files changed

+171
-0
lines changed

9 files changed

+171
-0
lines changed

flyway-cdi-extension/pom.xml

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>flyway-cdi-extension</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>1.8</maven.compiler.source>
13+
<maven.compiler.target>1.8</maven.compiler.target>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>javax.enterprise</groupId>
19+
<artifactId>cdi-api</artifactId>
20+
<version>2.0.SP1</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.jboss.weld.se</groupId>
24+
<artifactId>weld-se-core</artifactId>
25+
<version>3.0.5.Final</version>
26+
<scope>runtime</scope>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.flywaydb</groupId>
30+
<artifactId>flyway-core</artifactId>
31+
<version>5.1.4</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.apache.tomcat</groupId>
35+
<artifactId>tomcat-jdbc</artifactId>
36+
<version>8.5.33</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>javax.annotation</groupId>
40+
<artifactId>javax.annotation-api</artifactId>
41+
<version>1.3.2</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>com.h2database</groupId>
45+
<artifactId>h2</artifactId>
46+
<version>1.4.197</version>
47+
<scope>runtime</scope>
48+
</dependency>
49+
</dependencies>
50+
51+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.cdi.extension;
2+
3+
import javax.inject.Qualifier;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
import static java.lang.annotation.ElementType.*;
9+
10+
@Retention(RetentionPolicy.RUNTIME)
11+
@Target({FIELD, METHOD, PARAMETER, TYPE})
12+
@Qualifier
13+
public @interface FlywayType {
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.cdi.extension;
2+
3+
import javax.annotation.sql.DataSourceDefinition;
4+
import javax.enterprise.context.ApplicationScoped;
5+
import javax.enterprise.inject.se.SeContainer;
6+
import javax.enterprise.inject.se.SeContainerInitializer;
7+
8+
@ApplicationScoped
9+
@DataSourceDefinition(name = "ds", className = "org.h2.Driver", url = "jdbc:h2:mem:testdb")
10+
public class MainApp {
11+
public static void main(String[] args) {
12+
SeContainerInitializer initializer = SeContainerInitializer.newInstance();
13+
try (SeContainer container = initializer.initialize()) {
14+
}
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<beans version="2.0" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
4+
http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
5+
bean-discovery-mode="annotated">
6+
</beans>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
com.baeldung.cdi.extension.FlywayExtension
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
create table PERSON (
2+
ID int not null,
3+
NAME varchar(100) not null
4+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
insert into PERSON (ID, NAME) values (1, 'Axel');
2+
insert into PERSON (ID, NAME) values (2, 'Mr. Foo');
3+
insert into PERSON (ID, NAME) values (3, 'Ms. Bar');

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,7 @@
681681
<!-- <module>guest\webservices\rest-client</module> --><!-- guest post on different site -->
682682
<!-- <module>guest\webservices\rest-server</module> --><!-- guest post on different site -->
683683
<!-- <module>guest\webservices\spring-rest-service</module> --><!-- guest post on different site -->
684+
<module>flyway-cdi-extension</module>
684685
</modules>
685686

686687
</profile>

0 commit comments

Comments
 (0)