Line data Source code
1 : // Copyright (C) 2013 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.pgm.init.api; 16 : 17 : import com.google.common.flogger.FluentLogger; 18 : import com.google.gerrit.entities.Project; 19 : import com.google.gerrit.entities.RefNames; 20 : import com.google.gerrit.server.config.AllProjectsConfigProvider; 21 : import com.google.gerrit.server.config.AllProjectsName; 22 : import com.google.gerrit.server.config.SitePaths; 23 : import com.google.gerrit.server.project.GroupList; 24 : import com.google.gerrit.server.project.ProjectConfig; 25 : import com.google.inject.Inject; 26 : import java.io.IOException; 27 : import java.util.Optional; 28 : import org.eclipse.jgit.errors.ConfigInvalidException; 29 : import org.eclipse.jgit.lib.CommitBuilder; 30 : import org.eclipse.jgit.lib.Config; 31 : import org.eclipse.jgit.lib.PersonIdent; 32 : import org.eclipse.jgit.lib.RepositoryCache; 33 : import org.eclipse.jgit.lib.StoredConfig; 34 : 35 : public class AllProjectsConfig extends VersionedMetaDataOnInit { 36 16 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 37 : 38 : private final Optional<StoredConfig> baseConfig; 39 : private Config cfg; 40 : private GroupList groupList; 41 : 42 : @Inject 43 : AllProjectsConfig( 44 : AllProjectsNameOnInitProvider allProjects, 45 : AllProjectsConfigProvider allProjectsConfigProvider, 46 : SitePaths site, 47 : InitFlags flags) { 48 16 : super(flags, site, allProjects.get(), RefNames.REFS_CONFIG); 49 16 : this.baseConfig = allProjectsConfigProvider.get(new AllProjectsName(allProjects.get())); 50 16 : } 51 : 52 : public Config getConfig() { 53 16 : return cfg; 54 : } 55 : 56 : public GroupList getGroups() { 57 0 : return groupList; 58 : } 59 : 60 : @Override 61 : public AllProjectsConfig load() throws IOException, ConfigInvalidException { 62 16 : super.load(); 63 16 : return this; 64 : } 65 : 66 : @Override 67 : protected void onLoad() throws IOException, ConfigInvalidException { 68 16 : if (baseConfig.isPresent()) { 69 16 : baseConfig.get().load(); 70 : } 71 16 : groupList = readGroupList(); 72 16 : cfg = readConfig(ProjectConfig.PROJECT_CONFIG, baseConfig); 73 16 : } 74 : 75 : private GroupList readGroupList() throws IOException { 76 16 : return GroupList.parse( 77 16 : Project.nameKey(project), 78 16 : readUTF8(GroupList.FILE_NAME), 79 : error -> 80 0 : logger.atSevere().log( 81 0 : "Error parsing file %s: %s", GroupList.FILE_NAME, error.getMessage())); 82 : } 83 : 84 : public void save(String pluginName, String message) throws IOException, ConfigInvalidException { 85 0 : save( 86 : new PersonIdent(pluginName, pluginName + "@gerrit"), 87 : "Update from plugin " + pluginName + ": " + message); 88 0 : } 89 : 90 : @Override 91 : protected void save(PersonIdent ident, String msg) throws IOException, ConfigInvalidException { 92 0 : super.save(ident, msg); 93 : 94 : // we need to invalidate the JGit cache if the group list is invalidated in 95 : // an unattended init step 96 0 : RepositoryCache.clear(); 97 0 : } 98 : 99 : @Override 100 : protected boolean onSave(CommitBuilder commit) throws IOException, ConfigInvalidException { 101 0 : saveConfig(ProjectConfig.PROJECT_CONFIG, cfg); 102 0 : saveGroupList(); 103 0 : return true; 104 : } 105 : 106 : private void saveGroupList() throws IOException { 107 0 : saveUTF8(GroupList.FILE_NAME, groupList.asText()); 108 0 : } 109 : }