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 #1463

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 2 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 @@ -43,6 +46,13 @@ public class SingleClassLoaderSofaSerializerFactory extends SerializerFactory {
private static final Logger LOGGER = LoggerFactory
.getLogger(SingleClassLoaderSofaSerializerFactory.class);

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

@Override
protected Serializer getDefaultSerializer(Class cl) {
if (_defaultSerializer != null) {
Expand Down Expand Up @@ -73,12 +83,23 @@ 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) {
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, Thread.currentThread().getContextClassLoader()));
} else {
Expand Down
Loading