-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
127 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
92 changes: 45 additions & 47 deletions
92
...i/tickets/FreemarkerTemplateProducer.java → ...reemarker/FreemarkerTemplateProducer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
5
freemarker-cdi-producer/src/main/resources/META-INF/beans.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
27 changes: 27 additions & 0 deletions
27
...ker-cdi-producer/src/test/java/org/ndx/aadarchi/freemarker/FreemarkerCDIProducerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
freemarker-cdi-producer/src/test/resources/templates/FreemarkerCDIProducerTest/template.ftl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Un texte |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters