blob: 4f7b40f3f148e537f8d575d54e302cfa1f8e21fa [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
17import org.eclipse.jgit.errors.ConfigInvalidException;
18import org.eclipse.jgit.lib.Config;
19import org.eclipse.jgit.storage.file.FileBasedConfig;
20import org.eclipse.jgit.util.FS;
21
22import java.io.File;
23import java.io.IOException;
Stefan Zagerd2187432014-02-28 02:23:02 -080024import java.util.Map;
Dave Borowitz5d49ff22012-12-17 14:07:57 -080025
26import javax.servlet.FilterConfig;
27
28public 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 }
Dave Borowitz03397692012-12-18 17:11:16 -080042 return new File(configPath);
43 }
44
45 public static Config loadDefault() throws IOException, ConfigInvalidException {
46 return loadDefault(null);
47 }
48
49 public static Config loadDefault(FilterConfig filterConfig)
50 throws IOException, ConfigInvalidException {
Dave Borowitzc0960322013-03-26 23:53:27 -040051 FileBasedConfig config = new FileBasedConfig(defaultFile(filterConfig), FS.DETECTED);
Dave Borowitz5d49ff22012-12-17 14:07:57 -080052 config.load();
53 return config;
54 }
55
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020056 public static void putVariant(
57 Config config, String templateName, String keyName, Map<String, ? super String> out) {
Stefan Zagerd2187432014-02-28 02:23:02 -080058 String variant = config.getString("template", null, templateName);
59 if (variant != null) {
60 out.put(keyName, variant);
61 }
62 }
63
Dave Borowitz5d49ff22012-12-17 14:07:57 -080064 private GitilesConfig() {}
65}