|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2016 the original author or authors. |
| 2 | + * Copyright 2002-2017 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
20 | 20 | import java.net.URL;
|
21 | 21 | import java.util.ArrayList;
|
22 | 22 | import java.util.Arrays;
|
| 23 | +import java.util.Collections; |
23 | 24 | import java.util.Enumeration;
|
24 | 25 | import java.util.List;
|
| 26 | +import java.util.Map; |
25 | 27 | import java.util.Properties;
|
26 | 28 |
|
27 | 29 | import org.apache.commons.logging.Log;
|
28 | 30 | import org.apache.commons.logging.LogFactory;
|
29 |
| - |
30 | 31 | import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
31 | 32 | import org.springframework.core.io.UrlResource;
|
32 | 33 | import org.springframework.util.Assert;
|
33 | 34 | import org.springframework.util.ClassUtils;
|
| 35 | +import org.springframework.util.ConcurrentReferenceHashMap; |
| 36 | +import org.springframework.util.LinkedMultiValueMap; |
| 37 | +import org.springframework.util.MultiValueMap; |
34 | 38 | import org.springframework.util.ReflectionUtils;
|
35 | 39 | import org.springframework.util.StringUtils;
|
36 | 40 |
|
@@ -58,6 +62,8 @@ public abstract class SpringFactoriesLoader {
|
58 | 62 |
|
59 | 63 | private static final Log logger = LogFactory.getLog(SpringFactoriesLoader.class);
|
60 | 64 |
|
| 65 | + private static Map<ClassLoader, MultiValueMap<String, String>> cache = new ConcurrentReferenceHashMap<>(); |
| 66 | + |
61 | 67 | /**
|
62 | 68 | * The location to look for factories.
|
63 | 69 | * <p>Can be present in multiple JAR files.
|
@@ -107,21 +113,36 @@ public static <T> List<T> loadFactories(Class<T> factoryClass, ClassLoader class
|
107 | 113 | */
|
108 | 114 | public static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) {
|
109 | 115 | String factoryClassName = factoryClass.getName();
|
| 116 | + return loadSpringFactories(classLoader).getOrDefault(factoryClassName, |
| 117 | + Collections.emptyList()); |
| 118 | + } |
| 119 | + |
| 120 | + private static Map<String, List<String>> loadSpringFactories( |
| 121 | + ClassLoader classLoader) { |
| 122 | + MultiValueMap<String, String> result = cache.get(classLoader); |
| 123 | + if (result != null) |
| 124 | + return result; |
110 | 125 | try {
|
111 |
| - Enumeration<URL> urls = (classLoader != null ? classLoader.getResources(FACTORIES_RESOURCE_LOCATION) : |
112 |
| - ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION)); |
113 |
| - List<String> result = new ArrayList<>(); |
| 126 | + Enumeration<URL> urls = (classLoader != null |
| 127 | + ? classLoader.getResources(FACTORIES_RESOURCE_LOCATION) |
| 128 | + : ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION)); |
| 129 | + result = new LinkedMultiValueMap<>(); |
114 | 130 | while (urls.hasMoreElements()) {
|
115 | 131 | URL url = urls.nextElement();
|
116 |
| - Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url)); |
117 |
| - String factoryClassNames = properties.getProperty(factoryClassName); |
118 |
| - result.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(factoryClassNames))); |
| 132 | + UrlResource resource = new UrlResource(url); |
| 133 | + Properties properties = PropertiesLoaderUtils.loadProperties(resource); |
| 134 | + for (Map.Entry<?, ?> entry : properties.entrySet()) { |
| 135 | + List<String> factoryClassNames = Arrays.asList( |
| 136 | + StringUtils.commaDelimitedListToStringArray((String) entry.getValue())); |
| 137 | + result.addAll((String) entry.getKey(), factoryClassNames); |
| 138 | + } |
119 | 139 | }
|
| 140 | + cache.put(classLoader, result); |
120 | 141 | return result;
|
121 | 142 | }
|
122 | 143 | catch (IOException ex) {
|
123 |
| - throw new IllegalArgumentException("Unable to load [" + factoryClass.getName() + |
124 |
| - "] factories from location [" + FACTORIES_RESOURCE_LOCATION + "]", ex); |
| 144 | + throw new IllegalArgumentException("Unable to load factories from location [" |
| 145 | + + FACTORIES_RESOURCE_LOCATION + "]", ex); |
125 | 146 | }
|
126 | 147 | }
|
127 | 148 |
|
|
0 commit comments