Guest User

Untitled

a guest
Jul 20th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. package io.crazy88.beatrix.boot.akka;
  2.  
  3. import static io.crazy88.beatrix.boot.akka.AkkaConfigPropertySource.PROPERTY_SOURCE_NAME;
  4. import static java.util.Collections.emptyMap;
  5. import static java.util.Collections.unmodifiableList;
  6. import static java.util.Collections.unmodifiableMap;
  7. import static java.util.Comparator.comparing;
  8. import static java.util.stream.Collectors.toMap;
  9. import static java.util.stream.StreamSupport.stream;
  10. import static org.springframework.core.env.StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME;
  11. import static org.springframework.core.env.StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME;
  12. import static org.springframework.util.CollectionUtils.isEmpty;
  13.  
  14. import java.util.ArrayList;
  15. import java.util.LinkedHashMap;
  16. import java.util.List;
  17. import java.util.Map;
  18. import java.util.regex.Matcher;
  19. import java.util.regex.Pattern;
  20. import java.util.stream.Stream;
  21.  
  22. import org.apache.commons.lang3.tuple.ImmutablePair;
  23. import org.apache.commons.lang3.tuple.Pair;
  24. import org.springframework.core.env.ConfigurableEnvironment;
  25. import org.springframework.core.env.EnumerablePropertySource;
  26. import org.springframework.core.env.PropertySource;
  27.  
  28. class AkkaConfigPropertySourceAdapter {
  29.  
  30. private static final Pattern INDEXED_PROPERTY_PATTERN = Pattern.compile("^(?<path>.*)\\[(?<index>\\d+)\\]$");
  31.  
  32. static Map<String, Object> getConfigMap(ConfigurableEnvironment environment) {
  33. return convertIndexedProperties(getPropertyMap(environment));
  34. }
  35.  
  36. @SuppressWarnings("unchecked")
  37. private static Map<String, Object> convertIndexedProperties(Map<String, String> propertyMap) {
  38.  
  39. if (isEmpty(propertyMap)) {
  40. return emptyMap();
  41. }
  42.  
  43. Map<String, Object> converted = new LinkedHashMap<>();
  44.  
  45. for (Map.Entry<String, String> entry: propertyMap.entrySet()) {
  46. Matcher m = INDEXED_PROPERTY_PATTERN.matcher(entry.getKey());
  47. String path = m.matches() ? m.group("path") : entry.getKey();
  48. Object existing = converted.get(path);
  49.  
  50. // Make sure properties are either list-based or normal
  51. if ((existing != null) && ((m.matches() && !(existing instanceof List)) || (!m.matches() && !(existing instanceof String)))) {
  52. throw new IllegalArgumentException("Invalid property hierarchy - property must be either a regular or a list-based property (path:" + path + ", existing: " + existing + ", key:" + entry.getKey() + ", value:" + entry.getValue());
  53. }
  54.  
  55. // Convert indexed properties to lists
  56. if (m.matches()) {
  57.  
  58. int index = Integer.parseInt(m.group("index"));
  59. ArrayList<String> list = (ArrayList<String>) converted.getOrDefault(path, new ArrayList<>());
  60.  
  61. // Extend the list, if necessary
  62. list.ensureCapacity(index + 1);
  63. while (list.size() <= index) {
  64. list.add(null);
  65. }
  66.  
  67. list.set(index, entry.getValue());
  68. converted.put(path, list);
  69.  
  70. } else {
  71. // Copy normal properties
  72. converted.put(entry.getKey(), entry.getValue());
  73. }
  74. }
  75.  
  76. // Make sure any contained lists are immutable
  77. for (Map.Entry<String, Object> entry : converted.entrySet()) {
  78. if (entry.getValue() instanceof List) {
  79. entry.setValue(unmodifiableList((List<String>) entry.getValue()));
  80. }
  81. }
  82.  
  83. return unmodifiableMap(converted);
  84. }
  85.  
  86. private static Map<String, String> getPropertyMap(ConfigurableEnvironment environment) {
  87. return stream(environment.getPropertySources().spliterator(), false)
  88. .filter(AkkaConfigPropertySourceAdapter::isEligiblePropertySource)
  89. .sorted(comparing(source -> source.getName().length()))
  90. .map(EnumerablePropertySource.class::cast)
  91. .flatMap(source -> Stream.of(source.getPropertyNames()))
  92. .filter(AkkaConfigPropertySourceAdapter::isEligibleProperty)
  93. .map(name -> ImmutablePair.of(name, environment.getProperty(name)))
  94. .collect(toMap(Pair::getKey, Pair::getValue, (a, b) -> b));
  95. }
  96.  
  97. private static boolean isEligiblePropertySource(PropertySource source) {
  98. String name = source.getName();
  99. return (source instanceof EnumerablePropertySource)
  100. && !(name.equalsIgnoreCase(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)
  101. || name.equalsIgnoreCase(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)
  102. || name.equalsIgnoreCase(PROPERTY_SOURCE_NAME));
  103. }
  104.  
  105. private static boolean isEligibleProperty(String name) {
  106. return name.startsWith("akka") || name.startsWith("simple-akka-downing.");
  107. }
  108. }
Add Comment
Please, Sign In to add comment