Skip to content

Commit

Permalink
performance tuning
Browse files Browse the repository at this point in the history
  • Loading branch information
mariofusco committed Jul 30, 2024
1 parent fb388f8 commit a308b7c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ResteasyReactiveServerJacksonRecorder {
private static final Map<String, Class<?>> customSerializationMap = new HashMap<>();
private static final Map<String, Class<?>> customDeserializationMap = new HashMap<>();

private static final Set<Class<? extends StdSerializer>> generatedSerializer = new HashSet<>();
private static final Set<Class<? extends StdSerializer>> generatedSerializers = new HashSet<>();

/* STATIC INIT */
public RuntimeValue<Map<String, Supplier<String[]>>> createConfigExpToAllowedRoles() {
Expand Down Expand Up @@ -82,7 +82,7 @@ public void recordCustomDeserialization(String target, String className) {
}

public void recordGeneratedSerializer(String className) {
generatedSerializer.add((Class<? extends StdSerializer>) loadClass(className));
generatedSerializers.add((Class<? extends StdSerializer>) loadClass(className));
}

public void configureShutdown(ShutdownContext shutdownContext) {
Expand Down Expand Up @@ -125,8 +125,8 @@ public static Class<? extends BiFunction<ObjectMapper, Type, ObjectReader>> cust
return (Class<? extends BiFunction<ObjectMapper, Type, ObjectReader>>) customDeserializationMap.get(clazz.getName());
}

public static Set<Class<? extends StdSerializer>> getGeneratedSerializer() {
return generatedSerializer;
public static Set<Class<? extends StdSerializer>> getGeneratedSerializers() {
return generatedSerializers;
}

private Class<?> loadClass(String className) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.quarkus.resteasy.reactive.jackson.runtime.mappers;

import java.util.concurrent.locks.ReentrantLock;

import io.quarkus.arc.Arc;
import io.quarkus.arc.ArcContainer;
import io.quarkus.arc.InstanceHandle;
Expand All @@ -11,19 +9,12 @@
public class JacksonMapperUtil {

public static boolean includeSecureField(String[] rolesAllowed) {
ArcContainer container = Arc.container();
if (container == null) {
return false;
}

InstanceHandle<SecurityIdentity> instance = container.instance(SecurityIdentity.class);
if (!instance.isAvailable()) {
SecurityIdentity securityIdentity = RolesAllowedHolder.SECURITY_IDENTITY;
if (securityIdentity == null) {
return false;
}

SecurityIdentity securityIdentity = instance.get();
RolesAllowedConfigExpStorage rolesConfigExpStorage = RolesAllowedHolder.INSTANCE
.getRolesAllowedConfigExpStorage(container);
RolesAllowedConfigExpStorage rolesConfigExpStorage = RolesAllowedHolder.ROLES_ALLOWED_CONFIG_EXP_STORAGE;
for (String role : rolesAllowed) {
if (rolesConfigExpStorage != null) {
// role config expression => resolved roles
Expand All @@ -47,24 +38,26 @@ public static boolean includeSecureField(String[] rolesAllowed) {

private static class RolesAllowedHolder {

private static final RolesAllowedHolder INSTANCE = new RolesAllowedHolder();
private static final ArcContainer ARC_CONTAINER = Arc.container();

private volatile InstanceHandle<RolesAllowedConfigExpStorage> rolesAllowedConfigExpStorage;
private static final SecurityIdentity SECURITY_IDENTITY = createSecurityIdentity();

private final ReentrantLock lock = new ReentrantLock();
private static final RolesAllowedConfigExpStorage ROLES_ALLOWED_CONFIG_EXP_STORAGE = createRolesAllowedConfigExpStorage();

private RolesAllowedConfigExpStorage getRolesAllowedConfigExpStorage(ArcContainer container) {
if (rolesAllowedConfigExpStorage == null) {
lock.lock();
try {
if (rolesAllowedConfigExpStorage == null) {
rolesAllowedConfigExpStorage = container.instance(RolesAllowedConfigExpStorage.class);
}
} finally {
lock.unlock();
}
private static SecurityIdentity createSecurityIdentity() {
if (ARC_CONTAINER == null) {
return null;
}
InstanceHandle<SecurityIdentity> instance = ARC_CONTAINER.instance(SecurityIdentity.class);
return instance.isAvailable() ? instance.get() : null;
}

private static RolesAllowedConfigExpStorage createRolesAllowedConfigExpStorage() {
if (ARC_CONTAINER == null) {
return null;
}
InstanceHandle<RolesAllowedConfigExpStorage> rolesAllowedConfigExpStorage = ARC_CONTAINER
.instance(RolesAllowedConfigExpStorage.class);
return rolesAllowedConfigExpStorage.isAvailable() ? rolesAllowedConfigExpStorage.get() : null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,26 @@ public class BasicServerJacksonMessageBodyWriter extends ServerMessageBodyWriter

@Inject
public BasicServerJacksonMessageBodyWriter(ObjectMapper mapper) {
registerGeneratedSerializers(mapper);
mapper.registerModule(MappingModuleHolder.mappingModule);
this.defaultWriter = createDefaultWriter(mapper);
}

static void registerGeneratedSerializers(ObjectMapper mapper) {
if (ResteasyReactiveServerJacksonRecorder.getGeneratedSerializer().isEmpty()) {
return;
}
SimpleModule module = new SimpleModule();
for (Class<? extends StdSerializer> serClass : ResteasyReactiveServerJacksonRecorder.getGeneratedSerializer()) {
try {
StdSerializer serializer = serClass.getConstructor().newInstance();
module.addSerializer(serializer.handledType(), serializer);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException(e);
static class MappingModuleHolder {
static final SimpleModule mappingModule = createMappingModule();

private static SimpleModule createMappingModule() {
SimpleModule module = new SimpleModule();
for (Class<? extends StdSerializer> serClass : ResteasyReactiveServerJacksonRecorder.getGeneratedSerializers()) {
try {
StdSerializer serializer = serClass.getConstructor().newInstance();
module.addSerializer(serializer.handledType(), serializer);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException
| NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
return module;
}
mapper.registerModule(module);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.quarkus.resteasy.reactive.jackson.runtime.serialisers;

import static io.quarkus.resteasy.reactive.jackson.runtime.serialisers.BasicServerJacksonMessageBodyWriter.registerGeneratedSerializers;
import static org.jboss.resteasy.reactive.server.jackson.JacksonMessageBodyWriterUtil.createDefaultWriter;
import static org.jboss.resteasy.reactive.server.jackson.JacksonMessageBodyWriterUtil.doLegacyWrite;
import static org.jboss.resteasy.reactive.server.jackson.JacksonMessageBodyWriterUtil.setNecessaryJsonFactoryConfig;
Expand Down Expand Up @@ -57,7 +56,7 @@ public void writeResponse(Object o, Type genericType, ServerRequestContext conte
stream.write(((String) o).getBytes(StandardCharsets.UTF_8));
} else {
ObjectMapper effectiveMapper = getEffectiveMapper(o, context);
registerGeneratedSerializers(effectiveMapper);
effectiveMapper.registerModule(BasicServerJacksonMessageBodyWriter.MappingModuleHolder.mappingModule);
ObjectWriter effectiveWriter = getEffectiveWriter(effectiveMapper);
ResteasyReactiveResourceInfo resourceInfo = context.getResteasyReactiveResourceInfo();
if (resourceInfo != null) {
Expand Down

0 comments on commit a308b7c

Please sign in to comment.