|
16 | 16 |
|
17 | 17 | package com.alibaba.fescar.common.loader;
|
18 | 18 |
|
| 19 | +import com.alibaba.fescar.common.executor.Initialize; |
| 20 | +import com.alibaba.fescar.common.util.CollectionUtils; |
| 21 | +import org.apache.commons.lang.ObjectUtils; |
| 22 | +import org.apache.commons.lang.StringUtils; |
| 23 | +import org.apache.commons.lang.exception.ExceptionUtils; |
| 24 | +import org.slf4j.Logger; |
| 25 | +import org.slf4j.LoggerFactory; |
| 26 | + |
19 | 27 | import java.io.BufferedReader;
|
20 | 28 | import java.io.IOException;
|
21 | 29 | import java.io.InputStreamReader;
|
|
28 | 36 | import java.util.Map;
|
29 | 37 | import java.util.concurrent.ConcurrentHashMap;
|
30 | 38 |
|
31 |
| -import org.apache.commons.lang.ObjectUtils; |
32 |
| -import org.apache.commons.lang.StringUtils; |
33 |
| -import org.apache.commons.lang.exception.ExceptionUtils; |
34 |
| -import org.slf4j.Logger; |
35 |
| -import org.slf4j.LoggerFactory; |
36 |
| - |
37 | 39 | /**
|
38 | 40 | * The type Enhanced service loader.
|
39 | 41 | *
|
@@ -100,6 +102,29 @@ public static <S> S load(Class<S> service, String activateName, ClassLoader load
|
100 | 102 | return loadFile(service, activateName, loader);
|
101 | 103 | }
|
102 | 104 |
|
| 105 | + /** |
| 106 | + * get all implements |
| 107 | + * |
| 108 | + * @param <S> the type parameter |
| 109 | + * @param service the service |
| 110 | + * @return list |
| 111 | + */ |
| 112 | + public static <S> List<S> loadAll(Class<S> service){ |
| 113 | + List<S> allInstances = new ArrayList<>(); |
| 114 | + List<Class> allClazzs = getAllExtensionClass(service); |
| 115 | + if(CollectionUtils.isEmpty(allClazzs)){ |
| 116 | + return allInstances; |
| 117 | + } |
| 118 | + try { |
| 119 | + for(Class clazz : allClazzs){ |
| 120 | + allInstances.add(initInstance(service, clazz)); |
| 121 | + } |
| 122 | + } catch (Throwable t) { |
| 123 | + throw new EnhancedServiceNotFoundException(t); |
| 124 | + } |
| 125 | + return allInstances; |
| 126 | + } |
| 127 | + |
103 | 128 | /**
|
104 | 129 | * 获取所有的扩展类,按照{@linkplain LoadLevel}定义的order顺序进行排序
|
105 | 130 | *
|
@@ -163,20 +188,18 @@ private static <S> S loadFile(Class<S> service, String activateName, ClassLoader
|
163 | 188 | "not found service provider for : " + service.getName() + "[" + activateName
|
164 | 189 | + "] and classloader : " + ObjectUtils.toString(loader));
|
165 | 190 | }
|
166 |
| - Class<?> extension = extensions.get(extensions.size() - 1); |
167 |
| - S result = service.cast(extension.newInstance()); |
| 191 | + Class<?> extension = extensions.get(extensions.size() - 1);// 最大的一个 |
| 192 | + S result = initInstance(service, extension); |
168 | 193 | if (!foundFromCache && LOGGER.isInfoEnabled()) {
|
169 |
| - LOGGER.info("load " + service.getSimpleName() + "[" + activateName + "] extension by class[" + extension |
170 |
| - .getName() + "]"); |
| 194 | + LOGGER.info("load " + service.getSimpleName() + "[" + activateName + "] extension by class[" + extension.getName() + "]"); |
171 | 195 | }
|
172 | 196 | return result;
|
173 | 197 | } catch (Throwable e) {
|
174 | 198 | if (e instanceof EnhancedServiceNotFoundException) {
|
175 |
| - throw (EnhancedServiceNotFoundException)e; |
| 199 | + throw (EnhancedServiceNotFoundException) e; |
176 | 200 | } else {
|
177 | 201 | throw new EnhancedServiceNotFoundException(
|
178 |
| - "not found service provider for : " + service.getName() + " caused by " + ExceptionUtils |
179 |
| - .getFullStackTrace(e)); |
| 202 | + "not found service provider for : " + service.getName() + " caused by " + ExceptionUtils.getFullStackTrace(e)); |
180 | 203 | }
|
181 | 204 | }
|
182 | 205 | }
|
@@ -263,6 +286,24 @@ private static void loadFile(Class<?> service, String dir, ClassLoader classLoad
|
263 | 286 | }
|
264 | 287 | }
|
265 | 288 |
|
| 289 | + /** |
| 290 | + * init instance |
| 291 | + * |
| 292 | + * @param <S> the type parameter |
| 293 | + * @param service the service |
| 294 | + * @param implClazz the impl clazz |
| 295 | + * @return s |
| 296 | + * @throws IllegalAccessException the illegal access exception |
| 297 | + * @throws InstantiationException the instantiation exception |
| 298 | + */ |
| 299 | + protected static <S> S initInstance(Class<S> service, Class implClazz) throws IllegalAccessException, InstantiationException { |
| 300 | + S s = service.cast(implClazz.newInstance()); |
| 301 | + if(s instanceof Initialize){ |
| 302 | + ((Initialize)s).init(); |
| 303 | + } |
| 304 | + return s; |
| 305 | + } |
| 306 | + |
266 | 307 | private static ClassLoader findClassLoader() {
|
267 | 308 | // 不能使用TCCL,在pandora容器中会导致无法加载plugin中的类
|
268 | 309 | return EnhancedServiceLoader.class.getClassLoader();
|
|
0 commit comments