|
30 | 30 | import java.util.Map; |
31 | 31 | import java.util.Properties; |
32 | 32 | import java.util.TimeZone; |
| 33 | +import java.util.function.Supplier; |
33 | 34 |
|
34 | 35 | public abstract class BuildServerDataProvider { |
35 | 36 |
|
@@ -125,21 +126,37 @@ private void loadBuildVersionAndTimeData(@Nonnull Properties properties) { |
125 | 126 | } |
126 | 127 |
|
127 | 128 | private void loadBuildHostData(@Nonnull Properties properties) { |
128 | | - String buildHost = null; |
129 | | - try { |
130 | | - buildHost = InetAddress.getLocalHost().getHostName(); |
131 | | - } catch (UnknownHostException e) { |
132 | | - log.info("Unable to get build host, skipping property {}. Error message: {}", |
133 | | - GitCommitPropertyConstant.BUILD_HOST, |
134 | | - e.getMessage()); |
135 | | - } |
136 | | - put(properties, GitCommitPropertyConstant.BUILD_HOST, buildHost); |
| 129 | + Supplier<String> buildHostSupplier = () -> { |
| 130 | + String buildHost = null; |
| 131 | + try { |
| 132 | + // this call might be costly - try to avoid it too (similar concept as in GitDataProvider) |
| 133 | + buildHost = InetAddress.getLocalHost().getHostName(); |
| 134 | + } catch (UnknownHostException e) { |
| 135 | + log.info("Unable to get build host, skipping property {}. Error message: {}", |
| 136 | + GitCommitPropertyConstant.BUILD_HOST, |
| 137 | + e.getMessage()); |
| 138 | + } |
| 139 | + return buildHost; |
| 140 | + }; |
| 141 | + maybePut(properties, GitCommitPropertyConstant.BUILD_HOST, buildHostSupplier); |
137 | 142 | } |
138 | 143 |
|
139 | 144 | protected void put(@Nonnull Properties properties, @Nonnull String key, String value) { |
140 | 145 | String keyWithPrefix = prefixDot + key; |
141 | | - log.info("{} {}", keyWithPrefix, value); |
| 146 | + log.info("Collected {} with value {}", keyWithPrefix, value); |
142 | 147 | PropertyManager.putWithoutPrefix(properties, keyWithPrefix, value); |
143 | 148 | } |
144 | 149 |
|
| 150 | + protected void maybePut(@Nonnull Properties properties, @Nonnull String key, Supplier<String> supplier) { |
| 151 | + String keyWithPrefix = prefixDot + key; |
| 152 | + if (properties.contains(keyWithPrefix)) { |
| 153 | + String propertyValue = properties.getProperty(keyWithPrefix); |
| 154 | + log.info("Using cached {} with value {}", keyWithPrefix, propertyValue); |
| 155 | + } else { |
| 156 | + String propertyValue = supplier.get(); |
| 157 | + log.info("Collected {} with value {}", keyWithPrefix, propertyValue); |
| 158 | + PropertyManager.putWithoutPrefix(properties, keyWithPrefix, propertyValue); |
| 159 | + } |
| 160 | + } |
| 161 | + |
145 | 162 | } |
0 commit comments