Skip to content

Commit

Permalink
fix: hessian deserialize support sofa.serialize.dynamic.load.enable
Browse files Browse the repository at this point in the history
  • Loading branch information
Just-CJ committed Dec 22, 2024
1 parent be93251 commit 2b0af38
Showing 1 changed file with 32 additions and 1 deletion.
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,33 @@ 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) {
Map<String, Object> typeMap = _typeNotFoundMap.get(appClassLoader);
if (typeMap == null) {
synchronized (this) {
typeMap = _typeNotFoundMap.get(appClassLoader);
if (typeMap == null) {
_typeNotFoundMap.put(appClassLoader, new ConcurrentHashMap<String, Object>(8));
typeMap = _typeNotFoundMap.get(appClassLoader);
}
}
}
typeMap.put(type, NOT_FOUND);
}
LOGGER.errorWithApp(null, LogCodes.getLog(LogCodes.ERROR_DECODE_CLASS_NOT_FOUND,
getClass().getName(), type, Thread.currentThread().getContextClassLoader()));
} else {
Expand Down

0 comments on commit 2b0af38

Please sign in to comment.