|
1 | 1 | package org.springframework.integration.aws.config.xml; |
2 | 2 |
|
3 | | -import org.slf4j.Logger; |
4 | | -import org.slf4j.LoggerFactory; |
5 | 3 | import org.springframework.beans.factory.xml.BeanDefinitionParser; |
6 | 4 | import org.springframework.beans.factory.xml.NamespaceHandlerSupport; |
| 5 | +import org.springframework.core.io.support.PropertiesLoaderUtils; |
7 | 6 |
|
8 | 7 | import java.io.IOException; |
9 | 8 | import java.io.UncheckedIOException; |
10 | | -import java.net.URISyntaxException; |
11 | | -import java.nio.file.Files; |
12 | | -import java.nio.file.Path; |
| 9 | +import java.util.Properties; |
13 | 10 |
|
14 | 11 | public class SpringIntegrationAwsNamespaceHandler extends NamespaceHandlerSupport { |
15 | 12 |
|
16 | | - private final Logger log = LoggerFactory.getLogger(SpringIntegrationAwsNamespaceHandler.class); |
17 | | - |
18 | 13 | @Override |
19 | 14 | public void init() { |
20 | | - var classLoader = getClass().getClassLoader(); |
21 | | - var packageName = getClass().getPackageName(); |
22 | | - try { |
23 | | - for (var paths = classLoader.getResources(packageName.replace('.', '/')); paths.hasMoreElements();) { |
24 | | - var path = paths.nextElement(); |
25 | | - try { |
26 | | - Files.list(Path.of(path.toURI())) |
27 | | - .filter(Files::isRegularFile) |
28 | | - .map(file -> file.getFileName().toString()) |
29 | | - .filter(fileName -> fileName.endsWith("Parser.class")) |
30 | | - .map(fileName -> loadClass(classLoader, packageName + '.' + fileName.replace(".class", ""))) |
31 | | - .filter(BeanDefinitionParser.class::isAssignableFrom) |
32 | | - .forEach(type -> registerBeanDefinitionParser(mapName(type), createParser(type))) |
33 | | - ; |
34 | | - } catch (URISyntaxException e) { |
35 | | - log.warn("Error reading path {}", path, e); |
36 | | - } |
37 | | - } |
| 15 | + var mapping = new Properties(); |
| 16 | + try (var defaults = getClass().getResourceAsStream("element-parser.mapping")) { |
| 17 | + mapping.load(defaults); |
| 18 | + mapping.putAll(PropertiesLoaderUtils.loadAllProperties("META-INF/spring-integration-aws-element-parser.mapping")); |
38 | 19 | } catch (IOException e) { |
39 | | - throw new UncheckedIOException("Error reading classes for package " + packageName, e); |
40 | | - } |
41 | | - } |
42 | | - |
43 | | - private String mapName(Class<?> type) { |
44 | | - var typeName = type.getSimpleName(); |
45 | | - var length = typeName.length() - "Parser".length(); |
46 | | - var sb = new StringBuilder(); |
47 | | - sb.append(Character.toLowerCase(typeName.charAt(0))); |
48 | | - for (int i = 1; i < length; ++i) { |
49 | | - var c = typeName.charAt(i); |
50 | | - if (Character.isUpperCase(c)) { |
51 | | - sb.append('-').append(Character.toLowerCase(c)); |
52 | | - } else { |
53 | | - sb.append(c); |
54 | | - } |
55 | | - } |
56 | | - return sb.toString(); |
57 | | - } |
58 | | - |
59 | | - private Class<?> loadClass(ClassLoader classLoader, String name) { |
60 | | - try { |
61 | | - return classLoader.loadClass(name); |
62 | | - } catch (ClassNotFoundException e) { |
63 | | - throw new RuntimeException("Error load class " + name, e); |
| 20 | + throw new UncheckedIOException("Error load spring-integration-aws element-parser mapping", e); |
64 | 21 | } |
| 22 | + mapping.forEach(this::registerParserMapping); |
65 | 23 | } |
66 | 24 |
|
67 | | - private BeanDefinitionParser createParser(Class<?> type) { |
| 25 | + private void registerParserMapping(Object elementName, Object className) { |
68 | 26 | try { |
69 | | - return (BeanDefinitionParser) type.getConstructor().newInstance(); |
70 | | - } catch (Exception e) { |
71 | | - throw new RuntimeException("Error instantiate parser of type " + type, e); |
| 27 | + var type = Thread.currentThread().getContextClassLoader().loadClass((String) className); |
| 28 | + var parser = (BeanDefinitionParser) type.getConstructor().newInstance(); |
| 29 | + registerBeanDefinitionParser((String) elementName, parser); |
| 30 | + } catch (ReflectiveOperationException e) { |
| 31 | + throw new RuntimeException("Error load parser for " + elementName + ": " + className, e); |
72 | 32 | } |
73 | 33 | } |
74 | 34 | } |
0 commit comments