Line data Source code
1 : // Copyright (C) 2010 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.flogger.FluentLogger; 18 : import com.google.inject.Inject; 19 : import com.google.inject.Provider; 20 : import com.google.inject.Singleton; 21 : import java.util.ArrayList; 22 : import java.util.Arrays; 23 : import java.util.Collections; 24 : import java.util.HashSet; 25 : import java.util.List; 26 : import java.util.Set; 27 : import java.util.regex.PatternSyntaxException; 28 : import org.eclipse.jgit.lib.Config; 29 : 30 : /** Provides a list of all configured {@link TrackingFooter}s. */ 31 : @Singleton 32 : public class TrackingFootersProvider implements Provider<TrackingFooters> { 33 150 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 34 : 35 : private static final int MAX_LENGTH = 20; 36 : 37 : private static final String TRACKING_ID_TAG = "trackingid"; 38 : private static final String FOOTER_TAG = "footer"; 39 : private static final String SYSTEM_TAG = "system"; 40 : private static final String REGEX_TAG = "match"; 41 150 : private final List<TrackingFooter> trackingFooters = new ArrayList<>(); 42 : 43 : @Inject 44 150 : TrackingFootersProvider(@GerritServerConfig Config cfg) { 45 150 : for (String name : cfg.getSubsections(TRACKING_ID_TAG)) { 46 11 : boolean configValid = true; 47 : 48 11 : Set<String> footers = 49 11 : new HashSet<>(Arrays.asList(cfg.getStringList(TRACKING_ID_TAG, name, FOOTER_TAG))); 50 11 : footers.removeAll(Collections.singleton(null)); 51 : 52 11 : if (footers.isEmpty()) { 53 0 : configValid = false; 54 0 : logger.atSevere().log( 55 : "Missing %s.%s.%s in gerrit.config", TRACKING_ID_TAG, name, FOOTER_TAG); 56 : } 57 : 58 11 : String system = cfg.getString(TRACKING_ID_TAG, name, SYSTEM_TAG); 59 11 : if (system == null || system.isEmpty()) { 60 0 : configValid = false; 61 0 : logger.atSevere().log( 62 : "Missing %s.%s.%s in gerrit.config", TRACKING_ID_TAG, name, SYSTEM_TAG); 63 11 : } else if (system.length() > MAX_LENGTH) { 64 0 : configValid = false; 65 0 : logger.atSevere().log( 66 : "String too long \"%s\" in gerrit.config %s.%s.%s (max %d char)", 67 0 : system, TRACKING_ID_TAG, name, SYSTEM_TAG, MAX_LENGTH); 68 : } 69 : 70 11 : String match = cfg.getString(TRACKING_ID_TAG, name, REGEX_TAG); 71 11 : if (match == null || match.isEmpty()) { 72 0 : configValid = false; 73 0 : logger.atSevere().log( 74 : "Missing %s.%s.%s in gerrit.config", TRACKING_ID_TAG, name, REGEX_TAG); 75 : } 76 : 77 11 : if (configValid) { 78 : try { 79 11 : for (String footer : footers) { 80 11 : trackingFooters.add(new TrackingFooter(footer, match, system)); 81 11 : } 82 0 : } catch (PatternSyntaxException e) { 83 0 : logger.atSevere().log( 84 : "Invalid pattern \"%s\" in gerrit.config %s.%s.%s: %s", 85 0 : match, TRACKING_ID_TAG, name, REGEX_TAG, e.getMessage()); 86 11 : } 87 : } 88 11 : } 89 150 : } 90 : 91 : @Override 92 : public TrackingFooters get() { 93 150 : return new TrackingFooters(trackingFooters); 94 : } 95 : }