Line data Source code
1 : // Copyright (C) 2020 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 com.google.common.base.Preconditions.checkState; 18 : 19 : import com.google.common.base.Strings; 20 : import com.google.gerrit.entities.RefNames; 21 : import com.google.gerrit.exceptions.StorageException; 22 : import com.google.gerrit.server.git.meta.VersionedMetaData; 23 : import java.io.IOException; 24 : import org.eclipse.jgit.errors.ConfigInvalidException; 25 : import org.eclipse.jgit.lib.CommitBuilder; 26 : import org.eclipse.jgit.lib.Config; 27 : import org.eclipse.jgit.lib.Repository; 28 : 29 : /** 30 : * Low-level storage API to load Gerrit's default config from {@code All-Users}. Should not be used 31 : * directly. 32 : */ 33 151 : public class VersionedDefaultPreferences extends VersionedMetaData { 34 : private static final String PREFERENCES_CONFIG = "preferences.config"; 35 : 36 : private Config cfg; 37 : 38 : public static Config get(Repository allUsersRepo, AllUsersName allUsersName) 39 : throws StorageException, ConfigInvalidException { 40 151 : VersionedDefaultPreferences versionedDefaultPreferences = new VersionedDefaultPreferences(); 41 : try { 42 151 : versionedDefaultPreferences.load(allUsersName, allUsersRepo); 43 0 : } catch (IOException e) { 44 0 : throw new StorageException(e); 45 151 : } 46 151 : return versionedDefaultPreferences.getConfig(); 47 : } 48 : 49 : @Override 50 : protected String getRefName() { 51 151 : return RefNames.REFS_USERS_DEFAULT; 52 : } 53 : 54 : public Config getConfig() { 55 151 : checkState(cfg != null, "Default preferences not loaded yet."); 56 151 : return cfg; 57 : } 58 : 59 : @Override 60 : protected void onLoad() throws IOException, ConfigInvalidException { 61 151 : cfg = readConfig(PREFERENCES_CONFIG); 62 151 : } 63 : 64 : @Override 65 : protected boolean onSave(CommitBuilder commit) throws IOException { 66 2 : if (Strings.isNullOrEmpty(commit.getMessage())) { 67 2 : commit.setMessage("Update default preferences\n"); 68 : } 69 2 : saveConfig(PREFERENCES_CONFIG, cfg); 70 2 : return true; 71 : } 72 : }