Line data Source code
1 : // Copyright (C) 2021 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 com.google.common.flogger.FluentLogger; 18 : import com.google.inject.Inject; 19 : import com.google.inject.Singleton; 20 : import java.io.IOException; 21 : import java.nio.file.Path; 22 : import org.eclipse.jgit.errors.ConfigInvalidException; 23 : import org.eclipse.jgit.lib.Config; 24 : import org.eclipse.jgit.storage.file.FileBasedConfig; 25 : import org.eclipse.jgit.util.FS; 26 : 27 : @Singleton 28 : public class FileBasedGlobalPluginConfigProvider implements GlobalPluginConfigProvider { 29 150 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 30 : private final SitePaths site; 31 : 32 : @Inject 33 150 : FileBasedGlobalPluginConfigProvider(SitePaths site) { 34 150 : this.site = site; 35 150 : } 36 : 37 : @Override 38 : public Config get(String pluginName) { 39 1 : Path pluginConfigFile = site.etc_dir.resolve(pluginName + ".config"); 40 1 : FileBasedConfig cfg = new FileBasedConfig(pluginConfigFile.toFile(), FS.DETECTED); 41 1 : if (!cfg.getFile().exists()) { 42 0 : logger.atInfo().log("No %s; assuming defaults", pluginConfigFile.toAbsolutePath()); 43 0 : return cfg; 44 : } 45 : 46 : try { 47 1 : cfg.load(); 48 0 : } catch (ConfigInvalidException e) { 49 : // This is an error in user input, don't spam logs with a stack trace. 50 0 : logger.atWarning().log( 51 0 : "Failed to load %s: %s", pluginConfigFile.toAbsolutePath(), e.getMessage()); 52 0 : } catch (IOException e) { 53 0 : logger.atWarning().withCause(e).log("Failed to load %s", pluginConfigFile.toAbsolutePath()); 54 1 : } 55 1 : return cfg; 56 : } 57 : }