Skip to content

Commit

Permalink
Support no interface (#14639)
Browse files Browse the repository at this point in the history
Co-authored-by: Albumen Kevin <jhq0812@gmail.com>
  • Loading branch information
oxsean and AlbumenJ committed Sep 6, 2024
1 parent 96bf731 commit ca6909a
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,7 @@ protected void processExtraRefresh(String preferredPrefix, InmemoryConfiguration
return;
}

if (!interfaceClass.isInterface() && !canSkipInterfaceCheck()) {
throw new IllegalStateException(interfaceName + " is not an interface");
}
checkInterface();

// Auto create MethodConfig/ArgumentConfig according to config props
Map<String, String> configProperties = subPropsConfiguration.getProperties();
Expand Down Expand Up @@ -377,13 +375,9 @@ && hasArgumentConfigProps(configProperties, methodConfig.getName(), i)) {

/**
* it is used for skipping the check of interface since dubbo 3.2
* rest protocol allow the service is implement class
*
* @return
* rest and triple protocol allow the service is implement class
*/
protected boolean canSkipInterfaceCheck() {
return false;
}
protected void checkInterface() {}

protected boolean verifyMethodConfig(
MethodConfig methodConfig, Class<?> interfaceClass, boolean ignoreInvalidMethodConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import java.beans.Transient;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executor;

Expand Down Expand Up @@ -281,7 +281,7 @@ public ProtocolConfig getProtocol() {
}

public void setProtocol(ProtocolConfig protocol) {
setProtocols(new ArrayList<>(Arrays.asList(protocol)));
setProtocols(new ArrayList<>(Collections.singletonList(protocol)));
}

@Parameter(excluded = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
*
* @export
*/
@SuppressWarnings({"rawtypes", "deprecation"})
public abstract class ServiceConfigBase<T> extends AbstractServiceConfig {

private static final long serialVersionUID = 3033787999037024738L;
Expand Down Expand Up @@ -109,6 +110,12 @@ public ServiceConfigBase(ModuleModel moduleModel, Service service) {
setMethods(MethodConfig.constructMethodConfig(service.methods()));
}

@Override
public void setProtocols(List<? extends ProtocolConfig> protocols) {
super.setProtocols(protocols);
checkInterface();
}

@Override
protected void postProcessAfterScopeModelChanged(ScopeModel oldScopeModel, ScopeModel newScopeModel) {
super.postProcessAfterScopeModelChanged(oldScopeModel, newScopeModel);
Expand Down Expand Up @@ -215,7 +222,7 @@ private boolean notHasSelfProtocolProperty() {
}

protected void completeCompoundConfigs() {
super.completeCompoundConfigs(provider);
completeCompoundConfigs(provider);
if (provider != null) {
if (notHasSelfProtocolProperty()) {
setProtocols(provider.getProtocols());
Expand Down Expand Up @@ -269,7 +276,7 @@ public Class<?> getInterfaceClass() {
}
try {
if (StringUtils.isNotEmpty(interfaceName)) {
this.interfaceClass = Class.forName(
interfaceClass = Class.forName(
interfaceName, true, Thread.currentThread().getContextClassLoader());
}
} catch (ClassNotFoundException t) {
Expand All @@ -279,7 +286,6 @@ public Class<?> getInterfaceClass() {
}

/**
* @param interfaceClass
* @see #setInterface(Class)
* @deprecated
*/
Expand All @@ -288,36 +294,33 @@ public void setInterfaceClass(Class<?> interfaceClass) {
}

public void setInterface(Class<?> interfaceClass) {
// rest protocol allow set impl class
if (interfaceClass != null && !interfaceClass.isInterface() && !canSkipInterfaceCheck()) {
throw new IllegalStateException("The interface class " + interfaceClass + " is not a interface!");
}
this.interfaceClass = interfaceClass;
checkInterface();
setInterface(interfaceClass == null ? null : interfaceClass.getName());
if (getInterfaceClassLoader() == null) {
setInterfaceClassLoader(interfaceClass == null ? null : interfaceClass.getClassLoader());
}
}

@Override
public boolean canSkipInterfaceCheck() {
// for multipart protocol so for each contain
public void checkInterface() {
if (interfaceClass == null || interfaceClass.isInterface()) {
return;
}
List<ProtocolConfig> protocols = getProtocols();

if (protocols == null) {
return false;
if (CollectionUtils.isEmpty(protocols)) {
return;
}

for (ProtocolConfig protocol : protocols) {
String name = protocol.getName();
if (CommonConstants.TRIPLE.equals(name) && Boolean.TRUE.equals(protocol.isNoInterfaceSupport())) {
return true;
return;
}
if (Constants.REST_PROTOCOL.equals(name)) {
return true;
return;
}
}
return false;
throw new IllegalStateException("The interface class " + interfaceClass + " is not a interface!");
}

@Transient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.dubbo.config;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.config.api.DemoService;
import org.apache.dubbo.config.api.Greeting;
Expand Down Expand Up @@ -86,6 +87,7 @@
import static org.mockito.Mockito.withSettings;

class ServiceConfigTest {

private Protocol protocolDelegate = Mockito.mock(Protocol.class);
private Registry registryDelegate = Mockito.mock(Registry.class);
private Exporter exporter = Mockito.mock(Exporter.class);
Expand Down Expand Up @@ -261,7 +263,9 @@ void testInterfaceClass() throws Exception {
@Test
void testInterface1() throws Exception {
Assertions.assertThrows(IllegalStateException.class, () -> {
ProtocolConfig protocolConfig = new ProtocolConfig(CommonConstants.TRIPLE);
ServiceConfig<DemoService> service = new ServiceConfig<>();
service.setProtocol(protocolConfig);
service.setInterface(DemoServiceImpl.class);
});
}
Expand All @@ -273,6 +277,16 @@ void testInterface2() throws Exception {
assertThat(service.getInterface(), equalTo(DemoService.class.getName()));
}

@Test
void testNoInterfaceSupport() throws Exception {
ProtocolConfig protocolConfig = new ProtocolConfig(CommonConstants.TRIPLE);
protocolConfig.setNoInterfaceSupport(true);
ServiceConfig<DemoService> service = new ServiceConfig<>();
service.setProtocol(protocolConfig);
service.setInterface(DemoServiceImpl.class);
assertThat(service.getInterface(), equalTo(DemoServiceImpl.class.getName()));
}

@Test
void testProvider() throws Exception {
ServiceConfig service = new ServiceConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ protected void initServConf() {
servConf.setProvider(provConf);

servConf.setRef(demoService);
servConf.setInterfaceClass(DemoService.class);
servConf.setInterface(DemoService.class);

methodConfForService.setName("sayName");
regConfForService.setAddress("127.0.0.1:9090");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,14 @@ public static String resolveInterfaceName(Map<String, Object> attributes, Class<
Class<?>[] allInterfaces = getAllInterfacesForClass(defaultInterfaceClass);
if (allInterfaces.length > 0) {
interfaceClass = allInterfaces[0];
} else {
interfaceClass = defaultInterfaceClass;
}
}

Assert.notNull(
interfaceClass, "@Service interfaceClass() or interfaceName() or interface class must be present!");
Assert.isTrue(interfaceClass.isInterface(), "The annotated type must be an interface!");
// Assert.isTrue(interfaceClass.isInterface(), "The annotated type must be an interface!");
return interfaceClass.getName();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,7 @@ protected void initializeAltSvc(URL url) {}
@Override
public final void onData(MESSAGE message) {
if (executor == null) {
try {
Throwable t = new NullPointerException("Executor not initialized");
logError(t);
onError(message, t);
} finally {
onDataFinally(message);
}
onDataFinally(message);
return;
}
executor.execute(() -> {
Expand Down

0 comments on commit ca6909a

Please sign in to comment.