Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Sohn2019-08-22 08:23:50 +0000
committerMatthias Sohn2019-08-23 09:07:05 +0000
commitd67fe3891553292d1496f5f056b7e6a8ffabee87 (patch)
treefb086485b75ce494b3dbff99c980ba05312edafe /org.eclipse.jgit/src
parent28553c189c0561b41d31567256cb320c05075d47 (diff)
downloadjgit-d67fe3891553292d1496f5f056b7e6a8ffabee87.tar.gz
jgit-d67fe3891553292d1496f5f056b7e6a8ffabee87.tar.xz
jgit-d67fe3891553292d1496f5f056b7e6a8ffabee87.zip
Use AtomicReferences to cache user and system level configs
This ensures that only one instance of user and one instance of system config is set. Change-Id: Idd00150f91d2d40af79499dd7bf8ad5940f87c4e Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
Diffstat (limited to 'org.eclipse.jgit/src')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/util/SystemReader.java47
1 files changed, 23 insertions, 24 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/SystemReader.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/SystemReader.java
index d554562a75..310bc83a66 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/util/SystemReader.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/SystemReader.java
@@ -56,6 +56,7 @@ import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.TimeZone;
+import java.util.concurrent.atomic.AtomicReference;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.errors.CorruptObjectException;
@@ -97,9 +98,9 @@ public abstract class SystemReader {
private static class Default extends SystemReader {
private volatile String hostname;
- private volatile FileBasedConfig systemConfig;
+ private AtomicReference<FileBasedConfig> systemConfig = new AtomicReference<>();
- private volatile FileBasedConfig userConfig;
+ private volatile AtomicReference<FileBasedConfig> userConfig = new AtomicReference<>();
@Override
public String getenv(String variable) {
@@ -113,10 +114,13 @@ public abstract class SystemReader {
@Override
public FileBasedConfig openSystemConfig(Config parent, FS fs) {
- if (systemConfig == null) {
- systemConfig = createSystemConfig(parent, fs);
+ FileBasedConfig c = systemConfig.get();
+ if (c == null) {
+ systemConfig.compareAndSet(null,
+ createSystemConfig(parent, fs));
+ c = systemConfig.get();
}
- return systemConfig;
+ return c;
}
protected FileBasedConfig createSystemConfig(Config parent, FS fs) {
@@ -142,40 +146,35 @@ public abstract class SystemReader {
@Override
public FileBasedConfig openUserConfig(Config parent, FS fs) {
- if (userConfig == null) {
- File home = fs.userHome();
- userConfig = new FileBasedConfig(parent,
- new File(home, ".gitconfig"), fs); //$NON-NLS-1$
+ FileBasedConfig c = userConfig.get();
+ if (c == null) {
+ userConfig.compareAndSet(null, new FileBasedConfig(parent,
+ new File(fs.userHome(), ".gitconfig"), fs)); //$NON-NLS-1$
+ c = userConfig.get();
}
- return userConfig;
+ return c;
}
@Override
public StoredConfig getSystemConfig()
throws IOException, ConfigInvalidException {
- if (systemConfig == null) {
- systemConfig = createSystemConfig(null, FS.DETECTED);
- }
- if (systemConfig.isOutdated()) {
+ FileBasedConfig c = openSystemConfig(null, FS.DETECTED);
+ if (c.isOutdated()) {
LOG.debug("loading system config {}", systemConfig); //$NON-NLS-1$
- systemConfig.load();
+ c.load();
}
- return systemConfig;
+ return c;
}
@Override
public StoredConfig getUserConfig()
throws IOException, ConfigInvalidException {
- if (userConfig == null) {
- userConfig = openUserConfig(getSystemConfig(), FS.DETECTED);
- } else {
- getSystemConfig();
- }
- if (userConfig.isOutdated()) {
+ FileBasedConfig c = openUserConfig(getSystemConfig(), FS.DETECTED);
+ if (c.isOutdated()) {
LOG.debug("loading user config {}", userConfig); //$NON-NLS-1$
- userConfig.load();
+ c.load();
}
- return userConfig;
+ return c;
}
@Override

Back to the top