|
19 | 19 | import java.time.Instant;
|
20 | 20 | import java.time.format.DateTimeFormatter;
|
21 | 21 | import java.time.format.DateTimeParseException;
|
| 22 | +import java.util.Objects; |
22 | 23 | import java.util.Properties;
|
| 24 | +import java.util.Set; |
| 25 | +import java.util.function.Function; |
| 26 | +import java.util.function.Predicate; |
23 | 27 |
|
24 | 28 | import org.springframework.aot.hint.RuntimeHints;
|
25 | 29 | import org.springframework.aot.hint.RuntimeHintsRegistrar;
|
|
35 | 39 | @ImportRuntimeHints(GitPropertiesRuntimeHints.class)
|
36 | 40 | public class GitProperties extends InfoProperties {
|
37 | 41 |
|
| 42 | + static final Set<Coercer> coercers = Set.of(Coercer.milliseconds(), |
| 43 | + Coercer.dateTimePattern("yyyy-MM-dd'T'HH:mm:ssXXX"), Coercer.dateTimePattern("yyyy-MM-dd'T'HH:mm:ssZ")); |
| 44 | + |
38 | 45 | public GitProperties(Properties entries) {
|
39 | 46 | super(processEntries(entries));
|
40 | 47 | }
|
@@ -97,46 +104,58 @@ private static Properties processEntries(Properties properties) {
|
97 | 104 | private static void coercePropertyToEpoch(Properties properties, String key) {
|
98 | 105 | String value = properties.getProperty(key);
|
99 | 106 | if (value != null) {
|
100 |
| - properties.setProperty(key, coerceToEpoch(value)); |
| 107 | + properties.setProperty(key, |
| 108 | + coercers.stream() |
| 109 | + .map((coercer) -> coercer.apply(value)) |
| 110 | + .filter(Objects::nonNull) |
| 111 | + .findFirst() |
| 112 | + .orElse(value)); |
101 | 113 | }
|
102 | 114 | }
|
103 | 115 |
|
104 | 116 | /**
|
105 |
| - * Attempt to convert the specified value to epoch time. Git properties information |
106 |
| - * are known to be specified either as epoch time in seconds or using a specific date |
107 |
| - * format. |
108 |
| - * @param s the value to coerce to |
109 |
| - * @return the epoch time in milliseconds or the original value if it couldn't be |
110 |
| - * converted |
| 117 | + * {@link RuntimeHintsRegistrar} for git properties. |
111 | 118 | */
|
112 |
| - private static String coerceToEpoch(String s) { |
113 |
| - Long epoch = parseEpochSecond(s); |
114 |
| - if (epoch != null) { |
115 |
| - return String.valueOf(epoch); |
116 |
| - } |
117 |
| - DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ"); |
118 |
| - try { |
119 |
| - return String.valueOf(format.parse(s, Instant::from).toEpochMilli()); |
120 |
| - } |
121 |
| - catch (DateTimeParseException ex) { |
122 |
| - return s; |
| 119 | + static class GitPropertiesRuntimeHints implements RuntimeHintsRegistrar { |
| 120 | + |
| 121 | + @Override |
| 122 | + public void registerHints(RuntimeHints hints, ClassLoader classLoader) { |
| 123 | + hints.resources().registerPattern("git.properties"); |
123 | 124 | }
|
| 125 | + |
124 | 126 | }
|
125 | 127 |
|
126 |
| - private static Long parseEpochSecond(String s) { |
127 |
| - try { |
128 |
| - return Long.parseLong(s) * 1000; |
129 |
| - } |
130 |
| - catch (NumberFormatException ex) { |
131 |
| - return null; |
| 128 | + /** |
| 129 | + * Coercer used to convert a source value to epoch time. |
| 130 | + */ |
| 131 | + private record Coercer(Function<String, Long> action, Predicate<RuntimeException> ignoredExceptions) { |
| 132 | + |
| 133 | + /** |
| 134 | + * Attempt to convert the specified value to epoch time. |
| 135 | + * @param value the value to coerce to |
| 136 | + * @return the epoch time in milliseconds or {@code null} |
| 137 | + */ |
| 138 | + String apply(String value) { |
| 139 | + try { |
| 140 | + Long result = this.action.apply(value); |
| 141 | + return (result != null) ? String.valueOf(result) : null; |
| 142 | + } |
| 143 | + catch (RuntimeException ex) { |
| 144 | + if (this.ignoredExceptions.test(ex)) { |
| 145 | + return null; |
| 146 | + } |
| 147 | + throw ex; |
| 148 | + } |
132 | 149 | }
|
133 |
| - } |
134 | 150 |
|
135 |
| - static class GitPropertiesRuntimeHints implements RuntimeHintsRegistrar { |
| 151 | + static Coercer milliseconds() { |
| 152 | + return new Coercer((value) -> Long.parseLong(value) * 1000, NumberFormatException.class::isInstance); |
| 153 | + } |
136 | 154 |
|
137 |
| - @Override |
138 |
| - public void registerHints(RuntimeHints hints, ClassLoader classLoader) { |
139 |
| - hints.resources().registerPattern("git.properties"); |
| 155 | + static Coercer dateTimePattern(String pattern) { |
| 156 | + return new Coercer( |
| 157 | + (value) -> DateTimeFormatter.ofPattern(pattern).parse(value, Instant::from).toEpochMilli(), |
| 158 | + DateTimeParseException.class::isInstance); |
140 | 159 | }
|
141 | 160 |
|
142 | 161 | }
|
|
0 commit comments