Skip to content
This repository was archived by the owner on Feb 23, 2025. It is now read-only.

Commit

Permalink
Merge pull request #124 from Linfar/fix_bug_116
Browse files Browse the repository at this point in the history
  • Loading branch information
morincer authored Feb 5, 2024
2 parents f806065 + d409d29 commit 29bcc89
Showing 1 changed file with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import jetbrains.buildServer.auth.saml.plugin.pojo.SamlPluginSettings;
import jetbrains.buildServer.configuration.FileWatcher;
import jetbrains.buildServer.log.Loggers;
import jetbrains.buildServer.serverSide.IOGuard;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import jetbrains.buildServer.log.Loggers;
import jetbrains.buildServer.serverSide.IOGuard;
import lombok.Getter;
Expand All @@ -14,21 +20,49 @@
public class SamlPluginSettingsStorageImpl implements SamlPluginSettingsStorage {
private final ObjectMapper objectMapper;

@Nullable
private volatile SamlPluginSettings cachedSamlPluginSettings = null;

private final Object lock = new Object();

@Getter
private final Path configPath;
private final FileWatcher samlPluginSettingsFileWatcher;

public SamlPluginSettingsStorageImpl(Path configPath) {
this.objectMapper = new ObjectMapper();
this.configPath = configPath;

samlPluginSettingsFileWatcher = new FileWatcher(this.configPath.toFile());
}

@Override
public void init() throws IOException {
load();

samlPluginSettingsFileWatcher.registerListener(s -> {
try {
reload();
} catch (IOException e) {
throw new RuntimeException(e);
}
});
samlPluginSettingsFileWatcher.start();
}

@Override
public SamlPluginSettings load() throws IOException {
if (cachedSamlPluginSettings != null) return cachedSamlPluginSettings;

synchronized (lock) {
if (cachedSamlPluginSettings != null) return cachedSamlPluginSettings;

return reload();
}
}

@NotNull
private SamlPluginSettings reload() throws IOException {
if (!this.configPath.toFile().exists() || this.configPath.toFile().length() == 0) {
save(new SamlPluginSettings());
}
Expand All @@ -41,6 +75,7 @@ public SamlPluginSettings load() throws IOException {
result.getAdditionalCerts().clear();
}

cachedSamlPluginSettings = result;
return result;
} catch (RuntimeException ex) {
Loggers.SERVER.error("Cannot load SAML plugin settings", ex);
Expand All @@ -49,7 +84,16 @@ public SamlPluginSettings load() throws IOException {
}

@Override
public void save(SamlPluginSettings settings) throws IOException {
IOGuard.allowDiskWrite(() -> this.objectMapper.writeValue(this.configPath.toFile(), settings));
public void save(SamlPluginSettings settings) {
synchronized (lock) {
samlPluginSettingsFileWatcher.runActionWithDisabledObserver(() -> {
try {
IOGuard.allowDiskWrite(() -> this.objectMapper.writeValue(this.configPath.toFile(), settings));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
cachedSamlPluginSettings = settings;
}
}
}

0 comments on commit 29bcc89

Please sign in to comment.