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

Support exclude fastjson2 or hessian2 serialization dependencies #13441

Merged
merged 7 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,6 +42,7 @@ class ProtocolConfigTest {
@BeforeEach
public void setUp() {
DubboBootstrap.reset();
// FrameworkModel.defaultModel().getBeanFactory().registerBean(TestPreferSerializationProvider.class);
}

@AfterEach
Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
}
}
Original file line number Diff line number Diff line change
@@ -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) {}
}
Original file line number Diff line number Diff line change
@@ -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<String> 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
serialization-api=org.apache.dubbo.common.serialize.SerializationScopeModelInitializer
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
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;
Expand All @@ -37,6 +39,19 @@
* </pre>
*/
public class FastJson2Serialization implements Serialization {
private static final Logger logger = LoggerFactory.getLogger(FastJson2Serialization.class);

static {
Class<?> aClass = null;
try {
aClass = com.alibaba.fastjson2.JSONB.class;
} 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,17 @@ 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 = com.alibaba.fastjson2.JSONB.class;
} catch (Throwable ignored) {
}

if (aClass != null) {
ScopeBeanFactory beanFactory = frameworkModel.getBeanFactory();
beanFactory.registerBean(Fastjson2CreatorManager.class);
beanFactory.registerBean(Fastjson2SecurityManager.class);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,18 @@
public class Hessian2ScopeModelInitializer implements ScopeModelInitializer {
@Override
public void initializeFrameworkModel(FrameworkModel frameworkModel) {
ScopeBeanFactory beanFactory = frameworkModel.getBeanFactory();
beanFactory.registerBean(Hessian2FactoryManager.class);
Class<?> aClass = null;
try {
aClass = com.alibaba.com.caucho.hessian.io.Hessian2Output.class;
} catch (Throwable ignored) {
}

frameworkModel.addClassLoaderListener(new Hessian2ClassLoaderListener());
if (aClass != null) {
ScopeBeanFactory beanFactory = frameworkModel.getBeanFactory();
beanFactory.registerBean(Hessian2FactoryManager.class);

frameworkModel.addClassLoaderListener(new Hessian2ClassLoaderListener());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
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;
Expand All @@ -37,6 +39,20 @@
* </pre>
*/
public class Hessian2Serialization implements Serialization {
private static final Logger logger = LoggerFactory.getLogger(Hessian2Serialization.class);

static {
Class<?> aClass = null;
try {
aClass = com.alibaba.com.caucho.hessian.io.Hessian2Output.class;
} 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() {
Expand Down
Loading