Line data Source code
1 : // Copyright (C) 2012 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.project; 16 : 17 : import com.google.common.collect.ImmutableList; 18 : import com.google.common.collect.Multimap; 19 : import com.google.common.flogger.FluentLogger; 20 : import com.google.gerrit.entities.StoredCommentLinkInfo; 21 : import com.google.gerrit.extensions.api.projects.CommentLinkInfo; 22 : import com.google.gerrit.server.config.ConfigUpdatedEvent; 23 : import com.google.gerrit.server.config.ConfigUpdatedEvent.ConfigUpdateEntry; 24 : import com.google.gerrit.server.config.ConfigUpdatedEvent.UpdateResult; 25 : import com.google.gerrit.server.config.GerritConfigListener; 26 : import com.google.gerrit.server.config.GerritServerConfig; 27 : import com.google.inject.Inject; 28 : import com.google.inject.Provider; 29 : import com.google.inject.Singleton; 30 : import java.util.List; 31 : import java.util.Set; 32 : import org.eclipse.jgit.lib.Config; 33 : 34 : @Singleton 35 : public class CommentLinkProvider implements Provider<List<CommentLinkInfo>>, GerritConfigListener { 36 151 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 37 : 38 : private volatile List<CommentLinkInfo> commentLinks; 39 : 40 : @Inject 41 151 : CommentLinkProvider(@GerritServerConfig Config cfg) { 42 151 : this.commentLinks = parseConfig(cfg); 43 151 : } 44 : 45 : private List<CommentLinkInfo> parseConfig(Config cfg) { 46 151 : Set<String> subsections = cfg.getSubsections(ProjectConfig.COMMENTLINK); 47 151 : ImmutableList.Builder<CommentLinkInfo> cls = 48 151 : ImmutableList.builderWithExpectedSize(subsections.size()); 49 151 : for (String name : subsections) { 50 : try { 51 1 : StoredCommentLinkInfo cl = ProjectConfig.buildCommentLink(cfg, name); 52 1 : if (cl.getOverrideOnly()) { 53 0 : logger.atWarning().log("commentlink %s empty except for \"enabled\"", name); 54 0 : continue; 55 : } 56 1 : cls.add(cl.toInfo()); 57 0 : } catch (IllegalArgumentException e) { 58 0 : logger.atWarning().log("invalid commentlink: %s", e.getMessage()); 59 1 : } 60 1 : } 61 151 : return cls.build(); 62 : } 63 : 64 : @Override 65 : public List<CommentLinkInfo> get() { 66 151 : return commentLinks; 67 : } 68 : 69 : @Override 70 : public Multimap<UpdateResult, ConfigUpdateEntry> configUpdated(ConfigUpdatedEvent event) { 71 1 : if (event.isSectionUpdated(ProjectConfig.COMMENTLINK)) { 72 0 : commentLinks = parseConfig(event.getNewConfig()); 73 0 : return event.accept(ProjectConfig.COMMENTLINK); 74 : } 75 1 : return ConfigUpdatedEvent.NO_UPDATES; 76 : } 77 : }