Skip to content

Commit 0c89d09

Browse files
author
TheSnoozer
committed
use a simlar cache concept for gathering the BuildHostData since this operation might cause some delays
1 parent 8b0bb77 commit 0c89d09

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

src/main/java/pl/project13/maven/git/build/BuildServerDataProvider.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Map;
3131
import java.util.Properties;
3232
import java.util.TimeZone;
33+
import java.util.function.Supplier;
3334

3435
public abstract class BuildServerDataProvider {
3536

@@ -125,21 +126,37 @@ private void loadBuildVersionAndTimeData(@Nonnull Properties properties) {
125126
}
126127

127128
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);
137142
}
138143

139144
protected void put(@Nonnull Properties properties, @Nonnull String key, String value) {
140145
String keyWithPrefix = prefixDot + key;
141-
log.info("{} {}", keyWithPrefix, value);
146+
log.info("Collected {} with value {}", keyWithPrefix, value);
142147
PropertyManager.putWithoutPrefix(properties, keyWithPrefix, value);
143148
}
144149

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+
145162
}

0 commit comments

Comments
 (0)