1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
private void readExtension0(Map<String, Class<?>> extName2Class, Map<String, Map<String, String>> name2Attributes, Map<String, Class<? extends T>> name2Wrapper, ClassLoader classLoader, URL url) { BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8")); String line; while ((line = reader.readLine()) != null) { String config = line;
final int ci = config.indexOf('#'); if (ci >= 0) config = config.substring(0, ci); config = config.trim(); if (config.length() == 0) continue;
try { String name = null; String body = null; String attribute = null; int i = config.indexOf('='); if (i > 0) { name = config.substring(0, i).trim(); body = config.substring(i + 1).trim(); } if (name == null || name.length() == 0) { throw new IllegalStateException( "missing extension name, config value: " + config); } int j = config.indexOf("(", i); if (j > 0) { if (config.charAt(config.length() - 1) != ')') { throw new IllegalStateException( "missing ')' of extension attribute!"); } body = config.substring(i + 1, j).trim(); attribute = config.substring(j + 1, config.length() - 1); }
Class<? extends T> clazz = Class.forName(body, true, classLoader).asSubclass(type); if (!type.isAssignableFrom(clazz)) { throw new IllegalStateException("Error when load extension class(interface: " + type.getName() + ", class line: " + clazz.getName() + "), class " + clazz.getName() + "is not subtype of interface."); } if (name.startsWith(PREFIX_ADAPTIVE_CLASS)) { if (adaptiveClass == null) { adaptiveClass = clazz; } else if (!adaptiveClass.equals(clazz)) { throw new IllegalStateException("More than 1 adaptive class found: " + adaptiveClass.getClass().getName() + ", " + clazz.getClass().getName()); } } else { final boolean isWrapper = name.startsWith(PREFIX_WRAPPER_CLASS); if (isWrapper) name = name.substring(PREFIX_WRAPPER_CLASS.length());
String[] nameList = NAME_SEPARATOR.split(name); for (String n : nameList) { if (!isValidExtName(n)) { throw new IllegalStateException("name(" + n + ") of extension " + type.getName() + "is invalid!"); }
if (isWrapper) { try { clazz.getConstructor(type); name2Wrapper.put(name, clazz); } catch (NoSuchMethodException e) { throw new IllegalStateException("wrapper class(" + clazz + ") has NO copy constructor!", e); } } else { try { clazz.getConstructor(); } catch (NoSuchMethodException e) { throw new IllegalStateException("extension class(" + clazz + ") has NO default constructor!", e); } if (extName2Class.containsKey(n)) { if (extName2Class.get(n) != clazz) { throw new IllegalStateException("Duplicate extension " + type.getName() + " name " + n + " on " + clazz.getName() + " and " + clazz.getName()); } } else { extName2Class.put(n, clazz); } name2Attributes.put(n, parseExtAttribute(attribute));
if (!extClass2Name.containsKey(clazz)) { extClass2Name.put(clazz, n); } } } } } catch (Throwable t) { IllegalStateException e = new IllegalStateException("Failed to load config line(" + line + ") of config file(" + url + ") for extension(" + type.getName() + "), cause: " + t.getMessage(), t); logger.warn("", e); extClassLoadExceptions.put(line, e); } } } catch (Throwable t) { logger.error("Exception when load extension class(interface: " + type.getName() + ", class file: " + url + ") in " + url, t); } finally { if (reader != null) { try { reader.close(); } catch (Throwable t) { } } } }
|