Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: hessian deserialize support sofa.serialize.dynamic.load.enable #1482

Merged
merged 1 commit into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import com.caucho.hessian.io.Serializer;
import com.caucho.hessian.io.SerializerFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static com.alipay.hessian.generic.io.GenericDeserializer.ARRAY_PREFIX;
import static com.alipay.sofa.rpc.codec.sofahessian.serialize.GenericCustomThrowableDeterminer.isGenericThrowException;

Expand All @@ -40,8 +43,14 @@ public class SingleClassLoaderSofaSerializerFactory extends SerializerFactory {
/**
* logger for this class
*/
private static final Logger LOGGER = LoggerFactory
.getLogger(SingleClassLoaderSofaSerializerFactory.class);
private static final Logger LOGGER = LoggerFactory
.getLogger(SingleClassLoaderSofaSerializerFactory.class);

private final Map<ClassLoader, Map<String, Object>> _typeNotFoundMap = new ConcurrentHashMap<>(8);
private static final Object NOT_FOUND = new Object();
private final boolean dynamicLoadEnable = Boolean.parseBoolean(System.getProperty(
DYNAMIC_LOAD_ENABLE_KEY,
Boolean.FALSE.toString()));

@Override
protected Serializer getDefaultSerializer(Class cl) {
Expand Down Expand Up @@ -73,14 +82,24 @@ public Deserializer getDeserializer(String type) throws HessianProtocolException
Deserializer subDeserializer = getDeserializer(type.substring(1));
deserializer = new ArrayDeserializer(subDeserializer);
} else {
ClassLoader appClassLoader = Thread.currentThread().getContextClassLoader();
try {
ClassLoader appClassLoader = Thread.currentThread().getContextClassLoader();
if (!dynamicLoadEnable) {
Map<String, Object> typeMap = _typeNotFoundMap.get(appClassLoader);
if (typeMap != null) {
if (typeMap.containsKey(type)) {
return null;
}
}
}
Class<?> cl = Class.forName(type, true, appClassLoader);
deserializer = getDeserializer(cl);
} catch (Exception e) {
if (e instanceof ClassNotFoundException) {
LOGGER.errorWithApp(null, LogCodes.getLog(LogCodes.ERROR_DECODE_CLASS_NOT_FOUND,
getClass().getName(), type, Thread.currentThread().getContextClassLoader()));
if (!dynamicLoadEnable) {
_typeNotFoundMap.computeIfAbsent(appClassLoader, k -> new ConcurrentHashMap<>(8)).put(type, NOT_FOUND);
}
LOGGER.errorWithApp(null, LogCodes.getLog(LogCodes.ERROR_DECODE_CLASS_NOT_FOUND, getClass().getName(), type, appClassLoader));
} else {
LOGGER.errorWithApp(null, e.toString(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@
*/
package com.alipay.sofa.rpc.codec.sofahessian;

import com.caucho.hessian.io.Deserializer;
import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import com.caucho.hessian.io.SerializerFactory;
import org.junit.Assert;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -63,4 +67,35 @@ public void testAll() throws IOException {
assertEquals(a1[i], a2[i]);
}

@Test
public void testDynamicLoadDisabled() throws Exception {
SingleClassLoaderSofaSerializerFactory factory = new SingleClassLoaderSofaSerializerFactory();
Field field = SingleClassLoaderSofaSerializerFactory.class.getDeclaredField("_typeNotFoundMap");
field.setAccessible(true);
Map<ClassLoader, Map<String, Object>> _typeNotFoundMap = (Map<ClassLoader, Map<String, Object>>) field
.get(factory);
Assert.assertEquals(0, _typeNotFoundMap.size());
Deserializer deserializer = factory.getDeserializer("mock.xxx.MockObject");
Assert.assertNull(deserializer);
Assert.assertEquals(1, _typeNotFoundMap.size());
}

@Test
public void testDynamicLoadEnabled() throws Exception {
try {
System.setProperty(SerializerFactory.DYNAMIC_LOAD_ENABLE_KEY, "true");
SingleClassLoaderSofaSerializerFactory factory = new SingleClassLoaderSofaSerializerFactory();
Field field = SingleClassLoaderSofaSerializerFactory.class.getDeclaredField("_typeNotFoundMap");
field.setAccessible(true);
Map<ClassLoader, Map<String, Object>> _typeNotFoundMap = (Map<ClassLoader, Map<String, Object>>) field
.get(factory);
Assert.assertEquals(0, _typeNotFoundMap.size());
Deserializer deserializer = factory.getDeserializer("mock.xxx.MockObject");
Assert.assertNull(deserializer);
Assert.assertEquals(0, _typeNotFoundMap.size());
} finally {
System.clearProperty(SerializerFactory.DYNAMIC_LOAD_ENABLE_KEY);
}
}

}
Loading