privatevoidloadClass(Map<String, Class<?>> extensionClasses, java.net.URL resourceURL, Class<?> clazz, String name)throws NoSuchMethodException { if (!type.isAssignableFrom(clazz)) { thrownew IllegalStateException("Error occurred when loading extension class (interface: " + type + ", class line: " + clazz.getName() + "), class " + clazz.getName() + " is not subtype of interface."); } if (clazz.isAnnotationPresent(Adaptive.class)) { //缓存Adaptive的class cacheAdaptiveClass(clazz); } elseif (isWrapperClass(clazz)) { //缓存wrapper class //这里会把clazz缓存在cachedWrapperClasses中 //可否还记得前面实例化dubbo对应的DubboProtocol后,接着使用了cachedWrapperClasses对其进行包装 cacheWrapperClass(clazz); } else { clazz.getConstructor(); if (StringUtils.isEmpty(name)) { name = findAnnotationName(clazz); if (name.length() == 0) { thrownew IllegalStateException("No such extension name for the class " + clazz.getName() + " in the config " + resourceURL); } }
String[] names = NAME_SEPARATOR.split(name); if (ArrayUtils.isNotEmpty(names)) { cacheActivateClass(clazz, names[0]); for (String n : names) { cacheName(clazz, n); saveInExtensionClass(extensionClasses, clazz, name); } } } }
publicintport(){ thrownew UnsupportedOperationException( "The method public abstract int com.young.dubbo.spi.AdaptiveProtocol.port() of interface com.young.dubbo.spi.AdaptiveProtocol is not adaptive method!"); }
public java.lang.String method1(org.apache.dubbo.common.URL arg0){ if (arg0 == null) { thrownew IllegalArgumentException("url == null"); } org.apache.dubbo.common.URL url = arg0; //关键点,这里会去url里查找指定key的value,这个value就是services文件里的key,它对应的实现类就是我们要找的 //url里如果没有这个参数,默认使用@SPI中指定的值 String extName = url.getParameter("method1", "Default"); if (extName == null) { thrownew IllegalStateException( "Failed to get extension (com.young.dubbo.spi.AdaptiveProtocol) name from url (" + url.toString() + ") use keys([method1])"); } //这里加载了真正的extension com.young.dubbo.spi.AdaptiveProtocol extension = (com.young.dubbo.spi.AdaptiveProtocol) ExtensionLoader .getExtensionLoader(com.young.dubbo.spi.AdaptiveProtocol.class).getExtension(extName); return extension.method1(arg0); }
public java.lang.String method2(org.apache.dubbo.common.URL arg0){ if (arg0 == null) { thrownew IllegalArgumentException("url == null"); } org.apache.dubbo.common.URL url = arg0; String extName = url.getParameter("method2", "Default"); if (extName == null) { thrownew IllegalStateException( "Failed to get extension (com.young.dubbo.spi.AdaptiveProtocol) name from url (" + url.toString() + ") use keys([method2])"); } com.young.dubbo.spi.AdaptiveProtocol extension = (com.young.dubbo.spi.AdaptiveProtocol) ExtensionLoader .getExtensionLoader(com.young.dubbo.spi.AdaptiveProtocol.class).getExtension(extName); return extension.method2(arg0); }
public java.lang.String method3(org.apache.dubbo.common.URL arg0){ if (arg0 == null) { thrownew IllegalArgumentException("url == null"); } org.apache.dubbo.common.URL url = arg0; //这个key有点特殊,当@Adaptive没有指定任何key时,dubbo会根据接口名按照驼峰命名法将其拆开组装成一个key String extName = url.getParameter("adaptive.protocol", "Default"); if (extName == null) { thrownew IllegalStateException( "Failed to get extension (com.young.dubbo.spi.AdaptiveProtocol) name from url (" + url.toString() + ") use keys([adaptive.protocol])"); } com.young.dubbo.spi.AdaptiveProtocol extension = (com.young.dubbo.spi.AdaptiveProtocol) ExtensionLoader .getExtensionLoader(com.young.dubbo.spi.AdaptiveProtocol.class).getExtension(extName); return extension.method3(arg0); } }