[3.3] Update hessian-lite to 4.0.0 & Set hessian serialization back (#13974)
This commit is contained in:
parent
80ffcd7a85
commit
225ca9d232
|
|
@ -199,7 +199,6 @@ jobs:
|
|||
env:
|
||||
DISABLE_FILE_SYSTEM_TEST: true
|
||||
CURRENT_ROLE: ${{ matrix.case-role }}
|
||||
DUBBO_DEFAULT_SERIALIZATION: fastjson2
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import org.apache.dubbo.common.beans.factory.ScopeBeanFactory;
|
|||
import org.apache.dubbo.common.config.ConfigurationCache;
|
||||
import org.apache.dubbo.common.convert.ConverterUtil;
|
||||
import org.apache.dubbo.common.lang.ShutdownHookCallbacks;
|
||||
import org.apache.dubbo.common.serialization.ClassHolder;
|
||||
import org.apache.dubbo.common.ssl.CertManager;
|
||||
import org.apache.dubbo.common.status.reporter.FrameworkStatusReportService;
|
||||
import org.apache.dubbo.common.threadpool.manager.FrameworkExecutorRepository;
|
||||
|
|
@ -40,6 +41,7 @@ public class CommonScopeModelInitializer implements ScopeModelInitializer {
|
|||
beanFactory.registerBean(SerializeSecurityManager.class);
|
||||
beanFactory.registerBean(DefaultSerializeClassChecker.class);
|
||||
beanFactory.registerBean(CertManager.class);
|
||||
beanFactory.registerBean(ClassHolder.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.dubbo.common.serialization;
|
||||
|
||||
import org.apache.dubbo.common.utils.ConcurrentHashSet;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class ClassHolder {
|
||||
private final Map<String, Set<Class<?>>> classCache = new ConcurrentHashMap<>();
|
||||
|
||||
public void storeClass(Class<?> clazz) {
|
||||
classCache
|
||||
.computeIfAbsent(clazz.getName(), k -> new ConcurrentHashSet<>())
|
||||
.add(clazz);
|
||||
}
|
||||
|
||||
public Class<?> loadClass(String className, ClassLoader classLoader) {
|
||||
Set<Class<?>> classList = classCache.get(className);
|
||||
if (classList == null) {
|
||||
return null;
|
||||
}
|
||||
for (Class<?> clazz : classList) {
|
||||
if (classLoader.equals(clazz.getClassLoader())) {
|
||||
return clazz;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -16,8 +16,10 @@
|
|||
*/
|
||||
package org.apache.dubbo.common.utils;
|
||||
|
||||
import org.apache.dubbo.common.aot.NativeDetector;
|
||||
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
|
||||
import org.apache.dubbo.common.logger.LoggerFactory;
|
||||
import org.apache.dubbo.common.serialization.ClassHolder;
|
||||
import org.apache.dubbo.rpc.model.FrameworkModel;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
|
@ -40,6 +42,7 @@ public class DefaultSerializeClassChecker implements AllowClassNotifyListener {
|
|||
private volatile boolean checkSerializable = true;
|
||||
|
||||
private final SerializeSecurityManager serializeSecurityManager;
|
||||
private final ClassHolder classHolder;
|
||||
private volatile long[] allowPrefixes = new long[0];
|
||||
|
||||
private volatile long[] disAllowPrefixes = new long[0];
|
||||
|
|
@ -47,6 +50,8 @@ public class DefaultSerializeClassChecker implements AllowClassNotifyListener {
|
|||
public DefaultSerializeClassChecker(FrameworkModel frameworkModel) {
|
||||
serializeSecurityManager = frameworkModel.getBeanFactory().getOrRegisterBean(SerializeSecurityManager.class);
|
||||
serializeSecurityManager.registerListener(this);
|
||||
classHolder =
|
||||
NativeDetector.inNativeImage() ? frameworkModel.getBeanFactory().getBean(ClassHolder.class) : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -120,7 +125,7 @@ public class DefaultSerializeClassChecker implements AllowClassNotifyListener {
|
|||
|
||||
private Class<?> loadClass0(ClassLoader classLoader, String className) throws ClassNotFoundException {
|
||||
if (checkStatus == SerializeCheckStatus.DISABLE) {
|
||||
return ClassUtils.forName(className, classLoader);
|
||||
return classForName(classLoader, className);
|
||||
}
|
||||
|
||||
long hash = MAGIC_HASH_CODE;
|
||||
|
|
@ -133,7 +138,7 @@ public class DefaultSerializeClassChecker implements AllowClassNotifyListener {
|
|||
hash *= MAGIC_PRIME;
|
||||
|
||||
if (Arrays.binarySearch(allowPrefixes, hash) >= 0) {
|
||||
return ClassUtils.forName(className, classLoader);
|
||||
return classForName(classLoader, className);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -190,7 +195,7 @@ public class DefaultSerializeClassChecker implements AllowClassNotifyListener {
|
|||
}
|
||||
}
|
||||
|
||||
Class<?> clazz = ClassUtils.forName(className, classLoader);
|
||||
Class<?> clazz = classForName(classLoader, className);
|
||||
if (serializeSecurityManager.getWarnedClasses().add(className)) {
|
||||
logger.warn(
|
||||
PROTOCOL_UNTRUSTED_SERIALIZE_CLASS,
|
||||
|
|
@ -204,6 +209,16 @@ public class DefaultSerializeClassChecker implements AllowClassNotifyListener {
|
|||
return clazz;
|
||||
}
|
||||
|
||||
private Class<?> classForName(ClassLoader classLoader, String className) throws ClassNotFoundException {
|
||||
if (classHolder != null) {
|
||||
Class<?> aClass = classHolder.loadClass(className, classLoader);
|
||||
if (aClass != null) {
|
||||
return aClass;
|
||||
}
|
||||
}
|
||||
return ClassUtils.forName(className, classLoader);
|
||||
}
|
||||
|
||||
public static DefaultSerializeClassChecker getInstance() {
|
||||
return FrameworkModel.defaultModel().getBeanFactory().getBean(DefaultSerializeClassChecker.class);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,9 +16,11 @@
|
|||
*/
|
||||
package org.apache.dubbo.common.utils;
|
||||
|
||||
import org.apache.dubbo.common.aot.NativeDetector;
|
||||
import org.apache.dubbo.common.constants.CommonConstants;
|
||||
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
|
||||
import org.apache.dubbo.common.logger.LoggerFactory;
|
||||
import org.apache.dubbo.common.serialization.ClassHolder;
|
||||
import org.apache.dubbo.config.ApplicationConfig;
|
||||
import org.apache.dubbo.rpc.model.FrameworkModel;
|
||||
import org.apache.dubbo.rpc.model.ModuleModel;
|
||||
|
|
@ -52,6 +54,8 @@ public class SerializeSecurityConfigurator implements ScopeClassLoaderListener<M
|
|||
|
||||
private final ModuleModel moduleModel;
|
||||
|
||||
private final ClassHolder classHolder;
|
||||
|
||||
private volatile boolean autoTrustSerializeClass = true;
|
||||
|
||||
private volatile int trustSerializeClassLevel = Integer.MAX_VALUE;
|
||||
|
|
@ -62,6 +66,8 @@ public class SerializeSecurityConfigurator implements ScopeClassLoaderListener<M
|
|||
|
||||
FrameworkModel frameworkModel = moduleModel.getApplicationModel().getFrameworkModel();
|
||||
serializeSecurityManager = frameworkModel.getBeanFactory().getBean(SerializeSecurityManager.class);
|
||||
classHolder =
|
||||
NativeDetector.inNativeImage() ? frameworkModel.getBeanFactory().getBean(ClassHolder.class) : null;
|
||||
|
||||
refreshStatus();
|
||||
refreshCheck();
|
||||
|
|
@ -210,7 +216,7 @@ public class SerializeSecurityConfigurator implements ScopeClassLoaderListener<M
|
|||
Set<Type> markedClass = new HashSet<>();
|
||||
checkClass(markedClass, clazz);
|
||||
|
||||
addToAllow(clazz.getName());
|
||||
addToAllow(clazz);
|
||||
|
||||
Method[] methodsToExport = clazz.getMethods();
|
||||
|
||||
|
|
@ -291,7 +297,7 @@ public class SerializeSecurityConfigurator implements ScopeClassLoaderListener<M
|
|||
return;
|
||||
}
|
||||
|
||||
addToAllow(clazz.getName());
|
||||
addToAllow(clazz);
|
||||
|
||||
if (ClassUtils.isSimpleType(clazz) || clazz.isPrimitive() || clazz.isArray()) {
|
||||
return;
|
||||
|
|
@ -337,11 +343,17 @@ public class SerializeSecurityConfigurator implements ScopeClassLoaderListener<M
|
|||
}
|
||||
}
|
||||
|
||||
private void addToAllow(String className) {
|
||||
private void addToAllow(Class<?> clazz) {
|
||||
if (classHolder != null) {
|
||||
classHolder.storeClass(clazz);
|
||||
}
|
||||
|
||||
String className = clazz.getName();
|
||||
// ignore jdk
|
||||
if (className.startsWith("java.")
|
||||
|| className.startsWith("javax.")
|
||||
|| className.startsWith("com.sun.")
|
||||
|| className.startsWith("jakarta.")
|
||||
|| className.startsWith("sun.")
|
||||
|| className.startsWith("jdk.")) {
|
||||
serializeSecurityManager.addToAllowed(className);
|
||||
|
|
|
|||
|
|
@ -481,7 +481,7 @@ class ConfigManagerTest {
|
|||
public static class TestPreferSerializationProvider implements PreferSerializationProvider {
|
||||
@Override
|
||||
public String getPreferSerialization() {
|
||||
return "fastjson2,hessian2";
|
||||
return "hessian2";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -389,7 +389,7 @@ class ProtocolConfigTest {
|
|||
assertNull(protocolConfig.getPreferSerialization());
|
||||
|
||||
protocolConfig.checkDefault();
|
||||
assertThat(protocolConfig.getPreferSerialization(), equalTo("fastjson2,hessian2"));
|
||||
assertThat(protocolConfig.getPreferSerialization(), equalTo("hessian2,fastjson2"));
|
||||
|
||||
protocolConfig = new ProtocolConfig();
|
||||
protocolConfig.setSerialization("x-serialization");
|
||||
|
|
@ -405,7 +405,7 @@ class ProtocolConfigTest {
|
|||
assertNull(protocolConfig.getPreferSerialization());
|
||||
|
||||
protocolConfig.refresh();
|
||||
assertThat(protocolConfig.getPreferSerialization(), equalTo("fastjson2,hessian2"));
|
||||
assertThat(protocolConfig.getPreferSerialization(), equalTo("hessian2,fastjson2"));
|
||||
|
||||
protocolConfig = new ProtocolConfig();
|
||||
protocolConfig.setSerialization("x-serialization");
|
||||
|
|
|
|||
|
|
@ -122,6 +122,11 @@
|
|||
<artifactId>dubbo-serialization-fastjson2</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
<artifactId>dubbo-serialization-hessian2</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
|
|
@ -134,11 +139,6 @@
|
|||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
<artifactId>dubbo-native</artifactId>
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ public class Application {
|
|||
public static void main(String[] args) {
|
||||
SystemPropertyConfigUtils.setSystemProperty(DubboProperty.DUBBO_APPLICATION_LOGGER, "logback");
|
||||
System.setProperty("native", "true");
|
||||
SystemPropertyConfigUtils.setSystemProperty(DubboProperty.DUBBO_PREFER_JSON_FRAMEWORK_NAME, "fastjson");
|
||||
SystemPropertyConfigUtils.setSystemProperty(DubboProperty.DUBBO_PREFER_JSON_FRAMEWORK_NAME, "fastjson2");
|
||||
runWithBootstrap();
|
||||
}
|
||||
|
||||
|
|
@ -55,7 +55,7 @@ public class Application {
|
|||
reference.setGeneric("false");
|
||||
|
||||
ProtocolConfig protocolConfig = new ProtocolConfig(CommonConstants.DUBBO, -1);
|
||||
protocolConfig.setSerialization("fastjson2");
|
||||
protocolConfig.setSerialization("hessian2");
|
||||
bootstrap
|
||||
.application(applicationConfig)
|
||||
.registry(new RegistryConfig(REGISTRY_URL))
|
||||
|
|
|
|||
|
|
@ -122,6 +122,11 @@
|
|||
<artifactId>dubbo-serialization-fastjson2</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
<artifactId>dubbo-serialization-hessian2</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
|
|
@ -133,10 +138,6 @@
|
|||
<artifactId>dubbo-filter-validation</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ public class Application {
|
|||
public static void main(String[] args) throws Exception {
|
||||
SystemPropertyConfigUtils.setSystemProperty(DubboProperty.DUBBO_APPLICATION_LOGGER, "logback");
|
||||
System.setProperty("native", "true");
|
||||
SystemPropertyConfigUtils.setSystemProperty(DubboProperty.DUBBO_PREFER_JSON_FRAMEWORK_NAME, "fastjson");
|
||||
SystemPropertyConfigUtils.setSystemProperty(DubboProperty.DUBBO_PREFER_JSON_FRAMEWORK_NAME, "fastjson2");
|
||||
startWithBootstrap();
|
||||
System.in.read();
|
||||
}
|
||||
|
|
@ -60,7 +60,7 @@ public class Application {
|
|||
service.setRef(new DemoServiceImpl());
|
||||
|
||||
ProtocolConfig protocolConfig = new ProtocolConfig(CommonConstants.DUBBO, -1);
|
||||
protocolConfig.setSerialization("fastjson2");
|
||||
protocolConfig.setSerialization("hessian2");
|
||||
bootstrap
|
||||
.application(applicationConfig)
|
||||
.registry(new RegistryConfig(REGISTRY_URL))
|
||||
|
|
|
|||
|
|
@ -167,7 +167,7 @@
|
|||
<jaxb_version>2.2.7</jaxb_version>
|
||||
<activation_version>1.2.0</activation_version>
|
||||
<test_container_version>1.19.7</test_container_version>
|
||||
<hessian_lite_version>3.2.13</hessian_lite_version>
|
||||
<hessian_lite_version>4.0.0</hessian_lite_version>
|
||||
<swagger_version>1.6.14</swagger_version>
|
||||
|
||||
<snappy_java_version>1.1.10.5</snappy_java_version>
|
||||
|
|
@ -364,7 +364,7 @@
|
|||
<version>${hessian_version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
<artifactId>hessian-lite</artifactId>
|
||||
<version>${hessian_lite_version}</version>
|
||||
</dependency>
|
||||
|
|
|
|||
|
|
@ -427,7 +427,7 @@
|
|||
<artifactId>snakeyaml</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
<artifactId>hessian-lite</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
|
|
|||
|
|
@ -456,7 +456,7 @@
|
|||
<artifactId>snakeyaml</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
<artifactId>hessian-lite</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
|
|
|||
|
|
@ -19,6 +19,9 @@ package org.apache.dubbo.aot.generate;
|
|||
import org.apache.dubbo.aot.api.JdkProxyDescriber;
|
||||
import org.apache.dubbo.aot.api.ProxyDescriberRegistrar;
|
||||
import org.apache.dubbo.aot.api.ReflectionTypeDescriberRegistrar;
|
||||
import org.apache.dubbo.aot.api.ResourceBundleDescriber;
|
||||
import org.apache.dubbo.aot.api.ResourceDescriberRegistrar;
|
||||
import org.apache.dubbo.aot.api.ResourcePatternDescriber;
|
||||
import org.apache.dubbo.aot.api.TypeDescriber;
|
||||
import org.apache.dubbo.common.constants.CommonConstants;
|
||||
import org.apache.dubbo.rpc.model.FrameworkModel;
|
||||
|
|
@ -46,6 +49,12 @@ public class AotProcessor {
|
|||
ResourceScanner.INSTANCE.distinctSpiResource().toArray(new String[] {}));
|
||||
resourceRepository.registerIncludesPatterns(
|
||||
ResourceScanner.INSTANCE.distinctSecurityResource().toArray(new String[] {}));
|
||||
for (ResourcePatternDescriber resourcePatternDescriber : getResourcePatternDescribers()) {
|
||||
resourceRepository.registerIncludesPattern(resourcePatternDescriber);
|
||||
}
|
||||
for (ResourceBundleDescriber resourceBundleDescriber : getResourceBundleDescribers()) {
|
||||
resourceRepository.registerBundles(resourceBundleDescriber);
|
||||
}
|
||||
writer.writeResourceConfig(resourceRepository);
|
||||
|
||||
ReflectConfigMetadataRepository reflectRepository = new ReflectConfigMetadataRepository();
|
||||
|
|
@ -89,6 +98,52 @@ public class AotProcessor {
|
|||
return typeDescribers;
|
||||
}
|
||||
|
||||
private static List<ResourcePatternDescriber> getResourcePatternDescribers() {
|
||||
List<ResourcePatternDescriber> resourcePatternDescribers = new ArrayList<>();
|
||||
FrameworkModel.defaultModel()
|
||||
.defaultApplication()
|
||||
.getExtensionLoader(ResourceDescriberRegistrar.class)
|
||||
.getSupportedExtensionInstances()
|
||||
.forEach(reflectionTypeDescriberRegistrar -> {
|
||||
List<ResourcePatternDescriber> describers = new ArrayList<>();
|
||||
try {
|
||||
describers = reflectionTypeDescriberRegistrar.getResourcePatternDescribers();
|
||||
} catch (Throwable e) {
|
||||
// The ResourceDescriberRegistrar implementation classes are shaded, causing some unused
|
||||
// classes to be loaded.
|
||||
// When loading a dependent class may appear that cannot be found, it does not affect.
|
||||
// ignore
|
||||
}
|
||||
|
||||
resourcePatternDescribers.addAll(describers);
|
||||
});
|
||||
|
||||
return resourcePatternDescribers;
|
||||
}
|
||||
|
||||
private static List<ResourceBundleDescriber> getResourceBundleDescribers() {
|
||||
List<ResourceBundleDescriber> resourceBundleDescribers = new ArrayList<>();
|
||||
FrameworkModel.defaultModel()
|
||||
.defaultApplication()
|
||||
.getExtensionLoader(ResourceDescriberRegistrar.class)
|
||||
.getSupportedExtensionInstances()
|
||||
.forEach(reflectionTypeDescriberRegistrar -> {
|
||||
List<ResourceBundleDescriber> describers = new ArrayList<>();
|
||||
try {
|
||||
describers = reflectionTypeDescriberRegistrar.getResourceBundleDescribers();
|
||||
} catch (Throwable e) {
|
||||
// The ResourceDescriberRegistrar implementation classes are shaded, causing some unused
|
||||
// classes to be loaded.
|
||||
// When loading a dependent class may appear that cannot be found, it does not affect.
|
||||
// ignore
|
||||
}
|
||||
|
||||
resourceBundleDescribers.addAll(describers);
|
||||
});
|
||||
|
||||
return resourceBundleDescribers;
|
||||
}
|
||||
|
||||
private static List<JdkProxyDescriber> getProxyDescribers() {
|
||||
List<JdkProxyDescriber> jdkProxyDescribers = new ArrayList<>();
|
||||
FrameworkModel.defaultModel()
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@
|
|||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
<artifactId>hessian-lite</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ public class PreferSerializationProviderImpl implements PreferSerializationProvi
|
|||
private final String preferSerialization;
|
||||
|
||||
public PreferSerializationProviderImpl(FrameworkModel frameworkModel) {
|
||||
List<String> defaultSerializations = Arrays.asList("fastjson2", "hessian2");
|
||||
List<String> defaultSerializations = Arrays.asList("hessian2", "fastjson2");
|
||||
this.preferSerialization = defaultSerializations.stream()
|
||||
.filter(s ->
|
||||
frameworkModel.getExtensionLoader(Serialization.class).hasExtension(s))
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ limitations under the License.
|
|||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
<artifactId>hessian-lite</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
|
@ -45,5 +45,11 @@ limitations under the License.
|
|||
<artifactId>log4j-slf4j-impl</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
<artifactId>dubbo-native</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
|||
|
|
@ -25,6 +25,8 @@ import com.alibaba.com.caucho.hessian.io.JavaDeserializer;
|
|||
import com.alibaba.com.caucho.hessian.io.JavaSerializer;
|
||||
import com.alibaba.com.caucho.hessian.io.Serializer;
|
||||
import com.alibaba.com.caucho.hessian.io.SerializerFactory;
|
||||
import com.alibaba.com.caucho.hessian.io.UnsafeDeserializer;
|
||||
import com.alibaba.com.caucho.hessian.io.UnsafeSerializer;
|
||||
|
||||
public class Hessian2SerializerFactory extends SerializerFactory {
|
||||
|
||||
|
|
@ -54,7 +56,9 @@ public class Hessian2SerializerFactory extends SerializerFactory {
|
|||
|
||||
checkSerializable(cl);
|
||||
|
||||
return new JavaSerializer(cl, getClassLoader());
|
||||
if (isEnableUnsafeSerializer() && JavaSerializer.getWriteReplace(cl) == null) {
|
||||
return UnsafeSerializer.create(cl);
|
||||
} else return JavaSerializer.create(cl);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -68,7 +72,9 @@ public class Hessian2SerializerFactory extends SerializerFactory {
|
|||
|
||||
checkSerializable(cl);
|
||||
|
||||
return new JavaDeserializer(cl);
|
||||
if (isEnableUnsafeSerializer()) {
|
||||
return new UnsafeDeserializer(cl, getFieldDeserializerFactory());
|
||||
} else return new JavaDeserializer(cl, getFieldDeserializerFactory());
|
||||
}
|
||||
|
||||
private void checkSerializable(Class<?> cl) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,100 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.dubbo.common.serialize.hessian2.aot;
|
||||
|
||||
import org.apache.dubbo.aot.api.MemberCategory;
|
||||
import org.apache.dubbo.aot.api.ReflectionTypeDescriberRegistrar;
|
||||
import org.apache.dubbo.aot.api.TypeDescriber;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.sql.Time;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import com.alibaba.com.caucho.hessian.io.BigDecimalDeserializer;
|
||||
import com.alibaba.com.caucho.hessian.io.FileDeserializer;
|
||||
import com.alibaba.com.caucho.hessian.io.HessianRemote;
|
||||
import com.alibaba.com.caucho.hessian.io.LocaleSerializer;
|
||||
import com.alibaba.com.caucho.hessian.io.ObjectNameDeserializer;
|
||||
import com.alibaba.com.caucho.hessian.io.StringValueSerializer;
|
||||
import com.alibaba.com.caucho.hessian.io.java8.DurationSerializer;
|
||||
import com.alibaba.com.caucho.hessian.io.java8.InstantSerializer;
|
||||
import com.alibaba.com.caucho.hessian.io.java8.LocalDateSerializer;
|
||||
import com.alibaba.com.caucho.hessian.io.java8.LocalDateTimeSerializer;
|
||||
import com.alibaba.com.caucho.hessian.io.java8.LocalTimeSerializer;
|
||||
import com.alibaba.com.caucho.hessian.io.java8.MonthDaySerializer;
|
||||
import com.alibaba.com.caucho.hessian.io.java8.OffsetDateTimeSerializer;
|
||||
import com.alibaba.com.caucho.hessian.io.java8.OffsetTimeSerializer;
|
||||
import com.alibaba.com.caucho.hessian.io.java8.PeriodSerializer;
|
||||
import com.alibaba.com.caucho.hessian.io.java8.YearMonthSerializer;
|
||||
import com.alibaba.com.caucho.hessian.io.java8.YearSerializer;
|
||||
import com.alibaba.com.caucho.hessian.io.java8.ZoneIdSerializer;
|
||||
import com.alibaba.com.caucho.hessian.io.java8.ZoneOffsetSerializer;
|
||||
import com.alibaba.com.caucho.hessian.io.java8.ZonedDateTimeSerializer;
|
||||
|
||||
public class HessianReflectionTypeDescriberRegistrar implements ReflectionTypeDescriberRegistrar {
|
||||
|
||||
@Override
|
||||
public List<TypeDescriber> getTypeDescribers() {
|
||||
List<TypeDescriber> typeDescribers = new ArrayList<>();
|
||||
typeDescribers.add(buildTypeDescriberWithDeclared(BigDecimalDeserializer.class));
|
||||
typeDescribers.add(buildTypeDescriberWithDeclared(FileDeserializer.class));
|
||||
typeDescribers.add(buildTypeDescriberWithDeclared(HessianRemote.class));
|
||||
typeDescribers.add(buildTypeDescriberWithDeclared(LocaleSerializer.class));
|
||||
typeDescribers.add(buildTypeDescriberWithDeclared(ObjectNameDeserializer.class));
|
||||
typeDescribers.add(buildTypeDescriberWithDeclared(StringValueSerializer.class));
|
||||
typeDescribers.add(buildTypeDescriberWithDeclared(DurationSerializer.class));
|
||||
typeDescribers.add(buildTypeDescriberWithDeclared(InstantSerializer.class));
|
||||
typeDescribers.add(buildTypeDescriberWithDeclared(LocalDateSerializer.class));
|
||||
typeDescribers.add(buildTypeDescriberWithDeclared(LocalDateTimeSerializer.class));
|
||||
typeDescribers.add(buildTypeDescriberWithDeclared(LocalTimeSerializer.class));
|
||||
typeDescribers.add(buildTypeDescriberWithDeclared(MonthDaySerializer.class));
|
||||
typeDescribers.add(buildTypeDescriberWithDeclared(OffsetDateTimeSerializer.class));
|
||||
typeDescribers.add(buildTypeDescriberWithDeclared(OffsetTimeSerializer.class));
|
||||
typeDescribers.add(buildTypeDescriberWithDeclared(PeriodSerializer.class));
|
||||
typeDescribers.add(buildTypeDescriberWithDeclared(YearMonthSerializer.class));
|
||||
typeDescribers.add(buildTypeDescriberWithDeclared(YearSerializer.class));
|
||||
typeDescribers.add(buildTypeDescriberWithDeclared(ZoneIdSerializer.class));
|
||||
typeDescribers.add(buildTypeDescriberWithDeclared(ZoneOffsetSerializer.class));
|
||||
typeDescribers.add(buildTypeDescriberWithDeclared(ZonedDateTimeSerializer.class));
|
||||
typeDescribers.add(buildTypeDescriberWithDeclared(Date.class));
|
||||
typeDescribers.add(buildTypeDescriberWithDeclared(Time.class));
|
||||
typeDescribers.add(buildTypeDescriberWithDeclared(Timestamp.class));
|
||||
|
||||
return typeDescribers;
|
||||
}
|
||||
|
||||
private TypeDescriber buildTypeDescriberWithDeclared(Class<?> cl) {
|
||||
Set<MemberCategory> memberCategories = new HashSet<>();
|
||||
memberCategories.add(MemberCategory.INVOKE_DECLARED_METHODS);
|
||||
memberCategories.add(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
|
||||
memberCategories.add(MemberCategory.DECLARED_FIELDS);
|
||||
return new TypeDescriber(
|
||||
cl.getName(), null, new HashSet<>(), new HashSet<>(), new HashSet<>(), memberCategories);
|
||||
}
|
||||
|
||||
private TypeDescriber buildTypeDescriberWithDeclared(String cl) {
|
||||
Set<MemberCategory> memberCategories = new HashSet<>();
|
||||
memberCategories.add(MemberCategory.INVOKE_DECLARED_METHODS);
|
||||
memberCategories.add(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
|
||||
memberCategories.add(MemberCategory.DECLARED_FIELDS);
|
||||
return new TypeDescriber(cl, null, new HashSet<>(), new HashSet<>(), new HashSet<>(), memberCategories);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.dubbo.common.serialize.hessian2.aot;
|
||||
|
||||
import org.apache.dubbo.aot.api.ResourceBundleDescriber;
|
||||
import org.apache.dubbo.aot.api.ResourceDescriberRegistrar;
|
||||
import org.apache.dubbo.aot.api.ResourcePatternDescriber;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class HessianResourceDescriberRegistrar implements ResourceDescriberRegistrar {
|
||||
@Override
|
||||
public List<ResourcePatternDescriber> getResourcePatternDescribers() {
|
||||
return Collections.singletonList(new ResourcePatternDescriber("DENY_CLASS", null));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ResourceBundleDescriber> getResourceBundleDescribers() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
hessian=org.apache.dubbo.common.serialize.hessian2.aot.HessianReflectionTypeDescriberRegistrar
|
||||
|
|
@ -0,0 +1 @@
|
|||
hessian=org.apache.dubbo.common.serialize.hessian2.aot.HessianResourceDescriberRegistrar
|
||||
|
|
@ -1 +1 @@
|
|||
hessian2=org.apache.dubbo.common.serialize.hessian2.Hessian2Serialization
|
||||
hessian2=org.apache.dubbo.common.serialize.hessian2.Hessian2Serialization
|
||||
|
|
|
|||
|
|
@ -206,7 +206,7 @@ class Hessian2SerializationTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
void testReadByte() throws IOException {
|
||||
void testReadByte() throws IOException, ClassNotFoundException {
|
||||
FrameworkModel frameworkModel = new FrameworkModel();
|
||||
Serialization serialization =
|
||||
frameworkModel.getExtensionLoader(Serialization.class).getExtension("hessian2");
|
||||
|
|
@ -219,6 +219,19 @@ class Hessian2SerializationTest {
|
|||
objectOutput.writeObject((byte) 11);
|
||||
objectOutput.flushBuffer();
|
||||
|
||||
byte[] bytes = outputStream.toByteArray();
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
|
||||
ObjectInput objectInput = serialization.deserialize(url, inputStream);
|
||||
Assertions.assertEquals((byte) 11, objectInput.readObject());
|
||||
}
|
||||
|
||||
// write byte, read byte
|
||||
{
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
ObjectOutput objectOutput = serialization.serialize(url, outputStream);
|
||||
objectOutput.writeByte((byte) 11);
|
||||
objectOutput.flushBuffer();
|
||||
|
||||
byte[] bytes = outputStream.toByteArray();
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
|
||||
ObjectInput objectInput = serialization.deserialize(url, inputStream);
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ limitations under the License.
|
|||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
<artifactId>hessian-lite</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
|
|
|||
Loading…
Reference in New Issue