diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/ExtensionLoader.java b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/ExtensionLoader.java index e47e19d07fa..3f2709e8621 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/ExtensionLoader.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/ExtensionLoader.java @@ -33,6 +33,7 @@ import java.io.InputStreamReader; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -71,9 +72,9 @@ public class ExtensionLoader { private static final Pattern NAME_SEPARATOR = Pattern.compile("\\s*[,]+\\s*"); - private static final ConcurrentMap, ExtensionLoader> EXTENSION_LOADERS = new ConcurrentHashMap, ExtensionLoader>(); + private static final ConcurrentMap, ExtensionLoader> EXTENSION_LOADERS = new ConcurrentHashMap<>(); - private static final ConcurrentMap, Object> EXTENSION_INSTANCES = new ConcurrentHashMap, Object>(); + private static final ConcurrentMap, Object> EXTENSION_INSTANCES = new ConcurrentHashMap<>(); // ============================== @@ -81,20 +82,20 @@ public class ExtensionLoader { private final ExtensionFactory objectFactory; - private final ConcurrentMap, String> cachedNames = new ConcurrentHashMap, String>(); + private final ConcurrentMap, String> cachedNames = new ConcurrentHashMap<>(); - private final Holder>> cachedClasses = new Holder>>(); + private final Holder>> cachedClasses = new Holder<>(); - private final Map cachedActivates = new ConcurrentHashMap(); - private final ConcurrentMap> cachedInstances = new ConcurrentHashMap>(); - private final Holder cachedAdaptiveInstance = new Holder(); + private final Map cachedActivates = new ConcurrentHashMap<>(); + private final ConcurrentMap> cachedInstances = new ConcurrentHashMap<>(); + private final Holder cachedAdaptiveInstance = new Holder<>(); private volatile Class cachedAdaptiveClass = null; private String cachedDefaultName; private volatile Throwable createAdaptiveInstanceError; private Set> cachedWrapperClasses; - private Map exceptions = new ConcurrentHashMap(); + private Map exceptions = new ConcurrentHashMap<>(); private ExtensionLoader(Class type) { this.type = type; @@ -111,11 +112,11 @@ public static ExtensionLoader getExtensionLoader(Class type) { throw new IllegalArgumentException("Extension type == null"); } if (!type.isInterface()) { - throw new IllegalArgumentException("Extension type (" + type + ") is not interface!"); + throw new IllegalArgumentException("Extension type (" + type + ") is not an interface!"); } if (!withExtensionAnnotation(type)) { throw new IllegalArgumentException("Extension type (" + type + - ") is not extension, because WITHOUT @" + SPI.class.getSimpleName() + " Annotation!"); + ") is not an extension, because it is NOT annotated with @" + SPI.class.getSimpleName() + "!"); } ExtensionLoader loader = (ExtensionLoader) EXTENSION_LOADERS.get(type); @@ -201,8 +202,8 @@ public List getActivateExtension(URL url, String key, String group) { * @see org.apache.dubbo.common.extension.Activate */ public List getActivateExtension(URL url, String[] values, String group) { - List exts = new ArrayList(); - List names = values == null ? new ArrayList(0) : Arrays.asList(values); + List exts = new ArrayList<>(); + List names = values == null ? new ArrayList<>(0) : Arrays.asList(values); if (!names.contains(Constants.REMOVE_VALUE_PREFIX + Constants.DEFAULT_KEY)) { getExtensionClasses(); for (Map.Entry entry : cachedActivates.entrySet()) { @@ -229,9 +230,9 @@ && isActive(activateValue, url)) { } } } - Collections.sort(exts, ActivateComparator.COMPARATOR); + exts.sort(ActivateComparator.COMPARATOR); } - List usrs = new ArrayList(); + List usrs = new ArrayList<>(); for (int i = 0; i < names.size(); i++) { String name = names.get(i); if (!name.startsWith(Constants.REMOVE_VALUE_PREFIX) @@ -297,12 +298,17 @@ public T getLoadedExtension(String name) { if (StringUtils.isEmpty(name)) { throw new IllegalArgumentException("Extension name == null"); } + Holder holder = getOrCreateHolder(name); + return (T) holder.get(); + } + + private Holder getOrCreateHolder(String name) { Holder holder = cachedInstances.get(name); if (holder == null) { - cachedInstances.putIfAbsent(name, new Holder()); + cachedInstances.putIfAbsent(name, new Holder<>()); holder = cachedInstances.get(name); } - return (T) holder.get(); + return holder; } /** @@ -313,7 +319,7 @@ public T getLoadedExtension(String name) { * @see #getSupportedExtensions() */ public Set getLoadedExtensions() { - return Collections.unmodifiableSet(new TreeSet(cachedInstances.keySet())); + return Collections.unmodifiableSet(new TreeSet<>(cachedInstances.keySet())); } public Object getLoadedAdaptiveExtensionInstances() { @@ -332,11 +338,7 @@ public T getExtension(String name) { if ("true".equals(name)) { return getDefaultExtension(); } - Holder holder = cachedInstances.get(name); - if (holder == null) { - cachedInstances.putIfAbsent(name, new Holder()); - holder = cachedInstances.get(name); - } + Holder holder = getOrCreateHolder(name); Object instance = holder.get(); if (instance == null) { synchronized (holder) { @@ -371,7 +373,7 @@ public boolean hasExtension(String name) { public Set getSupportedExtensions() { Map> clazzes = getExtensionClasses(); - return Collections.unmodifiableSet(new TreeSet(clazzes.keySet())); + return Collections.unmodifiableSet(new TreeSet<>(clazzes.keySet())); } /** @@ -680,8 +682,7 @@ private void loadDirectory(Map> extensionClasses, String dir, S private void loadResource(Map> extensionClasses, ClassLoader classLoader, java.net.URL resourceURL) { try { - BufferedReader reader = new BufferedReader(new InputStreamReader(resourceURL.openStream(), "utf-8")); - try { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(resourceURL.openStream(), StandardCharsets.UTF_8))) { String line; while ((line = reader.readLine()) != null) { final int ci = line.indexOf('#'); @@ -706,8 +707,6 @@ private void loadResource(Map> extensionClasses, ClassLoader cl } } } - } finally { - reader.close(); } } catch (Throwable t) { logger.error("Exception occurred when loading extension class (interface: " + @@ -733,7 +732,7 @@ private void loadClass(Map> extensionClasses, java.net.URL reso throw new 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]); @@ -798,7 +797,7 @@ private void cacheAdaptiveClass(Class clazz) { } /** - * cache wrapper class + * cache wrapper class *

* like: ProtocolFilterWrapper, ProtocolListenerWrapper */ @@ -810,7 +809,7 @@ private void cacheWrapperClass(Class clazz) { } /** - * test if clazz is a wrapper class + * test if clazz is a wrapper class *

* which has Constructor with given class type as its only argument */ diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoaderTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoaderTest.java index b0e878ce60e..12d8aa92af7 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoaderTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoaderTest.java @@ -87,7 +87,7 @@ public void test_getExtensionLoader_NotInterface() throws Exception { fail(); } catch (IllegalArgumentException expected) { assertThat(expected.getMessage(), - containsString("Extension type (class org.apache.dubbo.common.extension.ExtensionLoaderTest) is not interface")); + containsString("Extension type (class org.apache.dubbo.common.extension.ExtensionLoaderTest) is not an interface")); } } @@ -99,8 +99,8 @@ public void test_getExtensionLoader_NotSpiAnnotation() throws Exception { } catch (IllegalArgumentException expected) { assertThat(expected.getMessage(), allOf(containsString("org.apache.dubbo.common.extension.NoSpiExt"), - containsString("is not extension"), - containsString("WITHOUT @SPI Annotation"))); + containsString("is not an extension"), + containsString("NOT annotated with @SPI"))); } }