标签:custom param 定义 map lan def 源码 ext3 code
ExtensionLoader
@SPI("impl1")
public interface SimpleExt {
// @Adaptive example, do not specify a explicit key.
@Adaptive
String echo(URL url, String s);
@Adaptive({"key1", "key2"})
String yell(URL url, String s);
// no @Adaptive
String bang(URL url, int i);
}
@SPI("impl1")
public interface UseProtocolKeyExt {
// protocol key is the second
@Adaptive({"key1", "protocol"})
String echo(URL url, String s);
// protocol key is the first
@Adaptive({"protocol", "key2"})
String yell(URL url, String s);
}
public class SimpleExtImpl1 implements SimpleExt {
public String echo(URL url, String s) {
return "Ext1Impl1-echo";
}
public String yell(URL url, String s) {
return "Ext1Impl1-yell";
}
public String bang(URL url, int i) {
return "bang1";
}
}
public class SimpleExtImpl2 implements SimpleExt {
public String echo(URL url, String s) {
return "Ext1Impl2-echo";
}
public String yell(URL url, String s) {
return "Ext1Impl2-yell";
}
public String bang(URL url, int i) {
return "bang2";
}
}
public class SimpleExtImpl3 implements SimpleExt {
public String echo(URL url, String s) {
return "Ext1Impl3-echo";
}
public String yell(URL url, String s) {
return "Ext1Impl3-yell";
}
public String bang(URL url, int i) {
return "bang3";
}
}
public class UseProtocolKeyExtImpl1 implements UseProtocolKeyExt {
public String echo(URL url, String s) {
return "Ext3Impl1-echo";
}
public String yell(URL url, String s) {
return "Ext3Impl1-yell";
}
}
public class UseProtocolKeyExtImpl2 implements UseProtocolKeyExt {
public String echo(URL url, String s) {
return "Ext3Impl2-echo";
}
public String yell(URL url, String s) {
return "Ext3Impl2-yell";
}
}
public class UseProtocolKeyExtImpl3 implements UseProtocolKeyExt {
public String echo(URL url, String s) {
return "Ext3Impl3-echo";
}
public String yell(URL url, String s) {
return "Ext3Impl3-yell";
}
}
3.SPI资源路径
com.alibaba.dubbo.common.extensionloader.ext1.SimpleExt com.alibaba.dubbo.common.extensionloader.ext3.UseProtocolKeyExt
实现类配置
impl1=com.alibaba.dubbo.common.extensionloader.ext1.impl.SimpleExtImpl1#Hello World impl2=com.alibaba.dubbo.common.extensionloader.ext1.impl.SimpleExtImpl2 # Comment 2 impl3=com.alibaba.dubbo.common.extensionloader.ext1.impl.SimpleExtImpl3 # with head space
impl1=com.alibaba.dubbo.common.extensionloader.ext3.impl.UseProtocolKeyExtImpl1 impl2=com.alibaba.dubbo.common.extensionloader.ext3.impl.UseProtocolKeyExtImpl2 impl3=com.alibaba.dubbo.common.extensionloader.ext3.impl.UseProtocolKeyExtImpl3
4.测试方法
@Test
public void test_getAdaptiveExtension_defaultAdaptiveKey() throws Exception {
{
// #1
SimpleExt ext = ExtensionLoader.getExtensionLoader(SimpleExt.class).getAdaptiveExtension();
Map<String, String> map = new HashMap<String, String>();
URL url = new URL("p1", "1.2.3.4", 1010, "path1", map);
// #2
String echo = ext.echo(url, "haha");
assertEquals("Ext1Impl1-echo", echo);
}
{
#3
SimpleExt ext = ExtensionLoader.getExtensionLoader(SimpleExt.class).getAdaptiveExtension();
Map<String, String> map = new HashMap<String, String>();
map.put("simple.ext", "impl2");
URL url = new URL("p1", "1.2.3.4", 1010, "path1", map);
#4
String echo = ext.echo(url, "haha");
assertEquals("Ext1Impl2-echo", echo);
}
}
@Test
public void test_getAdaptiveExtension_customizeAdaptiveKey() throws Exception {
SimpleExt ext = ExtensionLoader.getExtensionLoader(SimpleExt.class).getAdaptiveExtension();
Map<String, String> map = new HashMap<String, String>();
map.put("key2", "impl2");
URL url = new URL("p1", "1.2.3.4", 1010, "path1", map);
#5
String echo = ext.yell(url, "haha");
assertEquals("Ext1Impl2-yell", echo);
#6
url = url.addParameter("key1", "impl3"); // note: URL is value‘s type
echo = ext.yell(url, "haha");
assertEquals("Ext1Impl3-yell", echo);
}
@Test
public void test_getAdaptiveExtension_protocolKey() throws Exception {
#1
UseProtocolKeyExt ext = ExtensionLoader.getExtensionLoader(UseProtocolKeyExt.class).getAdaptiveExtension();
{
#2
String echo = ext.echo(URL.valueOf("1.2.3.4:20880"), "s");
assertEquals("Ext3Impl1-echo", echo); // default value
#3
Map<String, String> map = new HashMap<String, String>();
URL url = new URL("impl3", "1.2.3.4", 1010, "path1", map);
echo = ext.echo(url, "s");
assertEquals("Ext3Impl3-echo", echo); // use 2nd key, protocol
#4
url = url.addParameter("key1", "impl2");
echo = ext.echo(url, "s");
assertEquals("Ext3Impl2-echo", echo); // use 1st key, key1
}
{
Map<String, String> map = new HashMap<String, String>();
URL url = new URL(null, "1.2.3.4", 1010, "path1", map);
#5
String yell = ext.yell(url, "s");
assertEquals("Ext3Impl1-yell", yell); // default value
#6
url = url.addParameter("key2", "impl2"); // use 2nd key, key2
yell = ext.yell(url, "s");
assertEquals("Ext3Impl2-yell", yell);
#7
url = url.setProtocol("impl3"); // use 1st key, protocol
yell = ext.yell(url, "d");
assertEquals("Ext3Impl3-yell", yell);
}
}
package com.alibaba.dubbo.common.extensionloader.ext1;
import com.alibaba.dubbo.common.extension.ExtensionLoader;
public class SimpleExt$Adaptive implements com.alibaba.dubbo.common.extensionloader.ext1.SimpleExt {
public java.lang.String echo(com.alibaba.dubbo.common.URL arg0,
java.lang.String arg1) {
if (arg0 == null) {
throw new IllegalArgumentException("url == null");
}
com.alibaba.dubbo.common.URL url = arg0;
String extName = url.getParameter("simple.ext", "impl1");
if (extName == null) {
throw new IllegalStateException(
"Fail to get extension(com.alibaba.dubbo.common.extensionloader.ext1.SimpleExt) name from url(" +
url.toString() + ") use keys([simple.ext])");
}
com.alibaba.dubbo.common.extensionloader.ext1.SimpleExt extension = (com.alibaba.dubbo.common.extensionloader.ext1.SimpleExt) ExtensionLoader.getExtensionLoader(com.alibaba.dubbo.common.extensionloader.ext1.SimpleExt.class)
.getExtension(extName);
return (extension.echo(arg0, arg1));
}
public java.lang.String yell(com.alibaba.dubbo.common.URL arg0,
java.lang.String arg1) {
if (arg0 == null) {
throw new IllegalArgumentException("url == null");
}
com.alibaba.dubbo.common.URL url = arg0;
String extName = url.getParameter("key1",
url.getParameter("key2", "impl1"));
if (extName == null) {
throw new IllegalStateException(
"Fail to get extension(com.alibaba.dubbo.common.extensionloader.ext1.SimpleExt) name from url(" +
url.toString() + ") use keys([key1, key2])");
}
com.alibaba.dubbo.common.extensionloader.ext1.SimpleExt extension = (com.alibaba.dubbo.common.extensionloader.ext1.SimpleExt) ExtensionLoader.getExtensionLoader(com.alibaba.dubbo.common.extensionloader.ext1.SimpleExt.class)
.getExtension(extName);
return (extension.yell(arg0, arg1));
}
public java.lang.String bang(com.alibaba.dubbo.common.URL arg0, int arg1) {
throw new UnsupportedOperationException(
"method public abstract java.lang.String com.alibaba.dubbo.common.extensionloader.ext1.SimpleExt.bang(com.alibaba.dubbo.common.URL,int) of interface com.alibaba.dubbo.common.extensionloader.ext1.SimpleExt is not adaptive method!");
}
}
package com.alibaba.dubbo.common.extensionloader.ext3;
import com.alibaba.dubbo.common.extension.ExtensionLoader;
public class UseProtocolKeyExt$Adaptive implements com.alibaba.dubbo.common.extensionloader.ext3.UseProtocolKeyExt {
public java.lang.String echo( com.alibaba.dubbo.common.URL arg0, java.lang.String arg1 )
{
if ( arg0 == null )
throw new IllegalArgumentException( "url == null" );
com.alibaba.dubbo.common.URL url = arg0;
String extName = url.getParameter( "key1", (url.getProtocol() == null ? "impl1" : url.getProtocol() ) );
if ( extName == null )
throw new IllegalStateException( "Fail to get extension(com.alibaba.dubbo.common.extensionloader.ext3.UseProtocolKeyExt) name from url(" + url.toString() + ") use keys([key1, protocol])" );
com.alibaba.dubbo.common.extensionloader.ext3.UseProtocolKeyExt extension =
(com.alibaba.dubbo.common.extensionloader.ext3.UseProtocolKeyExt)ExtensionLoader.getExtensionLoader( com.alibaba.dubbo.common.extensionloader.ext3.UseProtocolKeyExt.class ).getExtension( extName );
return(extension.echo( arg0, arg1 ) );
}
public java.lang.String yell( com.alibaba.dubbo.common.URL arg0, java.lang.String arg1 )
{
if ( arg0 == null )
throw new IllegalArgumentException( "url == null" );
com.alibaba.dubbo.common.URL url = arg0;
String extName = url.getProtocol() == null ? (url.getParameter( "key2", "impl1" ) ) : url.getProtocol();
if ( extName == null )
throw new IllegalStateException( "Fail to get extension(com.alibaba.dubbo.common.extensionloader.ext3.UseProtocolKeyExt) name from url(" + url.toString() + ") use keys([protocol, key2])" );
com.alibaba.dubbo.common.extensionloader.ext3.UseProtocolKeyExt extension = (com.alibaba.dubbo.common.extensionloader.ext3.UseProtocolKeyExt)ExtensionLoader.
getExtensionLoader( com.alibaba.dubbo.common.extensionloader.ext3.UseProtocolKeyExt.class ).getExtension( extName );
return(extension.yell( arg0, arg1 ) );
}
}
标签:custom param 定义 map lan def 源码 ext3 code
原文地址:https://www.cnblogs.com/Yiran583/p/11926896.html