Skip to content
Permalink
Browse files
fix frequent class loading for classes which are unknown to current c…
…lassloader
  • Loading branch information
lizhong committed Jan 30, 2019
1 parent f7dd9bf commit 2052f9647a7c95c9551518e4bbbdbd56791e9544
Showing 2 changed files with 23 additions and 11 deletions.
@@ -70,15 +70,7 @@
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -231,6 +223,12 @@ public class SerializerFactory extends AbstractSerializerFactory {
private ConcurrentHashMap _cachedDeserializerMap;
private ConcurrentHashMap _cachedTypeDeserializerMap;
private boolean _isAllowNonSerializable;
/**
* For those classes are unknown in current classloader, record them in this set to avoid
* frequently class loading and to reduce performance overhead.
*/
private Map<String, Object> _typeNotFoundDeserializerMap = new ConcurrentHashMap<>(8);
private static final Object PRESENT = new Object();

public SerializerFactory() {
this(Thread.currentThread().getContextClassLoader());
@@ -617,7 +615,7 @@ else if (_arrayListDeserializer != null)
*/
public Deserializer getDeserializer(String type)
throws HessianProtocolException {
if (type == null || type.equals(""))
if (type == null || type.equals("") || _typeNotFoundDeserializerMap.containsKey(type))
return null;

Deserializer deserializer;
@@ -647,7 +645,7 @@ public Deserializer getDeserializer(String type)
deserializer = getDeserializer(cl);
} catch (Exception e) {
log.warning("Hessian/Burlap: '" + type + "' is an unknown class in " + _loader + ":\n" + e);

_typeNotFoundDeserializerMap.put(type, PRESENT);
log.log(Level.FINER, e.toString(), e);
}
}
@@ -99,4 +99,18 @@ public void run() {
countDownLatch.await();
}

@Test
public void getDeserializerByType() throws Exception {
final SerializerFactory serializerFactory = new SerializerFactory();

final String testClassName = TestClass.class.getName();
Deserializer d1 = serializerFactory.getDeserializer(testClassName);
Assert.assertTrue("TestClass Deserializer!", d1 != null);

Deserializer d2 = serializerFactory.getDeserializer("com.test.NotExistClass");
Assert.assertTrue("NotExistClass Deserializer!", d2 == null);
//again check NotExistClass, there should be no warning like Hessian/Burlap:.....
Deserializer d3 = serializerFactory.getDeserializer("com.test.NotExistClass");
Assert.assertTrue("NotExistClass Deserializer!", d3 == null);
}
}

0 comments on commit 2052f96

Please sign in to comment.