From 6eb3116a16c349a294a8aeed848b3d0d9a42d0b7 Mon Sep 17 00:00:00 2001 From: Albumen Kevin Date: Thu, 30 Nov 2023 14:19:38 +0800 Subject: [PATCH 1/7] Support exclude fastjson2 or hessian2 serialization dependencies --- .../fastjson2/FastJson2Serialization.java | 15 +++++++++++++++ .../Fastjson2ScopeModelInitializer.java | 16 +++++++++++++--- .../hessian2/Hessian2ScopeModelInitializer.java | 14 +++++++++++--- .../hessian2/Hessian2Serialization.java | 15 +++++++++++++++ 4 files changed, 54 insertions(+), 6 deletions(-) diff --git a/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/FastJson2Serialization.java b/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/FastJson2Serialization.java index 39ee568f423..09414aaf9d3 100644 --- a/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/FastJson2Serialization.java +++ b/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/FastJson2Serialization.java @@ -17,9 +17,12 @@ package org.apache.dubbo.common.serialize.fastjson2; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.logger.Logger; +import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.serialize.ObjectInput; import org.apache.dubbo.common.serialize.ObjectOutput; import org.apache.dubbo.common.serialize.Serialization; +import org.apache.dubbo.common.utils.ClassUtils; import org.apache.dubbo.rpc.model.FrameworkModel; import java.io.IOException; @@ -37,6 +40,18 @@ * */ public class FastJson2Serialization implements Serialization { + private final static Logger logger = LoggerFactory.getLogger(FastJson2Serialization.class); + + static { + Class aClass = null; + try { + aClass = ClassUtils.forName("com.alibaba.fastjson2.JSONB"); + } catch (Throwable ignored) { } + if (aClass == null) { + logger.info("Failed to load com.alibaba.fastjson2.JSONB, fastjson2 serialization will be disabled."); + throw new IllegalStateException("The fastjson2 is not in classpath."); + } + } @Override public byte getContentTypeId() { diff --git a/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/Fastjson2ScopeModelInitializer.java b/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/Fastjson2ScopeModelInitializer.java index 520d9465be1..a658d3a7b7f 100644 --- a/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/Fastjson2ScopeModelInitializer.java +++ b/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/Fastjson2ScopeModelInitializer.java @@ -17,6 +17,9 @@ package org.apache.dubbo.common.serialize.fastjson2; import org.apache.dubbo.common.beans.factory.ScopeBeanFactory; +import org.apache.dubbo.common.logger.Logger; +import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.common.utils.ClassUtils; import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.rpc.model.FrameworkModel; import org.apache.dubbo.rpc.model.ModuleModel; @@ -26,9 +29,16 @@ public class Fastjson2ScopeModelInitializer implements ScopeModelInitializer { @Override public void initializeFrameworkModel(FrameworkModel frameworkModel) { - ScopeBeanFactory beanFactory = frameworkModel.getBeanFactory(); - beanFactory.registerBean(Fastjson2CreatorManager.class); - beanFactory.registerBean(Fastjson2SecurityManager.class); + Class aClass = null; + try { + aClass = ClassUtils.forName("com.alibaba.fastjson2.JSONB"); + } catch (Throwable ignored) { } + + if (aClass != null) { + ScopeBeanFactory beanFactory = frameworkModel.getBeanFactory(); + beanFactory.registerBean(Fastjson2CreatorManager.class); + beanFactory.registerBean(Fastjson2SecurityManager.class); + } } @Override diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2ScopeModelInitializer.java b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2ScopeModelInitializer.java index 41ee9b5fa99..813cd8f4ba3 100644 --- a/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2ScopeModelInitializer.java +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2ScopeModelInitializer.java @@ -17,6 +17,7 @@ package org.apache.dubbo.common.serialize.hessian2; import org.apache.dubbo.common.beans.factory.ScopeBeanFactory; +import org.apache.dubbo.common.utils.ClassUtils; import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.rpc.model.FrameworkModel; import org.apache.dubbo.rpc.model.ModuleModel; @@ -25,10 +26,17 @@ public class Hessian2ScopeModelInitializer implements ScopeModelInitializer { @Override public void initializeFrameworkModel(FrameworkModel frameworkModel) { - ScopeBeanFactory beanFactory = frameworkModel.getBeanFactory(); - beanFactory.registerBean(Hessian2FactoryManager.class); + Class aClass = null; + try { + aClass = ClassUtils.forName("com.alibaba.com.caucho.hessian.io.Hessian2Output"); + } catch (Throwable ignored) { } - frameworkModel.addClassLoaderListener(new Hessian2ClassLoaderListener()); + if (aClass != null) { + ScopeBeanFactory beanFactory = frameworkModel.getBeanFactory(); + beanFactory.registerBean(Hessian2FactoryManager.class); + + frameworkModel.addClassLoaderListener(new Hessian2ClassLoaderListener()); + } } @Override diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2Serialization.java b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2Serialization.java index 4263d3de0e5..428e7a190c7 100644 --- a/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2Serialization.java +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2Serialization.java @@ -17,9 +17,12 @@ package org.apache.dubbo.common.serialize.hessian2; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.logger.Logger; +import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.serialize.ObjectInput; import org.apache.dubbo.common.serialize.ObjectOutput; import org.apache.dubbo.common.serialize.Serialization; +import org.apache.dubbo.common.utils.ClassUtils; import org.apache.dubbo.rpc.model.FrameworkModel; import java.io.IOException; @@ -37,6 +40,18 @@ * */ public class Hessian2Serialization implements Serialization { + private final static Logger logger = LoggerFactory.getLogger(Hessian2Serialization.class); + + static { + Class aClass = null; + try { + aClass = ClassUtils.forName("com.alibaba.com.caucho.hessian.io.Hessian2Output"); + } catch (Throwable ignored) { } + if (aClass == null) { + logger.info("Failed to load com.alibaba.com.caucho.hessian.io.Hessian2Output, hessian2 serialization will be disabled."); + throw new IllegalStateException("The hessian2 is not in classpath."); + } + } @Override public byte getContentTypeId() { From d56fcee4aaff6910ffe89edd95467ff414740b26 Mon Sep 17 00:00:00 2001 From: Albumen Kevin Date: Thu, 30 Nov 2023 15:33:04 +0800 Subject: [PATCH 2/7] Fix style --- .../serialize/fastjson2/FastJson2Serialization.java | 5 +++-- .../fastjson2/Fastjson2ScopeModelInitializer.java | 5 ++--- .../serialize/hessian2/Hessian2ScopeModelInitializer.java | 3 ++- .../common/serialize/hessian2/Hessian2Serialization.java | 8 +++++--- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/FastJson2Serialization.java b/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/FastJson2Serialization.java index 09414aaf9d3..5d60a5decf2 100644 --- a/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/FastJson2Serialization.java +++ b/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/FastJson2Serialization.java @@ -40,13 +40,14 @@ * */ public class FastJson2Serialization implements Serialization { - private final static Logger logger = LoggerFactory.getLogger(FastJson2Serialization.class); + private static final Logger logger = LoggerFactory.getLogger(FastJson2Serialization.class); static { Class aClass = null; try { aClass = ClassUtils.forName("com.alibaba.fastjson2.JSONB"); - } catch (Throwable ignored) { } + } catch (Throwable ignored) { + } if (aClass == null) { logger.info("Failed to load com.alibaba.fastjson2.JSONB, fastjson2 serialization will be disabled."); throw new IllegalStateException("The fastjson2 is not in classpath."); diff --git a/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/Fastjson2ScopeModelInitializer.java b/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/Fastjson2ScopeModelInitializer.java index a658d3a7b7f..242ca59d415 100644 --- a/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/Fastjson2ScopeModelInitializer.java +++ b/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/Fastjson2ScopeModelInitializer.java @@ -17,8 +17,6 @@ package org.apache.dubbo.common.serialize.fastjson2; import org.apache.dubbo.common.beans.factory.ScopeBeanFactory; -import org.apache.dubbo.common.logger.Logger; -import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.ClassUtils; import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.rpc.model.FrameworkModel; @@ -32,7 +30,8 @@ public void initializeFrameworkModel(FrameworkModel frameworkModel) { Class aClass = null; try { aClass = ClassUtils.forName("com.alibaba.fastjson2.JSONB"); - } catch (Throwable ignored) { } + } catch (Throwable ignored) { + } if (aClass != null) { ScopeBeanFactory beanFactory = frameworkModel.getBeanFactory(); diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2ScopeModelInitializer.java b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2ScopeModelInitializer.java index 813cd8f4ba3..ff07a9ed4bd 100644 --- a/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2ScopeModelInitializer.java +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2ScopeModelInitializer.java @@ -29,7 +29,8 @@ public void initializeFrameworkModel(FrameworkModel frameworkModel) { Class aClass = null; try { aClass = ClassUtils.forName("com.alibaba.com.caucho.hessian.io.Hessian2Output"); - } catch (Throwable ignored) { } + } catch (Throwable ignored) { + } if (aClass != null) { ScopeBeanFactory beanFactory = frameworkModel.getBeanFactory(); diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2Serialization.java b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2Serialization.java index 428e7a190c7..80ceb29f524 100644 --- a/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2Serialization.java +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2Serialization.java @@ -40,15 +40,17 @@ * */ public class Hessian2Serialization implements Serialization { - private final static Logger logger = LoggerFactory.getLogger(Hessian2Serialization.class); + private static final Logger logger = LoggerFactory.getLogger(Hessian2Serialization.class); static { Class aClass = null; try { aClass = ClassUtils.forName("com.alibaba.com.caucho.hessian.io.Hessian2Output"); - } catch (Throwable ignored) { } + } catch (Throwable ignored) { + } if (aClass == null) { - logger.info("Failed to load com.alibaba.com.caucho.hessian.io.Hessian2Output, hessian2 serialization will be disabled."); + logger.info( + "Failed to load com.alibaba.com.caucho.hessian.io.Hessian2Output, hessian2 serialization will be disabled."); throw new IllegalStateException("The hessian2 is not in classpath."); } } From 2945e8284679a1ec253e2cb7664b9ae1b230948f Mon Sep 17 00:00:00 2001 From: Albumen Kevin Date: Thu, 30 Nov 2023 17:29:26 +0800 Subject: [PATCH 3/7] Support check --- .../PreferSerializationProvider.java} | 13 ++---- .../apache/dubbo/config/ProtocolConfig.java | 9 +++- .../dubbo/config/ProtocolConfigTest.java | 6 +-- .../TestPreferSerializationProvider.java | 26 ++++++++++++ .../SerializationScopeModelInitializer.java | 36 ++++++++++++++++ .../PreferSerializationProviderImpl.java | 42 +++++++++++++++++++ ...ache.dubbo.rpc.model.ScopeModelInitializer | 1 + 7 files changed, 118 insertions(+), 15 deletions(-) rename dubbo-common/src/main/java/org/apache/dubbo/common/{constants/ProviderConstants.java => serialization/PreferSerializationProvider.java} (75%) create mode 100644 dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/utils/TestPreferSerializationProvider.java create mode 100644 dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/SerializationScopeModelInitializer.java create mode 100644 dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/support/PreferSerializationProviderImpl.java create mode 100644 dubbo-serialization/dubbo-serialization-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.model.ScopeModelInitializer diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/ProviderConstants.java b/dubbo-common/src/main/java/org/apache/dubbo/common/serialization/PreferSerializationProvider.java similarity index 75% rename from dubbo-common/src/main/java/org/apache/dubbo/common/constants/ProviderConstants.java rename to dubbo-common/src/main/java/org/apache/dubbo/common/serialization/PreferSerializationProvider.java index e94540d328f..ee97ba58ce8 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/ProviderConstants.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/serialization/PreferSerializationProvider.java @@ -14,15 +14,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.common.constants; +package org.apache.dubbo.common.serialization; -/** - * Provider Constants - */ -public interface ProviderConstants { - - /** - * Default prefer serialization,multiple separated by commas - */ - String DEFAULT_PREFER_SERIALIZATION = "fastjson2,hessian2"; +public interface PreferSerializationProvider { + String getPreferSerialization(); } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/ProtocolConfig.java b/dubbo-common/src/main/java/org/apache/dubbo/config/ProtocolConfig.java index 5fba5d3020d..bae108cfe58 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/config/ProtocolConfig.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/config/ProtocolConfig.java @@ -16,6 +16,7 @@ */ package org.apache.dubbo.config; +import org.apache.dubbo.common.serialization.PreferSerializationProvider; import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.config.support.Parameter; @@ -29,7 +30,6 @@ import static org.apache.dubbo.common.constants.CommonConstants.SSL_ENABLED_KEY; import static org.apache.dubbo.common.constants.CommonConstants.THREAD_POOL_EXHAUSTED_LISTENERS_KEY; import static org.apache.dubbo.common.constants.LoggerCodeConstants.COMMON_UNEXPECTED_EXCEPTION; -import static org.apache.dubbo.common.constants.ProviderConstants.DEFAULT_PREFER_SERIALIZATION; /** * ProtocolConfig @@ -262,7 +262,12 @@ protected void checkDefault() { } if (StringUtils.isBlank(preferSerialization)) { - preferSerialization = serialization != null ? serialization : DEFAULT_PREFER_SERIALIZATION; + preferSerialization = serialization != null + ? serialization + : getScopeModel() + .getBeanFactory() + .getBean(PreferSerializationProvider.class) + .getPreferSerialization(); } } diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ProtocolConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ProtocolConfigTest.java index 686794ffa68..3c86651c6c3 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ProtocolConfigTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ProtocolConfigTest.java @@ -31,7 +31,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.apache.dubbo.common.constants.ProviderConstants.DEFAULT_PREFER_SERIALIZATION; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasEntry; @@ -43,6 +42,7 @@ class ProtocolConfigTest { @BeforeEach public void setUp() { DubboBootstrap.reset(); + // FrameworkModel.defaultModel().getBeanFactory().registerBean(TestPreferSerializationProvider.class); } @AfterEach @@ -389,7 +389,7 @@ void testPreferSerializationDefault1() throws Exception { assertNull(protocolConfig.getPreferSerialization()); protocolConfig.checkDefault(); - assertThat(protocolConfig.getPreferSerialization(), equalTo(DEFAULT_PREFER_SERIALIZATION)); + assertThat(protocolConfig.getPreferSerialization(), equalTo("fastjson2,hessian2")); protocolConfig = new ProtocolConfig(); protocolConfig.setSerialization("x-serialization"); @@ -405,7 +405,7 @@ void testPreferSerializationDefault2() throws Exception { assertNull(protocolConfig.getPreferSerialization()); protocolConfig.refresh(); - assertThat(protocolConfig.getPreferSerialization(), equalTo(DEFAULT_PREFER_SERIALIZATION)); + assertThat(protocolConfig.getPreferSerialization(), equalTo("fastjson2,hessian2")); protocolConfig = new ProtocolConfig(); protocolConfig.setSerialization("x-serialization"); diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/utils/TestPreferSerializationProvider.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/utils/TestPreferSerializationProvider.java new file mode 100644 index 00000000000..dd2d1f119f4 --- /dev/null +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/utils/TestPreferSerializationProvider.java @@ -0,0 +1,26 @@ +/* + * 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.config.utils; + +import org.apache.dubbo.common.serialization.PreferSerializationProvider; + +public class TestPreferSerializationProvider implements PreferSerializationProvider { + @Override + public String getPreferSerialization() { + return "fastjson2,hessian2"; + } +} diff --git a/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/SerializationScopeModelInitializer.java b/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/SerializationScopeModelInitializer.java new file mode 100644 index 00000000000..184bdb7df89 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/SerializationScopeModelInitializer.java @@ -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; + +import org.apache.dubbo.common.serialize.support.PreferSerializationProviderImpl; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.model.FrameworkModel; +import org.apache.dubbo.rpc.model.ModuleModel; +import org.apache.dubbo.rpc.model.ScopeModelInitializer; + +public class SerializationScopeModelInitializer implements ScopeModelInitializer { + @Override + public void initializeFrameworkModel(FrameworkModel frameworkModel) { + frameworkModel.getBeanFactory().registerBean(PreferSerializationProviderImpl.class); + } + + @Override + public void initializeApplicationModel(ApplicationModel applicationModel) {} + + @Override + public void initializeModuleModel(ModuleModel moduleModel) {} +} diff --git a/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/support/PreferSerializationProviderImpl.java b/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/support/PreferSerializationProviderImpl.java new file mode 100644 index 00000000000..402600fe3b0 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/support/PreferSerializationProviderImpl.java @@ -0,0 +1,42 @@ +/* + * 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.support; + +import org.apache.dubbo.common.serialization.PreferSerializationProvider; +import org.apache.dubbo.common.serialize.Serialization; +import org.apache.dubbo.rpc.model.FrameworkModel; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class PreferSerializationProviderImpl implements PreferSerializationProvider { + private final String preferSerialization; + + public PreferSerializationProviderImpl(FrameworkModel frameworkModel) { + List defaultSerializations = Arrays.asList("fastjson2", "hessian2"); + this.preferSerialization = defaultSerializations.stream() + .filter(s -> + frameworkModel.getExtensionLoader(Serialization.class).hasExtension(s)) + .collect(Collectors.joining(",")); + } + + @Override + public String getPreferSerialization() { + return preferSerialization; + } +} diff --git a/dubbo-serialization/dubbo-serialization-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.model.ScopeModelInitializer b/dubbo-serialization/dubbo-serialization-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.model.ScopeModelInitializer new file mode 100644 index 00000000000..224d8115803 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.model.ScopeModelInitializer @@ -0,0 +1 @@ +serialization-api=org.apache.dubbo.common.serialize.SerializationScopeModelInitializer From 27687b0fbba113b8acf6c95a26e35b805268c9ef Mon Sep 17 00:00:00 2001 From: Albumen Kevin Date: Fri, 1 Dec 2023 09:50:12 +0800 Subject: [PATCH 4/7] Fix test cases --- .../apache/dubbo/config/context/ConfigManagerTest.java | 10 ++++++++++ .../serialize/fastjson2/FastJson2Serialization.java | 3 +-- .../serialize/hessian2/Hessian2Serialization.java | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/dubbo-common/src/test/java/org/apache/dubbo/config/context/ConfigManagerTest.java b/dubbo-common/src/test/java/org/apache/dubbo/config/context/ConfigManagerTest.java index ae74fc11a4c..bcccd7e1338 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/config/context/ConfigManagerTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/config/context/ConfigManagerTest.java @@ -16,6 +16,7 @@ */ package org.apache.dubbo.config.context; +import org.apache.dubbo.common.serialization.PreferSerializationProvider; import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.config.ApplicationConfig; import org.apache.dubbo.config.ConfigCenterConfig; @@ -27,6 +28,7 @@ import org.apache.dubbo.config.ProviderConfig; import org.apache.dubbo.config.RegistryConfig; import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.model.FrameworkModel; import java.util.Collection; import java.util.Optional; @@ -64,6 +66,7 @@ public void init() { ApplicationModel applicationModel = ApplicationModel.defaultModel(); configManager = applicationModel.getApplicationConfigManager(); moduleConfigManager = applicationModel.getDefaultModule().getConfigManager(); + FrameworkModel.defaultModel().getBeanFactory().registerBean(TestPreferSerializationProvider.class); } @Test @@ -474,4 +477,11 @@ void testLoadConfig() { Assertions.assertFalse(moduleConfigManager.getProviders().isEmpty()); Assertions.assertFalse(moduleConfigManager.getConsumers().isEmpty()); } + + public static class TestPreferSerializationProvider implements PreferSerializationProvider { + @Override + public String getPreferSerialization() { + return "fastjson2,hessian2"; + } + } } diff --git a/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/FastJson2Serialization.java b/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/FastJson2Serialization.java index 5d60a5decf2..64d4b6f4fda 100644 --- a/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/FastJson2Serialization.java +++ b/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/FastJson2Serialization.java @@ -22,7 +22,6 @@ import org.apache.dubbo.common.serialize.ObjectInput; import org.apache.dubbo.common.serialize.ObjectOutput; import org.apache.dubbo.common.serialize.Serialization; -import org.apache.dubbo.common.utils.ClassUtils; import org.apache.dubbo.rpc.model.FrameworkModel; import java.io.IOException; @@ -45,7 +44,7 @@ public class FastJson2Serialization implements Serialization { static { Class aClass = null; try { - aClass = ClassUtils.forName("com.alibaba.fastjson2.JSONB"); + aClass = com.alibaba.fastjson2.JSONB.class; } catch (Throwable ignored) { } if (aClass == null) { diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2Serialization.java b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2Serialization.java index 80ceb29f524..7d9d839679e 100644 --- a/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2Serialization.java +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2Serialization.java @@ -45,7 +45,7 @@ public class Hessian2Serialization implements Serialization { static { Class aClass = null; try { - aClass = ClassUtils.forName("com.alibaba.com.caucho.hessian.io.Hessian2Output"); + aClass = com.alibaba.com.caucho.hessian.io.Hessian2Output.class; } catch (Throwable ignored) { } if (aClass == null) { From bd637e1321ba110cc05f09642ee3685bf6c3ffcf Mon Sep 17 00:00:00 2001 From: Albumen Kevin Date: Fri, 1 Dec 2023 09:51:26 +0800 Subject: [PATCH 5/7] lint --- .../dubbo/common/serialize/hessian2/Hessian2Serialization.java | 1 - 1 file changed, 1 deletion(-) diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2Serialization.java b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2Serialization.java index 7d9d839679e..8bec705b72e 100644 --- a/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2Serialization.java +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2Serialization.java @@ -22,7 +22,6 @@ import org.apache.dubbo.common.serialize.ObjectInput; import org.apache.dubbo.common.serialize.ObjectOutput; import org.apache.dubbo.common.serialize.Serialization; -import org.apache.dubbo.common.utils.ClassUtils; import org.apache.dubbo.rpc.model.FrameworkModel; import java.io.IOException; From 6038f1e4d6a8ccb7883882ee39ebd479e01c6970 Mon Sep 17 00:00:00 2001 From: Albumen Kevin Date: Fri, 1 Dec 2023 15:13:25 +0800 Subject: [PATCH 6/7] Fix test cases --- .../serialize/fastjson2/Fastjson2ScopeModelInitializer.java | 2 +- .../serialize/hessian2/Hessian2ScopeModelInitializer.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/Fastjson2ScopeModelInitializer.java b/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/Fastjson2ScopeModelInitializer.java index 242ca59d415..ced73e7e748 100644 --- a/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/Fastjson2ScopeModelInitializer.java +++ b/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/Fastjson2ScopeModelInitializer.java @@ -29,7 +29,7 @@ public class Fastjson2ScopeModelInitializer implements ScopeModelInitializer { public void initializeFrameworkModel(FrameworkModel frameworkModel) { Class aClass = null; try { - aClass = ClassUtils.forName("com.alibaba.fastjson2.JSONB"); + aClass = com.alibaba.fastjson2.JSONB.class; } catch (Throwable ignored) { } diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2ScopeModelInitializer.java b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2ScopeModelInitializer.java index ff07a9ed4bd..44eab322297 100644 --- a/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2ScopeModelInitializer.java +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2ScopeModelInitializer.java @@ -28,7 +28,7 @@ public class Hessian2ScopeModelInitializer implements ScopeModelInitializer { public void initializeFrameworkModel(FrameworkModel frameworkModel) { Class aClass = null; try { - aClass = ClassUtils.forName("com.alibaba.com.caucho.hessian.io.Hessian2Output"); + aClass = com.alibaba.com.caucho.hessian.io.Hessian2Output.class; } catch (Throwable ignored) { } From 2e6d8ca78b472a0a4027855d7a2316a35cebf6f8 Mon Sep 17 00:00:00 2001 From: Albumen Kevin Date: Mon, 4 Dec 2023 09:34:38 +0800 Subject: [PATCH 7/7] Style --- .../serialize/fastjson2/Fastjson2ScopeModelInitializer.java | 1 - .../common/serialize/hessian2/Hessian2ScopeModelInitializer.java | 1 - 2 files changed, 2 deletions(-) diff --git a/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/Fastjson2ScopeModelInitializer.java b/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/Fastjson2ScopeModelInitializer.java index ced73e7e748..029839fa75e 100644 --- a/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/Fastjson2ScopeModelInitializer.java +++ b/dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/Fastjson2ScopeModelInitializer.java @@ -17,7 +17,6 @@ package org.apache.dubbo.common.serialize.fastjson2; import org.apache.dubbo.common.beans.factory.ScopeBeanFactory; -import org.apache.dubbo.common.utils.ClassUtils; import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.rpc.model.FrameworkModel; import org.apache.dubbo.rpc.model.ModuleModel; diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2ScopeModelInitializer.java b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2ScopeModelInitializer.java index 44eab322297..2ca838cb074 100644 --- a/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2ScopeModelInitializer.java +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2ScopeModelInitializer.java @@ -17,7 +17,6 @@ package org.apache.dubbo.common.serialize.hessian2; import org.apache.dubbo.common.beans.factory.ScopeBeanFactory; -import org.apache.dubbo.common.utils.ClassUtils; import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.rpc.model.FrameworkModel; import org.apache.dubbo.rpc.model.ModuleModel;