LCOV - code coverage report
Current view: top level - server/config - GerritServerConfigProvider.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 30 41 73.2 %
Date: 2022-11-19 15:00:39 Functions: 7 9 77.8 %

          Line data    Source code
       1             : // Copyright (C) 2009 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.server.config;
      16             : 
      17             : import static java.util.stream.Collectors.joining;
      18             : 
      19             : import com.google.common.flogger.FluentLogger;
      20             : import com.google.gerrit.common.Nullable;
      21             : import com.google.gerrit.server.securestore.SecureStore;
      22             : import com.google.inject.Inject;
      23             : import com.google.inject.Provider;
      24             : import com.google.inject.ProvisionException;
      25             : import com.google.inject.Singleton;
      26             : import java.io.IOException;
      27             : import java.nio.file.Path;
      28             : import java.util.ArrayList;
      29             : import java.util.List;
      30             : import org.eclipse.jgit.errors.ConfigInvalidException;
      31             : import org.eclipse.jgit.lib.Config;
      32             : import org.eclipse.jgit.storage.file.FileBasedConfig;
      33             : import org.eclipse.jgit.util.FS;
      34             : 
      35             : /**
      36             :  * Provides {@link Config} annotated with {@link GerritServerConfig}.
      37             :  *
      38             :  * <p>To react on config updates, the caller should implement @see GerritConfigListener.
      39             :  *
      40             :  * <p>The few callers that need a reloaded-on-demand config can inject a {@code
      41             :  * GerritServerConfigProvider} and request the lastest config with fetchLatestConfig().
      42             :  */
      43             : @Singleton
      44             : public class GerritServerConfigProvider implements Provider<Config> {
      45         138 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      46             : 
      47             :   private final SitePaths site;
      48             :   private final SecureStore secureStore;
      49             : 
      50         138 :   private final Object lock = new Object();
      51             : 
      52             :   private GerritConfig gerritConfig;
      53             : 
      54             :   @Inject
      55         138 :   GerritServerConfigProvider(SitePaths site, SecureStore secureStore) {
      56         138 :     this.site = site;
      57         138 :     this.secureStore = secureStore;
      58         138 :     this.gerritConfig = loadConfig();
      59         138 :   }
      60             : 
      61             :   @Override
      62             :   public Config get() {
      63          15 :     synchronized (lock) {
      64          15 :       return gerritConfig;
      65             :     }
      66             :   }
      67             : 
      68             :   protected ConfigUpdatedEvent updateConfig() {
      69           1 :     synchronized (lock) {
      70           1 :       Config oldConfig = gerritConfig;
      71           1 :       gerritConfig = loadConfig();
      72           1 :       return new ConfigUpdatedEvent(oldConfig, gerritConfig);
      73             :     }
      74             :   }
      75             : 
      76             :   public GerritConfig loadConfig() {
      77         138 :     FileBasedConfig baseConfig = loadConfig(null, site.gerrit_config);
      78         138 :     if (!baseConfig.getFile().exists()) {
      79           0 :       logger.atInfo().log("No %s; assuming defaults", site.gerrit_config.toAbsolutePath());
      80             :     }
      81             : 
      82         138 :     FileBasedConfig noteDbConfigOverBaseConfig = loadConfig(baseConfig, site.notedb_config);
      83         138 :     checkNoteDbConfig(noteDbConfigOverBaseConfig);
      84             : 
      85         138 :     return new GerritConfig(noteDbConfigOverBaseConfig, baseConfig, secureStore);
      86             :   }
      87             : 
      88             :   private static FileBasedConfig loadConfig(@Nullable Config base, Path path) {
      89         138 :     FileBasedConfig cfg = new FileBasedConfig(base, path.toFile(), FS.DETECTED);
      90             :     try {
      91         138 :       cfg.load();
      92           0 :     } catch (IOException | ConfigInvalidException e) {
      93           0 :       throw new ProvisionException(e.getMessage(), e);
      94         138 :     }
      95         138 :     return cfg;
      96             :   }
      97             : 
      98             :   private static void checkNoteDbConfig(FileBasedConfig noteDbConfig) {
      99         138 :     List<String> bad = new ArrayList<>();
     100         138 :     for (String section : noteDbConfig.getSections()) {
     101         138 :       if (section.equals("noteDb")) {
     102           0 :         continue;
     103             :       }
     104         138 :       for (String subsection : noteDbConfig.getSubsections(section)) {
     105           0 :         noteDbConfig
     106           0 :             .getNames(section, subsection, false)
     107           0 :             .forEach(n -> bad.add(section + "." + subsection + "." + n));
     108           0 :       }
     109         138 :       noteDbConfig.getNames(section, false).forEach(n -> bad.add(section + "." + n));
     110         138 :     }
     111         138 :     if (!bad.isEmpty()) {
     112           0 :       throw new ProvisionException(
     113             :           "Non-NoteDb config options not allowed in "
     114           0 :               + noteDbConfig.getFile()
     115             :               + ":\n"
     116           0 :               + bad.stream().collect(joining("\n")));
     117             :     }
     118         138 :   }
     119             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750