|
@@ -1,22 +1,11 @@
|
|
|
package com.alibaba.otter.canal.client.adapter.support;
|
|
|
|
|
|
-import java.io.BufferedReader;
|
|
|
-import java.io.File;
|
|
|
-import java.io.FilenameFilter;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStreamReader;
|
|
|
+import java.io.*;
|
|
|
import java.net.MalformedURLException;
|
|
|
import java.net.URL;
|
|
|
import java.net.URLClassLoader;
|
|
|
import java.nio.file.Paths;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.Enumeration;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.NoSuchElementException;
|
|
|
-import java.util.Set;
|
|
|
-import java.util.TreeSet;
|
|
|
+import java.util.*;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
import java.util.concurrent.ConcurrentMap;
|
|
|
import java.util.regex.Pattern;
|
|
@@ -27,12 +16,13 @@ import org.slf4j.LoggerFactory;
|
|
|
/**
|
|
|
* SPI 类加载器
|
|
|
*
|
|
|
- * @author machengyuan 2018-8-19 下午11:30:49
|
|
|
+ * @author rewerma 2018-8-19 下午11:30:49
|
|
|
* @version 1.0.0
|
|
|
*/
|
|
|
public class ExtensionLoader<T> {
|
|
|
|
|
|
- private static final Logger logger = LoggerFactory.getLogger(ExtensionLoader.class);
|
|
|
+ private static final Logger logger = LoggerFactory
|
|
|
+ .getLogger(ExtensionLoader.class);
|
|
|
|
|
|
private static final String SERVICES_DIRECTORY = "META-INF/services/";
|
|
|
|
|
@@ -40,7 +30,8 @@ public class ExtensionLoader<T> {
|
|
|
|
|
|
private static final String DEFAULT_CLASSLOADER_POLICY = "internal";
|
|
|
|
|
|
- private static final Pattern NAME_SEPARATOR = Pattern.compile("\\s*[,]+\\s*");
|
|
|
+ private static final Pattern NAME_SEPARATOR = Pattern
|
|
|
+ .compile("\\s*[,]+\\s*");
|
|
|
|
|
|
private static final ConcurrentMap<Class<?>, ExtensionLoader<?>> EXTENSION_LOADERS = new ConcurrentHashMap<>();
|
|
|
|
|
@@ -271,7 +262,8 @@ public class ExtensionLoader<T> {
|
|
|
return instance;
|
|
|
} catch (Throwable t) {
|
|
|
throw new IllegalStateException("Extension instance(name: " + name + ", class: " + type
|
|
|
- + ") could not be instantiated: " + t.getMessage(), t);
|
|
|
+ + ") could not be instantiated: " + t.getMessage(),
|
|
|
+ t);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -279,8 +271,8 @@ public class ExtensionLoader<T> {
|
|
|
if (type == null) throw new IllegalArgumentException("Extension type == null");
|
|
|
if (name == null) throw new IllegalArgumentException("Extension name == null");
|
|
|
Class<?> clazz = getExtensionClasses().get(name);
|
|
|
- if (clazz == null) throw new IllegalStateException("No such extension \"" + name + "\" for " + type.getName()
|
|
|
- + "!");
|
|
|
+ if (clazz == null)
|
|
|
+ throw new IllegalStateException("No such extension \"" + name + "\" for " + type.getName() + "!");
|
|
|
return clazz;
|
|
|
}
|
|
|
|
|
@@ -342,8 +334,8 @@ public class ExtensionLoader<T> {
|
|
|
logger.info("extension classpath dir: " + dir);
|
|
|
File externalLibDir = new File(dir);
|
|
|
if (!externalLibDir.exists()) {
|
|
|
- externalLibDir = new File(File.separator + this.getJarDirectoryPath() + File.separator + "canal_client"
|
|
|
- + File.separator + "lib");
|
|
|
+ externalLibDir = new File(
|
|
|
+ File.separator + this.getJarDirectoryPath() + File.separator + "canal_client" + File.separator + "lib");
|
|
|
}
|
|
|
if (externalLibDir.exists()) {
|
|
|
File[] files = externalLibDir.listFiles(new FilenameFilter() {
|
|
@@ -495,12 +487,10 @@ public class ExtensionLoader<T> {
|
|
|
// Class.forName(line, true,
|
|
|
// classLoader);
|
|
|
if (!type.isAssignableFrom(clazz)) {
|
|
|
- throw new IllegalStateException("Error when load extension class(interface: "
|
|
|
- + type
|
|
|
- + ", class line: "
|
|
|
- + clazz.getName()
|
|
|
- + "), class "
|
|
|
- + clazz.getName()
|
|
|
+ throw new IllegalStateException(
|
|
|
+ "Error when load extension class(interface: " + type
|
|
|
+ + ", class line: " + clazz.getName()
|
|
|
+ + "), class " + clazz.getName()
|
|
|
+ "is not subtype of interface.");
|
|
|
} else {
|
|
|
try {
|
|
@@ -518,9 +508,9 @@ public class ExtensionLoader<T> {
|
|
|
extensionClasses.put(n, clazz);
|
|
|
} else if (c != clazz) {
|
|
|
cachedNames.remove(clazz);
|
|
|
- throw new IllegalStateException("Duplicate extension "
|
|
|
- + type.getName()
|
|
|
- + " name " + n + " on "
|
|
|
+ throw new IllegalStateException(
|
|
|
+ "Duplicate extension " + type.getName() + " name "
|
|
|
+ + n + " on "
|
|
|
+ c.getName() + " and "
|
|
|
+ clazz.getName());
|
|
|
}
|
|
@@ -530,12 +520,9 @@ public class ExtensionLoader<T> {
|
|
|
}
|
|
|
}
|
|
|
} catch (Throwable t) {
|
|
|
- IllegalStateException e = new IllegalStateException("Failed to load extension class(interface: "
|
|
|
- + type
|
|
|
- + ", class line: "
|
|
|
- + line
|
|
|
- + ") in "
|
|
|
- + url
|
|
|
+ IllegalStateException e = new IllegalStateException(
|
|
|
+ "Failed to load extension class(interface: " + type + ", class line: "
|
|
|
+ + line + ") in " + url
|
|
|
+ ", cause: "
|
|
|
+ t.getMessage(),
|
|
|
t);
|
|
@@ -550,13 +537,15 @@ public class ExtensionLoader<T> {
|
|
|
}
|
|
|
} catch (Throwable t) {
|
|
|
logger.error("Exception when load extension class(interface: " + type + ", class file: " + url
|
|
|
- + ") in " + url, t);
|
|
|
+ + ") in " + url,
|
|
|
+ t);
|
|
|
}
|
|
|
} // end of while urls
|
|
|
}
|
|
|
} catch (Throwable t) {
|
|
|
- logger.error("Exception when load extension class(interface: " + type + ", description file: " + fileName
|
|
|
- + ").", t);
|
|
|
+ logger.error(
|
|
|
+ "Exception when load extension class(interface: " + type + ", description file: " + fileName + ").",
|
|
|
+ t);
|
|
|
}
|
|
|
}
|
|
|
|