Remove ExtensionLoader getExtension usage (#10971)

This commit is contained in:
Albumen Kevin 2022-12-22 10:02:22 +08:00 committed by GitHub
parent eba39fd65b
commit 0df34a1a20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
34 changed files with 253 additions and 168 deletions

View File

@ -62,21 +62,37 @@ public class Parameters {
return parameters;
}
/**
* @deprecated will be removed in 3.3.0
*/
@Deprecated
public <T> T getExtension(Class<T> type, String key) {
String name = getParameter(key);
return ExtensionLoader.getExtensionLoader(type).getExtension(name);
}
/**
* @deprecated will be removed in 3.3.0
*/
@Deprecated
public <T> T getExtension(Class<T> type, String key, String defaultValue) {
String name = getParameter(key, defaultValue);
return ExtensionLoader.getExtensionLoader(type).getExtension(name);
}
/**
* @deprecated will be removed in 3.3.0
*/
@Deprecated
public <T> T getMethodExtension(Class<T> type, String method, String key) {
String name = getMethodParameter(method, key);
return ExtensionLoader.getExtensionLoader(type).getExtension(name);
}
/**
* @deprecated will be removed in 3.3.0
*/
@Deprecated
public <T> T getMethodExtension(Class<T> type, String method, String key, String defaultValue) {
String name = getMethodParameter(method, key, defaultValue);
return ExtensionLoader.getExtensionLoader(type).getExtension(name);

View File

@ -20,10 +20,10 @@ import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.extension.ExtensionScope;
import org.apache.dubbo.common.extension.SPI;
import org.apache.dubbo.common.lang.Prioritized;
import org.apache.dubbo.rpc.model.FrameworkModel;
import java.util.Collection;
import static org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader;
import static org.apache.dubbo.common.utils.TypeUtils.findActualTypeArgument;
/**
@ -72,9 +72,12 @@ public interface MultiValueConverter<S> extends Prioritized {
* @return <code>null</code> if not found
* @see ExtensionLoader#getSupportedExtensionInstances()
* @since 2.7.8
* @deprecated will be removed in 3.3.0
*/
@Deprecated
static MultiValueConverter<?> find(Class<?> sourceType, Class<?> targetType) {
return getExtensionLoader(MultiValueConverter.class)
return FrameworkModel.defaultModel()
.getExtensionLoader(MultiValueConverter.class)
.getSupportedExtensionInstances()
.stream()
.filter(converter -> converter.accept(sourceType, targetType))
@ -82,6 +85,10 @@ public interface MultiValueConverter<S> extends Prioritized {
.orElse(null);
}
/**
* @deprecated will be removed in 3.3.0
*/
@Deprecated
static <T> T convertIfPossible(Object source, Class<?> multiValueType, Class<?> elementType) {
Class<?> sourceType = source.getClass();
MultiValueConverter converter = find(sourceType, multiValueType);

View File

@ -16,7 +16,7 @@
*/
package org.apache.dubbo.common.url.component.param;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.rpc.model.FrameworkModel;
import java.util.Arrays;
import java.util.Comparator;
@ -89,7 +89,7 @@ public final class DynamicParamTable {
keys.add("");
values.add(new DynamicValues(null));
ExtensionLoader.getExtensionLoader(DynamicParamSource.class)
FrameworkModel.defaultModel().getExtensionLoader(DynamicParamSource.class)
.getSupportedExtensionInstances().forEach(source -> source.init(keys, values));
TreeMap<String, ParamValue> resultMap = new TreeMap<>(Comparator.comparingInt(System::identityHashCode));

View File

@ -22,6 +22,7 @@ import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Service;
import org.apache.dubbo.metadata.definition.MethodDefinitionBuilder;
import org.apache.dubbo.metadata.definition.model.MethodDefinition;
import org.apache.dubbo.rpc.model.ApplicationModel;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
@ -38,7 +39,6 @@ import java.util.function.Consumer;
import static java.util.Collections.emptyList;
import static java.util.Collections.sort;
import static java.util.Collections.unmodifiableMap;
import static org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader;
import static org.apache.dubbo.common.function.ThrowableFunction.execute;
import static org.apache.dubbo.common.utils.AnnotationUtils.isAnyAnnotationPresent;
import static org.apache.dubbo.common.utils.ClassUtils.forName;
@ -57,8 +57,8 @@ public abstract class AbstractServiceRestMetadataResolver implements ServiceRest
private final Map<String, List<AnnotatedMethodParameterProcessor>> parameterProcessorsMap;
public AbstractServiceRestMetadataResolver() {
this.parameterProcessorsMap = loadAnnotatedMethodParameterProcessors();
public AbstractServiceRestMetadataResolver(ApplicationModel applicationModel) {
this.parameterProcessorsMap = loadAnnotatedMethodParameterProcessors(applicationModel);
}
@Override
@ -329,9 +329,9 @@ public abstract class AbstractServiceRestMetadataResolver implements ServiceRest
Class<?> serviceInterfaceClass, RestMethodMetadata metadata) {
}
private static Map<String, List<AnnotatedMethodParameterProcessor>> loadAnnotatedMethodParameterProcessors() {
private static Map<String, List<AnnotatedMethodParameterProcessor>> loadAnnotatedMethodParameterProcessors(ApplicationModel applicationModel) {
Map<String, List<AnnotatedMethodParameterProcessor>> parameterProcessorsMap = new LinkedHashMap<>();
getExtensionLoader(AnnotatedMethodParameterProcessor.class)
applicationModel.getExtensionLoader(AnnotatedMethodParameterProcessor.class)
.getSupportedExtensionInstances()
.forEach(processor -> {
List<AnnotatedMethodParameterProcessor> processors =

View File

@ -16,6 +16,8 @@
*/
package org.apache.dubbo.metadata.rest;
import org.apache.dubbo.rpc.model.ApplicationModel;
import java.lang.reflect.Method;
import java.util.Set;
@ -26,6 +28,9 @@ import java.util.Set;
* @since 2.7.6
*/
public class DefaultServiceRestMetadataResolver extends AbstractServiceRestMetadataResolver {
public DefaultServiceRestMetadataResolver(ApplicationModel applicationModel) {
super(applicationModel);
}
@Override
protected boolean supports0(Class<?> serviceType) {

View File

@ -18,6 +18,7 @@ package org.apache.dubbo.metadata.rest.jaxrs;
import org.apache.dubbo.metadata.rest.AbstractServiceRestMetadataResolver;
import org.apache.dubbo.metadata.rest.ServiceRestMetadataResolver;
import org.apache.dubbo.rpc.model.ApplicationModel;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
@ -40,6 +41,9 @@ import static org.apache.dubbo.metadata.rest.RestMetadataConstants.JAX_RS.PRODUC
* @since 2.7.6
*/
public class JAXRSServiceRestMetadataResolver extends AbstractServiceRestMetadataResolver {
public JAXRSServiceRestMetadataResolver(ApplicationModel applicationModel) {
super(applicationModel);
}
@Override
protected boolean supports0(Class<?> serviceType) {

View File

@ -18,6 +18,7 @@ package org.apache.dubbo.metadata.rest.springmvc;
import org.apache.dubbo.metadata.rest.AbstractServiceRestMetadataResolver;
import org.apache.dubbo.metadata.rest.ServiceRestMetadataResolver;
import org.apache.dubbo.rpc.model.ApplicationModel;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
@ -48,6 +49,10 @@ public class SpringMvcServiceRestMetadataResolver extends AbstractServiceRestMet
private static final int FIRST_ELEMENT_INDEX = 0;
public SpringMvcServiceRestMetadataResolver(ApplicationModel applicationModel) {
super(applicationModel);
}
@Override
protected boolean supports0(Class<?> serviceType) {
return isAnnotationPresent(serviceType, CONTROLLER_ANNOTATION_CLASS);

View File

@ -23,6 +23,7 @@ import org.apache.dubbo.metadata.rest.RestService;
import org.apache.dubbo.metadata.rest.ServiceRestMetadata;
import org.apache.dubbo.metadata.rest.SpringRestService;
import org.apache.dubbo.metadata.rest.StandardRestService;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.junit.jupiter.api.Test;
@ -39,7 +40,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
*/
class JAXRSServiceRestMetadataResolverTest {
private JAXRSServiceRestMetadataResolver instance = new JAXRSServiceRestMetadataResolver();
private JAXRSServiceRestMetadataResolver instance = new JAXRSServiceRestMetadataResolver(ApplicationModel.defaultModel());
@Test
void testSupports() {

View File

@ -23,6 +23,7 @@ import org.apache.dubbo.metadata.rest.RestService;
import org.apache.dubbo.metadata.rest.ServiceRestMetadata;
import org.apache.dubbo.metadata.rest.SpringRestService;
import org.apache.dubbo.metadata.rest.StandardRestService;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.junit.jupiter.api.Test;
@ -39,7 +40,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
*/
class SpringMvcServiceRestMetadataResolverTest {
private SpringMvcServiceRestMetadataResolver instance = new SpringMvcServiceRestMetadataResolver();
private SpringMvcServiceRestMetadataResolver instance = new SpringMvcServiceRestMetadataResolver(ApplicationModel.defaultModel());
@Test
void testSupports() {

View File

@ -18,14 +18,13 @@ package org.apache.dubbo.metadata.annotation.processing.builder;
import org.apache.dubbo.common.lang.Prioritized;
import org.apache.dubbo.metadata.definition.model.TypeDefinition;
import org.apache.dubbo.rpc.model.ApplicationModel;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.type.TypeMirror;
import java.util.Map;
import static org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader;
/**
* A class builds the instance of {@link TypeDefinition}
*
@ -57,7 +56,8 @@ public interface TypeDefinitionBuilder<T extends TypeMirror> extends Prioritized
static TypeDefinition build(ProcessingEnvironment processingEnv, TypeMirror type, Map<String, TypeDefinition> typeCache) {
// Build by all instances of TypeDefinitionBuilder that were loaded By Java SPI
TypeDefinition typeDefinition = getExtensionLoader(TypeBuilder.class)
TypeDefinition typeDefinition = ApplicationModel.defaultModel()
.getExtensionLoader(TypeBuilder.class)
.getSupportedExtensionInstances()
.stream()
// load(TypeDefinitionBuilder.class, TypeDefinitionBuilder.class.getClassLoader())

View File

@ -21,6 +21,7 @@ import org.apache.dubbo.metadata.definition.model.MethodDefinition;
import org.apache.dubbo.metadata.rest.RequestMetadata;
import org.apache.dubbo.metadata.rest.RestMethodMetadata;
import org.apache.dubbo.metadata.rest.ServiceRestMetadata;
import org.apache.dubbo.rpc.model.ApplicationModel;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
@ -44,7 +45,6 @@ import static java.util.Collections.emptyList;
import static java.util.Collections.sort;
import static java.util.Optional.empty;
import static java.util.Optional.of;
import static org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader;
import static org.apache.dubbo.metadata.annotation.processing.builder.MethodDefinitionBuilder.build;
import static org.apache.dubbo.metadata.annotation.processing.util.LoggerUtils.info;
import static org.apache.dubbo.metadata.annotation.processing.util.MethodUtils.getOverrideMethod;
@ -268,7 +268,8 @@ public abstract class AbstractServiceRestMetadataResolver implements ServiceRest
// load(AnnotatedMethodParameterProcessor.class, AnnotatedMethodParameterProcessor.class.getClassLoader())
getExtensionLoader(AnnotatedMethodParameterProcessor.class)
ApplicationModel.defaultModel()
.getExtensionLoader(AnnotatedMethodParameterProcessor.class)
.getSupportedExtensionInstances()
.forEach(processor -> {
List<AnnotatedMethodParameterProcessor> processors =

View File

@ -18,6 +18,7 @@ package org.apache.dubbo.metadata.annotation.processing.rest;
import org.apache.dubbo.metadata.annotation.processing.AbstractServiceAnnotationProcessor;
import org.apache.dubbo.metadata.rest.ServiceRestMetadata;
import org.apache.dubbo.rpc.model.ApplicationModel;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
@ -28,7 +29,6 @@ import java.util.LinkedHashSet;
import java.util.Set;
import static javax.lang.model.util.ElementFilter.typesIn;
import static org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader;
import static org.apache.dubbo.metadata.annotation.processing.util.ServiceAnnotationUtils.isServiceAnnotationPresent;
/**
@ -49,7 +49,9 @@ public class ServiceRestMetadataAnnotationProcessor extends AbstractServiceAnnot
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
this.metadataProcessors = getExtensionLoader(ServiceRestMetadataResolver.class).getSupportedExtensionInstances();
this.metadataProcessors = ApplicationModel.defaultModel()
.getExtensionLoader(ServiceRestMetadataResolver.class)
.getSupportedExtensionInstances();
this.serviceRestMetadataWriter = new ServiceRestMetadataStorage(processingEnv);
}

View File

@ -20,6 +20,7 @@ import org.apache.dubbo.common.URL;
import org.apache.dubbo.monitor.Monitor;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.ProxyFactory;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol;
import org.junit.jupiter.api.BeforeEach;
@ -45,7 +46,7 @@ class DubboMonitorFactoryTest {
public void setUp() throws Exception {
initMocks(this);
this.dubboMonitorFactory = new DubboMonitorFactory();
this.dubboMonitorFactory.setProtocol(new DubboProtocol());
this.dubboMonitorFactory.setProtocol(new DubboProtocol(FrameworkModel.defaultModel()));
this.dubboMonitorFactory.setProxyFactory(proxyFactory);
}

View File

@ -28,6 +28,7 @@ import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.RpcInvocation;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol;
import com.alibaba.metrics.FastCompass;
@ -247,7 +248,7 @@ class MetricsFilterTest {
//ignore
}
}
Protocol protocol = new DubboProtocol();
Protocol protocol = new DubboProtocol(FrameworkModel.defaultModel());
// using host name might cause connection failure because multiple addresses might be configured to the same name!
url = URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":" + port + "/" + MetricsService.class.getName());
Invoker<MetricsService> invoker = protocol.refer(MetricsService.class, url);
@ -306,7 +307,7 @@ class MetricsFilterTest {
}
}
Protocol protocol = new DubboProtocol();
Protocol protocol = new DubboProtocol(FrameworkModel.defaultModel());
// using host name might cause connection failure because multiple addresses might be configured to the same name!
url = URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":" + port + "/" + MetricsService.class.getName());
Invoker<MetricsService> invoker = protocol.refer(MetricsService.class, url);

View File

@ -51,7 +51,7 @@ public class Exchangers {
}
public static ExchangeServer bind(URL url, ChannelHandler handler, Replier<?> replier) throws RemotingException {
return bind(url, new ExchangeHandlerDispatcher(replier, handler));
return bind(url, new ExchangeHandlerDispatcher(url.getOrDefaultFrameworkModel(), replier, handler));
}
public static ExchangeServer bind(String url, ExchangeHandler handler) throws RemotingException {
@ -90,7 +90,7 @@ public class Exchangers {
}
public static ExchangeClient connect(URL url, ChannelHandler handler, Replier<?> replier) throws RemotingException {
return connect(url, new ExchangeHandlerDispatcher(replier, handler));
return connect(url, new ExchangeHandlerDispatcher(url.getOrDefaultFrameworkModel(), replier, handler));
}
public static ExchangeClient connect(String url, ExchangeHandler handler) throws RemotingException {

View File

@ -20,6 +20,7 @@ import org.apache.dubbo.remoting.RemotingException;
import org.apache.dubbo.remoting.exchange.ExchangeChannel;
import org.apache.dubbo.remoting.exchange.ExchangeHandler;
import org.apache.dubbo.remoting.telnet.support.TelnetHandlerAdapter;
import org.apache.dubbo.rpc.model.FrameworkModel;
import java.util.concurrent.CompletableFuture;
@ -27,6 +28,9 @@ import java.util.concurrent.CompletableFuture;
* ExchangeHandlerAdapter
*/
public abstract class ExchangeHandlerAdapter extends TelnetHandlerAdapter implements ExchangeHandler {
public ExchangeHandlerAdapter(FrameworkModel frameworkModel) {
super(frameworkModel);
}
@Override
public CompletableFuture<Object> reply(ExchangeChannel channel, Object msg) throws RemotingException {

View File

@ -24,6 +24,7 @@ import org.apache.dubbo.remoting.exchange.ExchangeHandler;
import org.apache.dubbo.remoting.telnet.TelnetHandler;
import org.apache.dubbo.remoting.telnet.support.TelnetHandlerAdapter;
import org.apache.dubbo.remoting.transport.ChannelHandlerDispatcher;
import org.apache.dubbo.rpc.model.FrameworkModel;
import java.util.concurrent.CompletableFuture;
@ -39,21 +40,21 @@ public class ExchangeHandlerDispatcher implements ExchangeHandler {
private final TelnetHandler telnetHandler;
public ExchangeHandlerDispatcher() {
this(null, null);
this(FrameworkModel.defaultModel(), null, (ChannelHandler) null);
}
public ExchangeHandlerDispatcher(Replier<?> replier) {
this(replier, null);
this(FrameworkModel.defaultModel(), replier, (ChannelHandler) null);
}
public ExchangeHandlerDispatcher(ChannelHandler... handlers) {
this(null, handlers);
this(FrameworkModel.defaultModel(), null, handlers);
}
public ExchangeHandlerDispatcher(Replier<?> replier, ChannelHandler... handlers) {
public ExchangeHandlerDispatcher(FrameworkModel frameworkModel, Replier<?> replier, ChannelHandler... handlers) {
replierDispatcher = new ReplierDispatcher(replier);
handlerDispatcher = new ChannelHandlerDispatcher(handlers);
telnetHandler = new TelnetHandlerAdapter();
telnetHandler = new TelnetHandlerAdapter(frameworkModel);
}
public ExchangeHandlerDispatcher addChannelHandler(ChannelHandler handler) {

View File

@ -24,13 +24,18 @@ import org.apache.dubbo.remoting.Constants;
import org.apache.dubbo.remoting.RemotingException;
import org.apache.dubbo.remoting.telnet.TelnetHandler;
import org.apache.dubbo.remoting.transport.ChannelHandlerAdapter;
import org.apache.dubbo.rpc.model.FrameworkModel;
import static org.apache.dubbo.common.constants.CommonConstants.COMMA_SPLIT_PATTERN;
import static org.apache.dubbo.remoting.Constants.TELNET_KEY;
public class TelnetHandlerAdapter extends ChannelHandlerAdapter implements TelnetHandler {
private final ExtensionLoader<TelnetHandler> extensionLoader = ExtensionLoader.getExtensionLoader(TelnetHandler.class);
private final ExtensionLoader<TelnetHandler> extensionLoader;
public TelnetHandlerAdapter(FrameworkModel frameworkModel) {
extensionLoader = frameworkModel.getExtensionLoader(TelnetHandler.class);
}
@Override
public String telnet(Channel channel, String message) throws RemotingException {

View File

@ -23,6 +23,7 @@ import org.apache.dubbo.remoting.Channel;
import org.apache.dubbo.remoting.telnet.TelnetHandler;
import org.apache.dubbo.remoting.telnet.support.Help;
import org.apache.dubbo.remoting.telnet.support.TelnetUtils;
import org.apache.dubbo.rpc.model.FrameworkModel;
import java.util.ArrayList;
import java.util.List;
@ -36,12 +37,16 @@ import java.util.WeakHashMap;
@Help(parameter = "[command]", summary = "Show help.", detail = "Show help.")
public class HelpTelnetHandler implements TelnetHandler {
private final ExtensionLoader<TelnetHandler> extensionLoader = ExtensionLoader.getExtensionLoader(TelnetHandler.class);
private final ExtensionLoader<TelnetHandler> extensionLoader;
private static final String MAIN_HELP = "mainHelp";
private static Map<String, String> processedTable = new WeakHashMap<>();
public HelpTelnetHandler(FrameworkModel frameworkModel) {
extensionLoader = frameworkModel.getExtensionLoader(TelnetHandler.class);
}
@Override
public String telnet(Channel channel, String message) {
if (message.length() > 0) {

View File

@ -22,6 +22,7 @@ import org.apache.dubbo.common.serialize.support.DefaultSerializationSelector;
import org.apache.dubbo.remoting.exchange.ExchangeClient;
import org.apache.dubbo.remoting.exchange.Exchangers;
import org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.junit.jupiter.api.Test;
@ -100,6 +101,7 @@ class ChanelHandlerTest {
* @param url
*/
public PeformanceTestHandler(String url) {
super(FrameworkModel.defaultModel());
this.url = url;
}

View File

@ -22,6 +22,7 @@ import org.apache.dubbo.common.serialize.support.DefaultSerializationSelector;
import org.apache.dubbo.remoting.exchange.ExchangeClient;
import org.apache.dubbo.remoting.exchange.Exchangers;
import org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.junit.jupiter.api.Test;
@ -218,6 +219,9 @@ class PerformanceClientTest {
}
static class PeformanceTestHandler extends ExchangeHandlerAdapter {
public PeformanceTestHandler() {
super(FrameworkModel.defaultModel());
}
@Override
public void connected(Channel channel) throws RemotingException {

View File

@ -24,6 +24,7 @@ import org.apache.dubbo.remoting.exchange.ExchangeServer;
import org.apache.dubbo.remoting.exchange.Exchangers;
import org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter;
import org.apache.dubbo.remoting.transport.dispatcher.execution.ExecutionDispatcher;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.junit.jupiter.api.Test;
@ -82,7 +83,7 @@ class PerformanceServerTest {
ExchangeServer server = Exchangers.bind("exchange://0.0.0.0:" + port + "?transporter="
+ transporter + "&serialization="
+ serialization + "&threadpool=" + threadpool
+ "&threads=" + threads + "&iothreads=" + iothreads + "&buffer=" + buffer + "&channel.handler=" + channelHandler, new ExchangeHandlerAdapter() {
+ "&threads=" + threads + "&iothreads=" + iothreads + "&buffer=" + buffer + "&channel.handler=" + channelHandler, new ExchangeHandlerAdapter(FrameworkModel.defaultModel()) {
public String telnet(Channel channel, String message) throws RemotingException {
return "echo: " + message + "\r\ntelnet> ";
}
@ -106,7 +107,7 @@ class PerformanceServerTest {
private static ExchangeServer statTelnetServer(int port) throws Exception {
// Start server
ExchangeServer telnetserver = Exchangers.bind("exchange://0.0.0.0:" + port, new ExchangeHandlerAdapter() {
ExchangeServer telnetserver = Exchangers.bind("exchange://0.0.0.0:" + port, new ExchangeHandlerAdapter(FrameworkModel.defaultModel()) {
public String telnet(Channel channel, String message) throws RemotingException {
if (message.equals("help")) {
return "support cmd: \r\n\tstart \r\n\tstop \r\n\tshutdown \r\n\trestart times [alive] [sleep] \r\ntelnet>";

View File

@ -19,6 +19,7 @@ package org.apache.dubbo.remoting.telnet.support;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.remoting.Channel;
import org.apache.dubbo.remoting.telnet.support.command.HelpTelnetHandler;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@ -30,7 +31,7 @@ class HelpTelnetHandlerTest {
Channel channel = Mockito.mock(Channel.class);
Mockito.when(channel.getUrl()).thenReturn(URL.valueOf("dubbo://127.0.0.1:12345"));
HelpTelnetHandler helpTelnetHandler = new HelpTelnetHandler();
HelpTelnetHandler helpTelnetHandler = new HelpTelnetHandler(FrameworkModel.defaultModel());
// default output
String prompt = "Please input \"help [command]\" show detail.\r\n";
Assertions.assertTrue(helpTelnetHandler.telnet(channel, "").contains(prompt));

View File

@ -20,6 +20,7 @@ import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.remoting.Channel;
import org.apache.dubbo.remoting.RemotingException;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@ -38,7 +39,7 @@ class TelnetHandlerAdapterTest {
param.put("telnet", "status");
URL url = new URL("p1", "127.0.0.1", 12345, "path1", param);
Mockito.when(channel.getUrl()).thenReturn(url);
TelnetHandlerAdapter telnetHandlerAdapter = new TelnetHandlerAdapter();
TelnetHandlerAdapter telnetHandlerAdapter = new TelnetHandlerAdapter(FrameworkModel.defaultModel());
String message = "--no-prompt status ";
String expectedResult = "OK\r\n";

View File

@ -25,6 +25,7 @@ import org.apache.dubbo.remoting.RemotingException;
import org.apache.dubbo.remoting.RemotingServer;
import org.apache.dubbo.remoting.exchange.Exchangers;
import org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
@ -81,6 +82,10 @@ class ClientReconnectTest {
}
static class HandlerAdapter extends ExchangeHandlerAdapter {
public HandlerAdapter() {
super(FrameworkModel.defaultModel());
}
@Override
public void connected(Channel channel) throws RemotingException {
}

View File

@ -25,6 +25,7 @@ import org.apache.dubbo.remoting.RemotingException;
import org.apache.dubbo.remoting.RemotingServer;
import org.apache.dubbo.remoting.exchange.Exchangers;
import org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
@ -83,6 +84,10 @@ class ClientReconnectTest {
}
static class HandlerAdapter extends ExchangeHandlerAdapter {
public HandlerAdapter() {
super(FrameworkModel.defaultModel());
}
@Override
public void connected(Channel channel) throws RemotingException {
}

View File

@ -19,7 +19,6 @@ package org.apache.dubbo.rpc.protocol.dubbo;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.URLBuilder;
import org.apache.dubbo.common.config.ConfigurationUtils;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.url.component.ServiceConfigURL;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.common.utils.NetUtils;
@ -44,6 +43,7 @@ import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.RpcInvocation;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.apache.dubbo.rpc.model.ScopeModel;
import org.apache.dubbo.rpc.protocol.AbstractProtocol;
@ -111,132 +111,133 @@ public class DubboProtocol extends AbstractProtocol {
private final AtomicBoolean destroyed = new AtomicBoolean();
private final ExchangeHandler requestHandler = new ExchangeHandlerAdapter() {
private final ExchangeHandler requestHandler;
@Override
public CompletableFuture<Object> reply(ExchangeChannel channel, Object message) throws RemotingException {
public DubboProtocol(FrameworkModel frameworkModel) {
requestHandler = new ExchangeHandlerAdapter(frameworkModel) {
if (!(message instanceof Invocation)) {
throw new RemotingException(channel, "Unsupported request: "
+ (message == null ? null : (message.getClass().getName() + ": " + message))
+ ", channel: consumer: " + channel.getRemoteAddress() + " --> provider: " + channel.getLocalAddress());
}
@Override
public CompletableFuture<Object> reply(ExchangeChannel channel, Object message) throws RemotingException {
Invocation inv = (Invocation) message;
Invoker<?> invoker = getInvoker(channel, inv);
inv.setServiceModel(invoker.getUrl().getServiceModel());
// switch TCCL
if (invoker.getUrl().getServiceModel() != null) {
Thread.currentThread().setContextClassLoader(invoker.getUrl().getServiceModel().getClassLoader());
}
// need to consider backward-compatibility if it's a callback
if (Boolean.TRUE.toString().equals(inv.getObjectAttachmentWithoutConvert(IS_CALLBACK_SERVICE_INVOKE))) {
String methodsStr = invoker.getUrl().getParameters().get("methods");
boolean hasMethod = false;
if (methodsStr == null || !methodsStr.contains(",")) {
hasMethod = inv.getMethodName().equals(methodsStr);
} else {
String[] methods = methodsStr.split(",");
for (String method : methods) {
if (inv.getMethodName().equals(method)) {
hasMethod = true;
break;
if (!(message instanceof Invocation)) {
throw new RemotingException(channel, "Unsupported request: "
+ (message == null ? null : (message.getClass().getName() + ": " + message))
+ ", channel: consumer: " + channel.getRemoteAddress() + " --> provider: " + channel.getLocalAddress());
}
Invocation inv = (Invocation) message;
Invoker<?> invoker = getInvoker(channel, inv);
inv.setServiceModel(invoker.getUrl().getServiceModel());
// switch TCCL
if (invoker.getUrl().getServiceModel() != null) {
Thread.currentThread().setContextClassLoader(invoker.getUrl().getServiceModel().getClassLoader());
}
// need to consider backward-compatibility if it's a callback
if (Boolean.TRUE.toString().equals(inv.getObjectAttachmentWithoutConvert(IS_CALLBACK_SERVICE_INVOKE))) {
String methodsStr = invoker.getUrl().getParameters().get("methods");
boolean hasMethod = false;
if (methodsStr == null || !methodsStr.contains(",")) {
hasMethod = inv.getMethodName().equals(methodsStr);
} else {
String[] methods = methodsStr.split(",");
for (String method : methods) {
if (inv.getMethodName().equals(method)) {
hasMethod = true;
break;
}
}
}
if (!hasMethod) {
logger.warn(PROTOCOL_FAILED_REFER_INVOKER, "", "", new IllegalStateException("The methodName " + inv.getMethodName()
+ " not found in callback service interface ,invoke will be ignored."
+ " please update the api interface. url is:"
+ invoker.getUrl()) + " ,invocation is :" + inv);
return null;
}
}
if (!hasMethod) {
logger.warn(PROTOCOL_FAILED_REFER_INVOKER, "", "", new IllegalStateException("The methodName " + inv.getMethodName()
+ " not found in callback service interface ,invoke will be ignored."
+ " please update the api interface. url is:"
+ invoker.getUrl()) + " ,invocation is :" + inv);
RpcContext.getServiceContext().setRemoteAddress(channel.getRemoteAddress());
Result result = invoker.invoke(inv);
return result.thenApply(Function.identity());
}
@Override
public void received(Channel channel, Object message) throws RemotingException {
if (message instanceof Invocation) {
reply((ExchangeChannel) channel, message);
} else {
super.received(channel, message);
}
}
@Override
public void connected(Channel channel) throws RemotingException {
invoke(channel, ON_CONNECT_KEY);
}
@Override
public void disconnected(Channel channel) throws RemotingException {
if (logger.isDebugEnabled()) {
logger.debug("disconnected from " + channel.getRemoteAddress() + ",url:" + channel.getUrl());
}
invoke(channel, ON_DISCONNECT_KEY);
}
private void invoke(Channel channel, String methodKey) {
Invocation invocation = createInvocation(channel, channel.getUrl(), methodKey);
if (invocation != null) {
try {
if (Boolean.TRUE.toString().equals(invocation.getAttachment(STUB_EVENT_KEY))) {
tryToGetStubService(channel, invocation);
}
received(channel, invocation);
} catch (Throwable t) {
logger.warn(PROTOCOL_FAILED_REFER_INVOKER, "", "", "Failed to invoke event method " + invocation.getMethodName() + "(), cause: " + t.getMessage(), t);
}
}
}
private void tryToGetStubService(Channel channel, Invocation invocation) throws RemotingException {
try {
Invoker<?> invoker = getInvoker(channel, invocation);
} catch (RemotingException e) {
String serviceKey = serviceKey(
0,
(String) invocation.getObjectAttachmentWithoutConvert(PATH_KEY),
(String) invocation.getObjectAttachmentWithoutConvert(VERSION_KEY),
(String) invocation.getObjectAttachmentWithoutConvert(GROUP_KEY)
);
throw new RemotingException(channel, "The stub service[" + serviceKey + "] is not found, it may not be exported yet");
}
}
/**
* FIXME channel.getUrl() always binds to a fixed service, and this service is random.
* we can choose to use a common service to carry onConnect event if there's no easy way to get the specific
* service this connection is binding to.
* @param channel
* @param url
* @param methodKey
* @return
*/
private Invocation createInvocation(Channel channel, URL url, String methodKey) {
String method = url.getParameter(methodKey);
if (method == null || method.length() == 0) {
return null;
}
}
RpcContext.getServiceContext().setRemoteAddress(channel.getRemoteAddress());
Result result = invoker.invoke(inv);
return result.thenApply(Function.identity());
}
@Override
public void received(Channel channel, Object message) throws RemotingException {
if (message instanceof Invocation) {
reply((ExchangeChannel) channel, message);
} else {
super.received(channel, message);
}
}
@Override
public void connected(Channel channel) throws RemotingException {
invoke(channel, ON_CONNECT_KEY);
}
@Override
public void disconnected(Channel channel) throws RemotingException {
if (logger.isDebugEnabled()) {
logger.debug("disconnected from " + channel.getRemoteAddress() + ",url:" + channel.getUrl());
}
invoke(channel, ON_DISCONNECT_KEY);
}
private void invoke(Channel channel, String methodKey) {
Invocation invocation = createInvocation(channel, channel.getUrl(), methodKey);
if (invocation != null) {
try {
if (Boolean.TRUE.toString().equals(invocation.getAttachment(STUB_EVENT_KEY))) {
tryToGetStubService(channel, invocation);
}
received(channel, invocation);
} catch (Throwable t) {
logger.warn(PROTOCOL_FAILED_REFER_INVOKER, "", "", "Failed to invoke event method " + invocation.getMethodName() + "(), cause: " + t.getMessage(), t);
RpcInvocation invocation = new RpcInvocation(url.getServiceModel(), method, url.getParameter(INTERFACE_KEY), "", new Class<?>[0], new Object[0]);
invocation.setAttachment(PATH_KEY, url.getPath());
invocation.setAttachment(GROUP_KEY, url.getGroup());
invocation.setAttachment(INTERFACE_KEY, url.getParameter(INTERFACE_KEY));
invocation.setAttachment(VERSION_KEY, url.getVersion());
if (url.getParameter(STUB_EVENT_KEY, false)) {
invocation.setAttachment(STUB_EVENT_KEY, Boolean.TRUE.toString());
}
return invocation;
}
}
private void tryToGetStubService(Channel channel, Invocation invocation) throws RemotingException {
try {
Invoker<?> invoker = getInvoker(channel, invocation);
} catch (RemotingException e) {
String serviceKey = serviceKey(
0,
(String) invocation.getObjectAttachmentWithoutConvert(PATH_KEY),
(String) invocation.getObjectAttachmentWithoutConvert(VERSION_KEY),
(String) invocation.getObjectAttachmentWithoutConvert(GROUP_KEY)
);
throw new RemotingException(channel, "The stub service[" + serviceKey + "] is not found, it may not be exported yet");
}
}
/**
* FIXME channel.getUrl() always binds to a fixed service, and this service is random.
* we can choose to use a common service to carry onConnect event if there's no easy way to get the specific
* service this connection is binding to.
* @param channel
* @param url
* @param methodKey
* @return
*/
private Invocation createInvocation(Channel channel, URL url, String methodKey) {
String method = url.getParameter(methodKey);
if (method == null || method.length() == 0) {
return null;
}
RpcInvocation invocation = new RpcInvocation(url.getServiceModel(), method, url.getParameter(INTERFACE_KEY), "", new Class<?>[0], new Object[0]);
invocation.setAttachment(PATH_KEY, url.getPath());
invocation.setAttachment(GROUP_KEY, url.getGroup());
invocation.setAttachment(INTERFACE_KEY, url.getParameter(INTERFACE_KEY));
invocation.setAttachment(VERSION_KEY, url.getVersion());
if (url.getParameter(STUB_EVENT_KEY, false)) {
invocation.setAttachment(STUB_EVENT_KEY, Boolean.TRUE.toString());
}
return invocation;
}
};
public DubboProtocol() {
};
}
/**
@ -244,7 +245,7 @@ public class DubboProtocol extends AbstractProtocol {
*/
@Deprecated
public static DubboProtocol getDubboProtocol() {
return (DubboProtocol) ExtensionLoader.getExtensionLoader(Protocol.class).getExtension(DubboProtocol.NAME, false);
return (DubboProtocol) FrameworkModel.defaultModel().getExtensionLoader(Protocol.class).getExtension(DubboProtocol.NAME, false);
}
public static DubboProtocol getDubboProtocol(ScopeModel scopeModel) {

View File

@ -18,10 +18,10 @@ package org.apache.dubbo.rpc.protocol.dubbo.status;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.status.Status;
import org.apache.dubbo.common.status.StatusChecker;
import org.apache.dubbo.common.store.DataStore;
import org.apache.dubbo.rpc.model.ApplicationModel;
import java.util.Map;
import java.util.concurrent.ExecutorService;
@ -33,9 +33,15 @@ import java.util.concurrent.ThreadPoolExecutor;
@Activate
public class ThreadPoolStatusChecker implements StatusChecker {
private final ApplicationModel applicationModel;
public ThreadPoolStatusChecker(ApplicationModel applicationModel) {
this.applicationModel = applicationModel;
}
@Override
public Status check() {
DataStore dataStore = ExtensionLoader.getExtensionLoader(DataStore.class).getDefaultExtension();
DataStore dataStore = applicationModel.getExtensionLoader(DataStore.class).getDefaultExtension();
Map<String, Object> executors = dataStore.get(CommonConstants.EXECUTOR_SERVICE_COMPONENT_KEY);
StringBuilder msg = new StringBuilder();

View File

@ -25,6 +25,7 @@ import org.apache.dubbo.remoting.exchange.ExchangeClient;
import org.apache.dubbo.rpc.Exporter;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.ProxyFactory;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils;
import org.junit.jupiter.api.AfterAll;
@ -51,7 +52,7 @@ class DubboInvokerAvailableTest {
@BeforeEach
public void setUp() throws Exception {
protocol = new DubboProtocol();
protocol = new DubboProtocol(FrameworkModel.defaultModel());
}
@AfterAll

View File

@ -106,7 +106,7 @@ class DubboTelnetDecodeTest {
MockHandler mockHandler = new MockHandler(null,
new MultiMessageHandler(
new DecodeHandler(
new HeaderExchangeHandler(new ExchangeHandlerAdapter() {
new HeaderExchangeHandler(new ExchangeHandlerAdapter(FrameworkModel.defaultModel()) {
@Override
public CompletableFuture<Object> reply(ExchangeChannel channel, Object msg) {
if (checkDubboDecoded(msg)) {
@ -157,7 +157,7 @@ class DubboTelnetDecodeTest {
},
new MultiMessageHandler(
new DecodeHandler(
new HeaderExchangeHandler(new ExchangeHandlerAdapter() {
new HeaderExchangeHandler(new ExchangeHandlerAdapter(FrameworkModel.defaultModel()) {
@Override
public CompletableFuture<Object> reply(ExchangeChannel channel, Object msg) {
return getDefaultFuture();
@ -220,7 +220,7 @@ class DubboTelnetDecodeTest {
},
new MultiMessageHandler(
new DecodeHandler(
new HeaderExchangeHandler(new ExchangeHandlerAdapter() {
new HeaderExchangeHandler(new ExchangeHandlerAdapter(FrameworkModel.defaultModel()) {
@Override
public CompletableFuture<Object> reply(ExchangeChannel channel, Object msg) {
if (checkDubboDecoded(msg)) {
@ -292,7 +292,7 @@ class DubboTelnetDecodeTest {
},
new MultiMessageHandler(
new DecodeHandler(
new HeaderExchangeHandler(new ExchangeHandlerAdapter() {
new HeaderExchangeHandler(new ExchangeHandlerAdapter(FrameworkModel.defaultModel()) {
@Override
public CompletableFuture<Object> reply(ExchangeChannel channel, Object msg) {
return getDefaultFuture();
@ -359,7 +359,7 @@ class DubboTelnetDecodeTest {
MockHandler mockHandler = new MockHandler(null,
new MultiMessageHandler(
new DecodeHandler(
new HeaderExchangeHandler(new ExchangeHandlerAdapter() {
new HeaderExchangeHandler(new ExchangeHandlerAdapter(FrameworkModel.defaultModel()) {
@Override
public CompletableFuture<Object> reply(ExchangeChannel channel, Object msg) {
if (checkDubboDecoded(msg)) {
@ -430,7 +430,7 @@ class DubboTelnetDecodeTest {
},
new MultiMessageHandler(
new DecodeHandler(
new HeaderExchangeHandler(new ExchangeHandlerAdapter() {
new HeaderExchangeHandler(new ExchangeHandlerAdapter(FrameworkModel.defaultModel()) {
@Override
public CompletableFuture<Object> reply(ExchangeChannel channel, Object msg) {
if (checkDubboDecoded(msg)) {

View File

@ -20,6 +20,7 @@ import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.status.Status;
import org.apache.dubbo.common.store.DataStore;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@ -40,7 +41,7 @@ class ThreadPoolStatusCheckerTest {
dataStore.put(CommonConstants.EXECUTOR_SERVICE_COMPONENT_KEY, "8888", executorService1);
dataStore.put(CommonConstants.EXECUTOR_SERVICE_COMPONENT_KEY, "8889", executorService2);
ThreadPoolStatusChecker threadPoolStatusChecker = new ThreadPoolStatusChecker();
ThreadPoolStatusChecker threadPoolStatusChecker = new ThreadPoolStatusChecker(ApplicationModel.defaultModel());
Status status = threadPoolStatusChecker.check();
Assertions.assertEquals(status.getLevel(), Status.Level.WARN);
Assertions.assertEquals(status.getMessage(),

View File

@ -17,13 +17,13 @@
package org.apache.dubbo.rpc.protocol.grpc;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.threadpool.ThreadPool;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.SslConfig;
import org.apache.dubbo.config.context.ConfigManager;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.apache.dubbo.rpc.protocol.grpc.interceptors.ClientInterceptor;
import org.apache.dubbo.rpc.protocol.grpc.interceptors.GrpcConfigurator;
import org.apache.dubbo.rpc.protocol.grpc.interceptors.ServerInterceptor;
@ -236,7 +236,7 @@ public class GrpcOptionsUtils {
private static Optional<GrpcConfigurator> getConfigurator() {
// Give users the chance to customize ServerBuilder
Set<GrpcConfigurator> configurators = ExtensionLoader.getExtensionLoader(GrpcConfigurator.class)
Set<GrpcConfigurator> configurators = FrameworkModel.defaultModel().getExtensionLoader(GrpcConfigurator.class)
.getSupportedExtensionInstances();
if (CollectionUtils.isNotEmpty(configurators)) {
return Optional.of(configurators.iterator().next());

View File

@ -35,8 +35,6 @@ import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import static org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader;
/**
* Dubbo {@link HealthIndicator}
*
@ -63,7 +61,7 @@ public class DubboHealthIndicator extends AbstractHealthIndicator {
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
ExtensionLoader<StatusChecker> extensionLoader = getExtensionLoader(StatusChecker.class);
ExtensionLoader<StatusChecker> extensionLoader = applicationModel.getExtensionLoader(StatusChecker.class);
Map<String, String> statusCheckerNamesMap = resolveStatusCheckerNamesMap();

View File

@ -16,9 +16,9 @@
*/
package org.apache.dubbo.test.spring.context;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.config.spring.context.DubboSpringInitContext;
import org.apache.dubbo.config.spring.context.DubboSpringInitCustomizer;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.junit.jupiter.api.Assertions;
import org.springframework.beans.BeansException;
@ -61,7 +61,7 @@ public class MockSpringInitCustomizer implements DubboSpringInitCustomizer {
}
public static void checkCustomizer(ConfigurableApplicationContext applicationContext) {
Set<DubboSpringInitCustomizer> customizers = ExtensionLoader
Set<DubboSpringInitCustomizer> customizers = FrameworkModel.defaultModel()
.getExtensionLoader(DubboSpringInitCustomizer.class)
.getSupportedExtensionInstances();