Skip to content

Commit

Permalink
Fixes #414
Browse files Browse the repository at this point in the history
  • Loading branch information
Riduidel committed Feb 8, 2024
1 parent e63cd5d commit bfb4be7
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 50 deletions.
5 changes: 3 additions & 2 deletions adr-tickets-extractor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<groupId>io.github.Riduidel.aadarchi</groupId>
<artifactId>freemarker-cdi-producer</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
5 changes: 5 additions & 0 deletions freemarker-cdi-producer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This component provides a simple CDI Producer allowing one to easily have Freemarker templates injected.

# How

Suppose you have a class named
39 changes: 39 additions & 0 deletions freemarker-cdi-producer/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.github.Riduidel.aadarchi</groupId>
<artifactId>system</artifactId>
<version>0.1.13-SNAPSHOT</version>
</parent>
<artifactId>freemarker-cdi-producer</artifactId>
<name>Freemarker CDI Producer</name>
<description>This CDI producer allows easy usage of Freemarker in a
CDI-compatible application.</description>
<dependencies>
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<dependency>
<groupId>io.github.Riduidel.aadarchi</groupId>
<artifactId>aadarchi-test-utils</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-junit5</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,47 +1,45 @@
package org.ndx.aadarchi.tickets;

import java.io.IOException;

import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;

import org.ndx.aadarchi.base.AgileArchitectureException;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

public class FreemarkerTemplateProducer {
public static class CantLoadTemplate extends AgileArchitectureException {

public CantLoadTemplate(String message, Throwable cause) {
super(message, cause);
}

}
@Produces Configuration createConfiguration() {
Configuration returned = new Configuration();
returned.setObjectWrapper(new DefaultObjectWrapper());
returned.setDefaultEncoding("UTF-8");
returned.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
returned.setIncompatibleEnhancements("2.3.20");
return returned;
}

/**
* Allow to inject a template named
* @param injection
* @return
*/
@Produces Template produceTemplate(InjectionPoint injection, Configuration configuration) {
Class<?> beanClass = injection.getBean().getBeanClass();
configuration.setClassForTemplateLoading(beanClass, "/templates/"+beanClass.getSimpleName());
String templateName = String.format("%s.ftl", injection.getMember().getName());
try {
return configuration.getTemplate(templateName);
} catch (IOException e) {
throw new CantLoadTemplate(String.format("Cant't load template %s", templateName), e);
}
}
}
package org.ndx.aadarchi.freemarker;

import java.io.IOException;

import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

public class FreemarkerTemplateProducer {
public static class CantLoadTemplate extends RuntimeException {

public CantLoadTemplate(String message, Throwable cause) {
super(message, cause);
}

}
@Produces Configuration createConfiguration() {
Configuration returned = new Configuration();
returned.setObjectWrapper(new DefaultObjectWrapper());
returned.setDefaultEncoding("UTF-8");
returned.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
returned.setIncompatibleEnhancements("2.3.20");
return returned;
}

/**
* Allow to inject a template named
* @param injection
* @return
*/
@Produces Template produceTemplate(InjectionPoint injection, Configuration configuration) {
Class<?> beanClass = injection.getBean().getBeanClass();
configuration.setClassForTemplateLoading(beanClass, "/templates/"+beanClass.getSimpleName());
String templateName = String.format("%s.ftl", injection.getMember().getName());
try {
return configuration.getTemplate(templateName);
} catch (IOException e) {
throw new CantLoadTemplate(String.format("Cant't load template %s", templateName), e);
}
}
}
5 changes: 5 additions & 0 deletions freemarker-cdi-producer/src/main/resources/META-INF/beans.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.1" bean-discovery-mode="all">
</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.ndx.aadarchi.freemarker;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.assertj.core.api.Assertions;
import org.jboss.weld.junit5.EnableWeld;
import org.jboss.weld.junit5.WeldInitiator;
import org.jboss.weld.junit5.WeldSetup;
import org.junit.jupiter.api.Test;

import freemarker.template.Template;

@EnableWeld
public class FreemarkerCDIProducerTest {

@WeldSetup
public WeldInitiator weld = WeldInitiator.performDefaultDiscovery();

@Inject Template template;

// @Test
public void can_inject_freemarker_template() {
Assertions.assertThat(template).isNotNull();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Un texte
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public class MavenDetailsInfererEnhancerTest {
;
Assertions.assertThat(sipocDiagramGenerator.getTechnology())
.isNotBlank()
.isEqualTo("Java");
.contains("Java");
Container springComponentDetector = system.getContainerWithName("spring-components-detector");
Assertions.assertThat(springComponentDetector)
.isNotNull()
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<module>cdi-in-maven-plugin-helper</module>
<module>structurizr-components-detector</module>
<module>aadarchi-test-utils</module>
<module>freemarker-cdi-producer</module>
</modules>
<dependencyManagement>
<dependencies>
Expand Down

0 comments on commit bfb4be7

Please sign in to comment.