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.common.cache.CacheLoader; 18 : import com.google.common.cache.LoadingCache; 19 : import com.google.gerrit.entities.RefNames; 20 : import com.google.gerrit.exceptions.StorageException; 21 : import com.google.gerrit.server.cache.CacheModule; 22 : import com.google.gerrit.server.git.GitRepositoryManager; 23 : import com.google.inject.Inject; 24 : import com.google.inject.Module; 25 : import com.google.inject.Singleton; 26 : import com.google.inject.name.Named; 27 : import java.io.IOException; 28 : import java.util.concurrent.ExecutionException; 29 : import org.eclipse.jgit.errors.ConfigInvalidException; 30 : import org.eclipse.jgit.lib.ObjectId; 31 : import org.eclipse.jgit.lib.Ref; 32 : import org.eclipse.jgit.lib.Repository; 33 : 34 : @Singleton 35 : public class DefaultPreferencesCacheImpl implements DefaultPreferencesCache { 36 : private static final String NAME = "default_preferences"; 37 : 38 : public static Module module() { 39 152 : return new CacheModule() { 40 : @Override 41 : protected void configure() { 42 : // Bind an in-memory cache that allows exactly 1 value to be cached. 43 152 : cache(NAME, ObjectId.class, CachedPreferences.class).loader(Loader.class).maximumWeight(1); 44 152 : bind(DefaultPreferencesCacheImpl.class); 45 152 : bind(DefaultPreferencesCache.class).to(DefaultPreferencesCacheImpl.class); 46 152 : } 47 : }; 48 : } 49 : 50 : private final GitRepositoryManager repositoryManager; 51 : private final AllUsersName allUsersName; 52 : private final LoadingCache<ObjectId, CachedPreferences> cache; 53 : 54 : @Inject 55 : DefaultPreferencesCacheImpl( 56 : GitRepositoryManager repositoryManager, 57 : AllUsersName allUsersName, 58 152 : @Named(NAME) LoadingCache<ObjectId, CachedPreferences> cache) { 59 152 : this.repositoryManager = repositoryManager; 60 152 : this.allUsersName = allUsersName; 61 152 : this.cache = cache; 62 152 : } 63 : 64 : @Override 65 : public CachedPreferences get() { 66 2 : try (Repository allUsersRepo = repositoryManager.openRepository(allUsersName)) { 67 2 : Ref ref = allUsersRepo.exactRef(RefNames.REFS_USERS_DEFAULT); 68 2 : if (ref == null) { 69 2 : return EMPTY; 70 : } 71 1 : return get(ref.getObjectId()); 72 2 : } catch (IOException e) { 73 0 : throw new StorageException(e); 74 : } 75 : } 76 : 77 : @Override 78 : public CachedPreferences get(ObjectId rev) { 79 : try { 80 2 : return cache.get(rev); 81 0 : } catch (ExecutionException e) { 82 0 : throw new StorageException(e); 83 : } 84 : } 85 : 86 : @Singleton 87 : private static class Loader extends CacheLoader<ObjectId, CachedPreferences> { 88 : private final GitRepositoryManager repositoryManager; 89 : private final AllUsersName allUsersName; 90 : 91 : @Inject 92 152 : Loader(GitRepositoryManager repositoryManager, AllUsersName allUsersName) { 93 152 : this.repositoryManager = repositoryManager; 94 152 : this.allUsersName = allUsersName; 95 152 : } 96 : 97 : @Override 98 : public CachedPreferences load(ObjectId key) throws IOException, ConfigInvalidException { 99 2 : try (Repository allUsersRepo = repositoryManager.openRepository(allUsersName)) { 100 2 : VersionedDefaultPreferences versionedDefaultPreferences = new VersionedDefaultPreferences(); 101 2 : versionedDefaultPreferences.load(allUsersName, allUsersRepo, key); 102 2 : return CachedPreferences.fromConfig(versionedDefaultPreferences.getConfig()); 103 : } 104 : } 105 : } 106 : }