Skip to content

Commit

Permalink
Fix #9 to add config to register classes
Browse files Browse the repository at this point in the history
  • Loading branch information
zhfeng committed Nov 5, 2024
1 parent 4bcb233 commit 51b16d9
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkiverse.fury.deployment;

import java.util.List;
import java.util.Optional;

import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.DotName;
Expand Down Expand Up @@ -46,11 +47,22 @@ void unremovableBeans(

@BuildStep
@Record(ExecutionTime.STATIC_INIT)
public void registerClasses(
public void registerClasses(FuryBuildTimeConfig configs,
FuryBuildItem fury, List<FurySerializerBuildItem> classes, FuryRecorder recorder) {
for (FurySerializerBuildItem item : classes) {
recorder.registerClass(fury.getFury(), item.getClazz(), item.getClassId(), item.getSerializer());
}

if (configs.registerClassNames().isPresent()) {
for (String name : configs.registerClassNames().get().split(",")) {
recorder.registerClassByName(fury.getFury(), name, -1, Optional.empty());
}
}

for (var config : configs.registerClasses().entrySet()) {
recorder.registerClassByName(fury.getFury(), config.getKey(), config.getValue().classId(),
config.getValue().serializer());
}
}

@BuildStep
Expand Down
56 changes: 56 additions & 0 deletions docs/modules/ROOT/pages/includes/quarkus-fury.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,61 @@ endif::add-copy-button-to-env-var[]
|boolean
|`true`

h|[[quarkus-fury_section_quarkus-fury-register-classes]] [.section-name.section-level0]##link:#quarkus-fury_section_quarkus-fury-register-classes[Configurations]##
h|Type
h|Default

a|icon:lock[title=Fixed at build time] [[quarkus-fury_quarkus-fury-register-classes-configuration-name-class-id]] [.property-path]##link:#quarkus-fury_quarkus-fury-register-classes-configuration-name-class-id[`quarkus.fury.register-classes."configuration-name".class-id`]##

[.description]
--
Class id must be greater or equal to 256, and it must be different between classes. The default is -1.


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_FURY_REGISTER_CLASSES__CONFIGURATION_NAME__CLASS_ID+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_FURY_REGISTER_CLASSES__CONFIGURATION_NAME__CLASS_ID+++`
endif::add-copy-button-to-env-var[]
--
|int
|`-1`

a|icon:lock[title=Fixed at build time] [[quarkus-fury_quarkus-fury-register-classes-configuration-name-serializer]] [.property-path]##link:#quarkus-fury_quarkus-fury-register-classes-configuration-name-serializer[`quarkus.fury.register-classes."configuration-name".serializer`]##

[.description]
--
Specify a customized serializer for current class. This should be empty to let Fury create serializer for current class. But if users want to customize serialization for this class, one can provide serializer here.


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_FURY_REGISTER_CLASSES__CONFIGURATION_NAME__SERIALIZER+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_FURY_REGISTER_CLASSES__CONFIGURATION_NAME__SERIALIZER+++`
endif::add-copy-button-to-env-var[]
--
|string
|


a|icon:lock[title=Fixed at build time] [[quarkus-fury_quarkus-fury-register-class-names]] [.property-path]##link:#quarkus-fury_quarkus-fury-register-class-names[`quarkus.fury.register-class-names`]##

[.description]
--
Names of classes to register.


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_FURY_REGISTER_CLASS_NAMES+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_FURY_REGISTER_CLASS_NAMES+++`
endif::add-copy-button-to-env-var[]
--
|string
|

|===

56 changes: 56 additions & 0 deletions docs/modules/ROOT/pages/includes/quarkus-fury_quarkus.fury.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,61 @@ endif::add-copy-button-to-env-var[]
|boolean
|`true`

h|[[quarkus-fury_section_quarkus-fury-register-classes]] [.section-name.section-level0]##link:#quarkus-fury_section_quarkus-fury-register-classes[Configurations]##
h|Type
h|Default

a|icon:lock[title=Fixed at build time] [[quarkus-fury_quarkus-fury-register-classes-configuration-name-class-id]] [.property-path]##link:#quarkus-fury_quarkus-fury-register-classes-configuration-name-class-id[`quarkus.fury.register-classes."configuration-name".class-id`]##

[.description]
--
Class id must be greater or equal to 256, and it must be different between classes. The default is -1.


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_FURY_REGISTER_CLASSES__CONFIGURATION_NAME__CLASS_ID+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_FURY_REGISTER_CLASSES__CONFIGURATION_NAME__CLASS_ID+++`
endif::add-copy-button-to-env-var[]
--
|int
|`-1`

a|icon:lock[title=Fixed at build time] [[quarkus-fury_quarkus-fury-register-classes-configuration-name-serializer]] [.property-path]##link:#quarkus-fury_quarkus-fury-register-classes-configuration-name-serializer[`quarkus.fury.register-classes."configuration-name".serializer`]##

[.description]
--
Specify a customized serializer for current class. This should be empty to let Fury create serializer for current class. But if users want to customize serialization for this class, one can provide serializer here.


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_FURY_REGISTER_CLASSES__CONFIGURATION_NAME__SERIALIZER+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_FURY_REGISTER_CLASSES__CONFIGURATION_NAME__SERIALIZER+++`
endif::add-copy-button-to-env-var[]
--
|string
|


a|icon:lock[title=Fixed at build time] [[quarkus-fury_quarkus-fury-register-class-names]] [.property-path]##link:#quarkus-fury_quarkus-fury-register-class-names[`quarkus.fury.register-class-names`]##

[.description]
--
Names of classes to register.


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_FURY_REGISTER_CLASS_NAMES+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_FURY_REGISTER_CLASS_NAMES+++`
endif::add-copy-button-to-env-var[]
--
|string
|

|===

Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,5 @@
import java.util.List;
import java.util.Map;

import io.quarkiverse.fury.FurySerialization;

@FurySerialization(classId = 300)
public record Foo(int f1, String f2, List<String> f3, Map<String, Long> f4) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
import java.io.Serializable;
import java.util.Objects;

import io.quarkiverse.fury.FurySerialization;

@FurySerialization
public class Struct implements Serializable {
public int f1;
public long f2;
Expand Down
2 changes: 2 additions & 0 deletions integration-tests/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
quarkus.fury.register-class-names=io.quarkiverse.fury.it.Struct
quarkus.fury.register-classes."io.quarkiverse.fury.it.Foo".class-id=300
17 changes: 17 additions & 0 deletions runtime/src/main/java/io/quarkiverse/fury/FuryBuildTimeConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package io.quarkiverse.fury;

import java.util.Map;
import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigDocMapKey;
import io.quarkus.runtime.annotations.ConfigDocSection;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
Expand All @@ -15,4 +20,16 @@ public interface FuryBuildTimeConfig {
/** Whether to use thread safe fury. The default is true. */
@WithDefault("true")
boolean threadSafe();

/**
* Configurations
*/
@ConfigDocSection
@ConfigDocMapKey("configuration-name")
Map<String, FuryRegisterClassConfig> registerClasses();

/**
* Names of classes to register.
*/
Optional<String> registerClassNames();
}
24 changes: 24 additions & 0 deletions runtime/src/main/java/io/quarkiverse/fury/FuryRecorder.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package io.quarkiverse.fury;

import java.util.Optional;

import org.apache.fury.BaseFury;
import org.apache.fury.Fury;
import org.apache.fury.ThreadSafeFury;
import org.apache.fury.config.FuryBuilder;
import org.apache.fury.resolver.ClassResolver;
import org.apache.fury.serializer.Serializer;
import org.apache.fury.util.Preconditions;
import org.jboss.logging.Logger;

import io.quarkus.arc.runtime.BeanContainer;
import io.quarkus.runtime.RuntimeValue;
import io.quarkus.runtime.annotations.Recorder;

@Recorder
public class FuryRecorder {
private static final Logger LOG = Logger.getLogger(FuryRecorder.class);

public RuntimeValue<BaseFury> createFury(
final FuryBuildTimeConfig config, final BeanContainer beanContainer) {
// create the Fury instance from the config
Expand All @@ -26,6 +31,25 @@ public RuntimeValue<BaseFury> createFury(
return new RuntimeValue<>(fury);
}

public void registerClassByName(final RuntimeValue<BaseFury> furyValue, final String className, final int classId,
final Optional<String> serializerClassName) {
try {
Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
Class<? extends Serializer> serializer = null;
if (serializerClassName.isPresent()) {
serializer = (Class<? extends Serializer>) Thread.currentThread().getContextClassLoader()
.loadClass(serializerClassName.get());
}
registerClass(furyValue, clazz, classId, serializer);
} catch (ClassNotFoundException e) {
LOG.warn("can not find register class: " + className
+ (serializerClassName.isPresent()
? " with serializer: " + serializerClassName.get()
: ""));
throw new RuntimeException(e);
}
}

public void registerClass(
final RuntimeValue<BaseFury> furyValue, final Class<?> clazz,
final int classId, Class<? extends Serializer> serializer) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkiverse.fury;

import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigGroup;
import io.smallrye.config.WithDefault;

@ConfigGroup
public interface FuryRegisterClassConfig {
/**
* Class id must be greater or equal to 256, and it must be different between classes.
* The default is -1.
*/
@WithDefault("-1")
Integer classId();

/**
* Specify a customized serializer for current class.
* This should be empty to let Fury create serializer for current class. But if
* users want to customize serialization for this class, one can provide serializer here.
*/
Optional<String> serializer();

}

0 comments on commit 51b16d9

Please sign in to comment.