Skip to content

Commit bf912b1

Browse files
authored
Merge pull request eugenp#5145 from eelhazati/master
BAEL-1998 cdi portable extensions
2 parents 42f4355 + b403f5d commit bf912b1

12 files changed

Lines changed: 246 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
<artifactId>flyway-cdi</artifactId>
8+
9+
<parent>
10+
<groupId>com.baeldung</groupId>
11+
<artifactId>cdi-portable-extension</artifactId>
12+
<version>1.0-SNAPSHOT</version>
13+
</parent>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>javax.enterprise</groupId>
18+
<artifactId>cdi-api</artifactId>
19+
<version>2.0.SP1</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.flywaydb</groupId>
23+
<artifactId>flyway-core</artifactId>
24+
<version>5.1.4</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.apache.tomcat</groupId>
28+
<artifactId>tomcat-jdbc</artifactId>
29+
<version>8.5.33</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>javax.annotation</groupId>
33+
<artifactId>javax.annotation-api</artifactId>
34+
<version>1.3.2</version>
35+
</dependency>
36+
</dependencies>
37+
38+
</project>
Lines changed: 74 additions & 0 deletions
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+
}
Lines changed: 14 additions & 0 deletions
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+
}
Lines changed: 6 additions & 0 deletions
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>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
com.baeldung.cdi.extension.FlywayExtension
2+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
<artifactId>main-app</artifactId>
8+
<packaging>jar</packaging>
9+
10+
<parent>
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>cdi-portable-extension</artifactId>
13+
<version>1.0-SNAPSHOT</version>
14+
</parent>
15+
16+
<dependencies>
17+
18+
<dependency>
19+
<groupId>javax.enterprise</groupId>
20+
<artifactId>cdi-api</artifactId>
21+
<version>2.0.SP1</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.jboss.weld.se</groupId>
25+
<artifactId>weld-se-core</artifactId>
26+
<version>3.0.5.Final</version>
27+
<scope>runtime</scope>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>com.baeldung</groupId>
32+
<artifactId>flyway-cdi</artifactId>
33+
<version>1.0-SNAPSHOT</version>
34+
<scope>runtime</scope>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>com.h2database</groupId>
39+
<artifactId>h2</artifactId>
40+
<version>1.4.197</version>
41+
<scope>runtime</scope>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>javax.annotation</groupId>
46+
<artifactId>javax.annotation-api</artifactId>
47+
<version>1.3.2</version>
48+
</dependency>
49+
50+
</dependencies>
51+
52+
</project>
Lines changed: 16 additions & 0 deletions
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+
}
Lines changed: 6 additions & 0 deletions
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>
Lines changed: 4 additions & 0 deletions
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+
);
Lines changed: 3 additions & 0 deletions
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');

0 commit comments

Comments
 (0)