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 com.google.auto.value.AutoValue; 18 : import com.google.gerrit.common.Nullable; 19 : import com.google.gerrit.exceptions.StorageException; 20 : import com.google.gerrit.extensions.client.DiffPreferencesInfo; 21 : import com.google.gerrit.extensions.client.EditPreferencesInfo; 22 : import com.google.gerrit.extensions.client.GeneralPreferencesInfo; 23 : import java.util.Optional; 24 : import org.eclipse.jgit.errors.ConfigInvalidException; 25 : import org.eclipse.jgit.lib.Config; 26 : 27 : /** 28 : * Container class for preferences serialized as Git-style config files. Keeps the values as {@link 29 : * String}s as they are immutable and thread-safe. 30 : */ 31 : @AutoValue 32 151 : public abstract class CachedPreferences { 33 : 34 151 : public static CachedPreferences EMPTY = fromString(""); 35 : 36 : public abstract String config(); 37 : 38 : /** Returns a cache-able representation of the config. */ 39 : public static CachedPreferences fromConfig(Config cfg) { 40 151 : return new AutoValue_CachedPreferences(cfg.toText()); 41 : } 42 : 43 : /** 44 : * Returns a cache-able representation of the config. To be used only when constructing a {@link 45 : * CachedPreferences} from a serialized, cached value. 46 : */ 47 : public static CachedPreferences fromString(String cfg) { 48 151 : return new AutoValue_CachedPreferences(cfg); 49 : } 50 : 51 : public static GeneralPreferencesInfo general( 52 : Optional<CachedPreferences> defaultPreferences, CachedPreferences userPreferences) { 53 : try { 54 106 : return PreferencesParserUtil.parseGeneralPreferences( 55 106 : userPreferences.asConfig(), configOrNull(defaultPreferences), null); 56 0 : } catch (ConfigInvalidException e) { 57 0 : return GeneralPreferencesInfo.defaults(); 58 : } 59 : } 60 : 61 : public static EditPreferencesInfo edit( 62 : Optional<CachedPreferences> defaultPreferences, CachedPreferences userPreferences) { 63 : try { 64 1 : return PreferencesParserUtil.parseEditPreferences( 65 1 : userPreferences.asConfig(), configOrNull(defaultPreferences), null); 66 0 : } catch (ConfigInvalidException e) { 67 0 : return EditPreferencesInfo.defaults(); 68 : } 69 : } 70 : 71 : public static DiffPreferencesInfo diff( 72 : Optional<CachedPreferences> defaultPreferences, CachedPreferences userPreferences) { 73 : try { 74 1 : return PreferencesParserUtil.parseDiffPreferences( 75 1 : userPreferences.asConfig(), configOrNull(defaultPreferences), null); 76 0 : } catch (ConfigInvalidException e) { 77 0 : return DiffPreferencesInfo.defaults(); 78 : } 79 : } 80 : 81 : public Config asConfig() { 82 107 : Config cfg = new Config(); 83 : try { 84 107 : cfg.fromText(config()); 85 0 : } catch (ConfigInvalidException e) { 86 : // Programmer error: We have parsed this config before and are unable to parse it now. 87 0 : throw new StorageException(e); 88 107 : } 89 107 : return cfg; 90 : } 91 : 92 : @Nullable 93 : private static Config configOrNull(Optional<CachedPreferences> cachedPreferences) { 94 106 : return cachedPreferences.map(CachedPreferences::asConfig).orElse(null); 95 : } 96 : }