blob: 40fd6d0baa0cd7181b7dae5c94501545a814f71b [file] [log] [blame]
Dave Borowitz5d49ff22012-12-17 14:07:57 -08001// Copyright 2012 Google Inc. All Rights Reserved.
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
15package com.google.gitiles;
16
David Pursehouse3c0207e2016-05-27 12:10:16 +090017import static com.google.common.base.Preconditions.checkNotNull;
18
Dave Borowitz3b744b12016-08-19 16:11:10 -040019import java.io.File;
20import java.io.IOException;
21import java.util.Map;
22import javax.servlet.FilterConfig;
Dave Borowitz5d49ff22012-12-17 14:07:57 -080023import org.eclipse.jgit.errors.ConfigInvalidException;
24import org.eclipse.jgit.lib.Config;
25import org.eclipse.jgit.storage.file.FileBasedConfig;
26import org.eclipse.jgit.util.FS;
27
Dave Borowitz5d49ff22012-12-17 14:07:57 -080028public class GitilesConfig {
29 private static final String FILTER_CONFIG_PARAM = "configPath";
30 private static final String PROPERTY_NAME = "com.google.gitiles.configPath";
31 private static final String DEFAULT_PATH = "gitiles.config";
32
Dave Borowitz03397692012-12-18 17:11:16 -080033 public static File defaultFile() {
34 return defaultFile(null);
Dave Borowitz5d49ff22012-12-17 14:07:57 -080035 }
36
Dave Borowitz03397692012-12-18 17:11:16 -080037 public static File defaultFile(FilterConfig filterConfig) {
Ian Kumlienffaca342014-01-29 15:32:22 +010038 String configPath = System.getProperty(PROPERTY_NAME, DEFAULT_PATH);
39 if (configPath == null && filterConfig != null) {
Dave Borowitz5d49ff22012-12-17 14:07:57 -080040 configPath = filterConfig.getInitParameter(FILTER_CONFIG_PARAM);
41 }
David Pursehouse3c0207e2016-05-27 12:10:16 +090042 checkNotNull(configPath);
Dave Borowitz03397692012-12-18 17:11:16 -080043 return new File(configPath);
44 }
45
46 public static Config loadDefault() throws IOException, ConfigInvalidException {
47 return loadDefault(null);
48 }
49
50 public static Config loadDefault(FilterConfig filterConfig)
51 throws IOException, ConfigInvalidException {
Dave Borowitzc0960322013-03-26 23:53:27 -040052 FileBasedConfig config = new FileBasedConfig(defaultFile(filterConfig), FS.DETECTED);
Dave Borowitz5d49ff22012-12-17 14:07:57 -080053 config.load();
54 return config;
55 }
56
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020057 public static void putVariant(
58 Config config, String templateName, String keyName, Map<String, ? super String> out) {
Stefan Zagerd2187432014-02-28 02:23:02 -080059 String variant = config.getString("template", null, templateName);
60 if (variant != null) {
61 out.put(keyName, variant);
62 }
63 }
64
Dave Borowitz5d49ff22012-12-17 14:07:57 -080065 private GitilesConfig() {}
66}