3737/**
3838 * The type Enhanced service loader.
3939 *
40- * @Author: jimin.jm @alibaba-inc.com
41- * @Project: fescar -all
42- * @DateTime: 2018 /10/10 14:28
43- * @FileName: EnhancedServiceLoader
44- * @Description:
40+ * @author: jimin.jm @alibaba-inc.com
41+ * @date: 2018/10/10
4542 */
4643public class EnhancedServiceLoader {
4744 private static final Logger LOGGER = LoggerFactory .getLogger (EnhancedServiceLoader .class );
@@ -98,7 +95,8 @@ public static <S> S load(Class<S> service, String activateName) throws EnhancedS
9895 * @return s
9996 * @throws EnhancedServiceNotFoundException the enhanced service not found exception
10097 */
101- public static <S > S load (Class <S > service , String activateName , ClassLoader loader ) throws EnhancedServiceNotFoundException {
98+ public static <S > S load (Class <S > service , String activateName , ClassLoader loader )
99+ throws EnhancedServiceNotFoundException {
102100 return loadFile (service , activateName , loader );
103101 }
104102
@@ -151,7 +149,7 @@ private static <S> S loadFile(Class<S> service, String activateName, ClassLoader
151149 for (int i = 0 ; i < extensions .size (); i ++) {
152150 Class clz = extensions .get (i );
153151 @ SuppressWarnings ("unchecked" )
154- LoadLevel activate = (LoadLevel ) clz .getAnnotation (LoadLevel .class );
152+ LoadLevel activate = (LoadLevel )clz .getAnnotation (LoadLevel .class );
155153 if (activate != null && activateName .equals (activate .name ())) {
156154 activateExtensions .add (clz );
157155 }
@@ -161,21 +159,24 @@ private static <S> S loadFile(Class<S> service, String activateName, ClassLoader
161159 }
162160
163161 if (extensions .isEmpty ()) {
164- throw new EnhancedServiceNotFoundException ("not found service provider for : " + service .getName () + "[" + activateName
165- + "] and classloader : " + ObjectUtils .toString (loader ));
162+ throw new EnhancedServiceNotFoundException (
163+ "not found service provider for : " + service .getName () + "[" + activateName
164+ + "] and classloader : " + ObjectUtils .toString (loader ));
166165 }
167- Class <?> extension = extensions .get (extensions .size () - 1 );// 最大的一个
166+ Class <?> extension = extensions .get (extensions .size () - 1 );
168167 S result = service .cast (extension .newInstance ());
169168 if (!foundFromCache && LOGGER .isInfoEnabled ()) {
170- LOGGER .info ("load " + service .getSimpleName () + "[" + activateName + "] extension by class[" + extension .getName () + "]" );
169+ LOGGER .info ("load " + service .getSimpleName () + "[" + activateName + "] extension by class[" + extension
170+ .getName () + "]" );
171171 }
172172 return result ;
173173 } catch (Throwable e ) {
174174 if (e instanceof EnhancedServiceNotFoundException ) {
175- throw (EnhancedServiceNotFoundException ) e ;
175+ throw (EnhancedServiceNotFoundException )e ;
176176 } else {
177177 throw new EnhancedServiceNotFoundException (
178- "not found service provider for : " + service .getName () + " caused by " + ExceptionUtils .getFullStackTrace (e ));
178+ "not found service provider for : " + service .getName () + " caused by " + ExceptionUtils
179+ .getFullStackTrace (e ));
179180 }
180181 }
181182 }
@@ -193,17 +194,15 @@ private static <S> List<Class> findAllExtensionClass(Class<S> service, String ac
193194 if (extensions .isEmpty ()) {
194195 return extensions ;
195196 }
196-
197- // 做一下排序
198197 Collections .sort (extensions , new Comparator <Class >() {
199198 @ Override
200199 public int compare (Class c1 , Class c2 ) {
201200 Integer o1 = 0 ;
202201 Integer o2 = 0 ;
203202 @ SuppressWarnings ("unchecked" )
204- LoadLevel a1 = (LoadLevel ) c1 .getAnnotation (LoadLevel .class );
203+ LoadLevel a1 = (LoadLevel )c1 .getAnnotation (LoadLevel .class );
205204 @ SuppressWarnings ("unchecked" )
206- LoadLevel a2 = (LoadLevel ) c2 .getAnnotation (LoadLevel .class );
205+ LoadLevel a2 = (LoadLevel )c2 .getAnnotation (LoadLevel .class );
207206
208207 if (a1 != null ) {
209208 o1 = a1 .order ();
@@ -222,7 +221,8 @@ public int compare(Class c1, Class c2) {
222221 }
223222
224223 @ SuppressWarnings ("rawtypes" )
225- private static void loadFile (Class <?> service , String dir , ClassLoader classLoader , List <Class > extensions ) throws IOException {
224+ private static void loadFile (Class <?> service , String dir , ClassLoader classLoader , List <Class > extensions )
225+ throws IOException {
226226 String fileName = dir + service .getName ();
227227 Enumeration <URL > urls ;
228228 if (classLoader != null ) {
@@ -249,16 +249,14 @@ private static void loadFile(Class<?> service, String dir, ClassLoader classLoad
249249 }
250250 }
251251 } catch (ClassNotFoundException e ) {
252- // ignore
253252 } catch (Throwable e ) {
254- LOGGER .warn (e .getMessage ()); // 记录一下失败日志
253+ LOGGER .warn (e .getMessage ());
255254 } finally {
256255 try {
257256 if (reader != null ) {
258257 reader .close ();
259258 }
260259 } catch (IOException ioe ) {
261- // ignore
262260 }
263261 }
264262 }
0 commit comments