-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Jandex based registry for serialized data types
- Loading branch information
1 parent
0a7cfb7
commit e1fbf23
Showing
12 changed files
with
193 additions
and
4 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
101 changes: 101 additions & 0 deletions
101
api/src/main/java/org/fuin/esc/api/JandexSerializedDataTypeRegistry.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,101 @@ | ||
package org.fuin.esc.api; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
import org.fuin.utils4j.JandexIndexFileReader; | ||
import org.fuin.utils4j.JandexUtils; | ||
import org.jboss.jandex.*; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
import java.lang.reflect.Modifier; | ||
import java.util.*; | ||
|
||
/** | ||
* Registry that is built up by scanning for classes that are annotated with {@link HasSerializedDataTypeConstant}. | ||
* Inner classes are ignored. | ||
*/ | ||
public class JandexSerializedDataTypeRegistry implements SerializedDataTypeRegistry { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(JandexSerializedDataTypeRegistry.class); | ||
|
||
private final SimpleSerializedDataTypeRegistry delegate; | ||
|
||
private final List<File> classesDirs; | ||
|
||
private final List<Class<?>> classes; | ||
|
||
/** | ||
* Default constructor. | ||
*/ | ||
public JandexSerializedDataTypeRegistry() { | ||
this(new File("target/classes")); | ||
} | ||
|
||
/** | ||
* Constructor with classes directories. Most likely only used in tests. | ||
* | ||
* @param classesDirs Directories with class files. | ||
*/ | ||
public JandexSerializedDataTypeRegistry(final File... classesDirs) { | ||
delegate = new SimpleSerializedDataTypeRegistry(); | ||
this.classesDirs = Arrays.asList(classesDirs); | ||
classes = scanForClasses(); | ||
for (final Class<?> domainEventClass : classes) { | ||
delegate.add(serializedDataTypeConstant(domainEventClass), domainEventClass); | ||
} | ||
} | ||
|
||
@Override | ||
@NotNull | ||
public Class<?> findClass(@NotNull SerializedDataType type) { | ||
return delegate.findClass(type); | ||
} | ||
|
||
/** | ||
* Returns a list of known classes that can be serialized. | ||
* | ||
* @return SerializedDataType classes. | ||
*/ | ||
public List<Class<?>> getClasses() { | ||
return Collections.unmodifiableList(classes); | ||
} | ||
|
||
private List<Class<?>> scanForClasses() { | ||
final List<IndexView> indexes = new ArrayList<>(); | ||
indexes.add(new JandexIndexFileReader.Builder().addDefaultResource().build().loadR()); | ||
indexes.add(indexClassesDirs()); | ||
return findClasses(CompositeIndex.create(indexes)); | ||
} | ||
|
||
private IndexView indexClassesDirs() { | ||
final Indexer indexer = new Indexer(); | ||
final List<File> knownClassFiles = new ArrayList<>(); | ||
for (final File classesDir : classesDirs) { | ||
JandexUtils.indexDir(indexer, knownClassFiles, classesDir); | ||
} | ||
return indexer.complete(); | ||
} | ||
|
||
private static List<Class<?>> findClasses(final IndexView index) { | ||
List<Class<?>> classes = new ArrayList<>(); | ||
final Collection<AnnotationInstance> annotationInstances = index.getAnnotations(DotName.createSimple(HasSerializedDataTypeConstant.class)); | ||
for (final AnnotationInstance annotationInstance : annotationInstances) { | ||
final ClassInfo classInfo = annotationInstance.target().asClass(); | ||
if (!Modifier.isAbstract(classInfo.flags()) | ||
&& !Modifier.isInterface(classInfo.flags()) | ||
&& !classInfo.name().toString().contains("$")) { | ||
final Class<?> clasz = JandexUtils.loadClass(classInfo.name()); | ||
classes.add(clasz); | ||
LOG.info("Added SerializedDataType to {}: {}", JandexSerializedDataTypeRegistry.class.getSimpleName(), clasz.getName()); | ||
} | ||
} | ||
return classes; | ||
} | ||
|
||
public SerializedDataType serializedDataTypeConstant(Class<?> domainEventClass) { | ||
final HasSerializedDataTypeConstant annotation = domainEventClass.getAnnotation(HasSerializedDataTypeConstant.class); | ||
return HasSerializedDataTypeConstantValidator.extractValue(domainEventClass, annotation.value()); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
api/src/test/java/org/fuin/esc/api/JandexSerializedDataTypeRegistryTest.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,26 @@ | ||
package org.fuin.esc.api; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.File; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Test for the {@link JandexSerializedDataTypeRegistry} class. | ||
*/ | ||
public class JandexSerializedDataTypeRegistryTest { | ||
|
||
@Test | ||
public void testCreate() { | ||
final JandexSerializedDataTypeRegistry testee = new JandexSerializedDataTypeRegistry(new File("target/test-classes")); | ||
assertThat(testee.getClasses()).containsOnly(MyEvent.class); | ||
} | ||
|
||
@Test | ||
public void testFind() { | ||
final JandexSerializedDataTypeRegistry testee = new JandexSerializedDataTypeRegistry(new File("target/test-classes")); | ||
assertThat(testee.findClass(MyEvent.SER_TYPE)).isEqualTo(MyEvent.class); | ||
} | ||
|
||
} |
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,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<configuration> | ||
|
||
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> | ||
<Pattern>%d{yyyy.dd.MM HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="info"> | ||
<appender-ref ref="CONSOLE" /> | ||
</root> | ||
|
||
<!-- | ||
<logger name="org.fuin.esc" level="DEBUG"/> | ||
--> | ||
|
||
</configuration> |
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
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
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
26 changes: 26 additions & 0 deletions
26
spi/src/test/java/org/fuin/esc/spi/JandexSerializedDataTypeRegistryTest.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,26 @@ | ||
package org.fuin.esc.spi; | ||
|
||
import org.fuin.esc.api.JandexSerializedDataTypeRegistry; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.File; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Test for the {@link JandexSerializedDataTypeRegistry} class. | ||
*/ | ||
public class JandexSerializedDataTypeRegistryTest { | ||
|
||
@Test | ||
public void testBaseDataTypes() { | ||
final JandexSerializedDataTypeRegistry testee = new JandexSerializedDataTypeRegistry(); | ||
assertThat(testee.getClasses()).containsOnly( | ||
Base64Data.class, | ||
EscEvent.class, | ||
EscEvents.class, | ||
EscMeta.class | ||
); | ||
} | ||
|
||
} |