| Dave Borowitz | 5d49ff2 | 2012-12-17 14:07:57 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.gitiles; |
| 16 | |
| David Pursehouse | 3c0207e | 2016-05-27 12:10:16 +0900 | [diff] [blame] | 17 | import static com.google.common.base.Preconditions.checkNotNull; |
| 18 | |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 19 | import java.io.File; |
| 20 | import java.io.IOException; |
| 21 | import java.util.Map; |
| 22 | import javax.servlet.FilterConfig; |
| Dave Borowitz | 5d49ff2 | 2012-12-17 14:07:57 -0800 | [diff] [blame] | 23 | import org.eclipse.jgit.errors.ConfigInvalidException; |
| 24 | import org.eclipse.jgit.lib.Config; |
| 25 | import org.eclipse.jgit.storage.file.FileBasedConfig; |
| 26 | import org.eclipse.jgit.util.FS; |
| 27 | |
| Dave Borowitz | 5d49ff2 | 2012-12-17 14:07:57 -0800 | [diff] [blame] | 28 | public 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 Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 33 | public static File defaultFile() { |
| 34 | return defaultFile(null); |
| Dave Borowitz | 5d49ff2 | 2012-12-17 14:07:57 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 37 | public static File defaultFile(FilterConfig filterConfig) { |
| Ian Kumlien | ffaca34 | 2014-01-29 15:32:22 +0100 | [diff] [blame] | 38 | String configPath = System.getProperty(PROPERTY_NAME, DEFAULT_PATH); |
| 39 | if (configPath == null && filterConfig != null) { |
| Dave Borowitz | 5d49ff2 | 2012-12-17 14:07:57 -0800 | [diff] [blame] | 40 | configPath = filterConfig.getInitParameter(FILTER_CONFIG_PARAM); |
| 41 | } |
| David Pursehouse | 3c0207e | 2016-05-27 12:10:16 +0900 | [diff] [blame] | 42 | checkNotNull(configPath); |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 43 | 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 Borowitz | c096032 | 2013-03-26 23:53:27 -0400 | [diff] [blame] | 52 | FileBasedConfig config = new FileBasedConfig(defaultFile(filterConfig), FS.DETECTED); |
| Dave Borowitz | 5d49ff2 | 2012-12-17 14:07:57 -0800 | [diff] [blame] | 53 | config.load(); |
| 54 | return config; |
| 55 | } |
| 56 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 57 | public static void putVariant( |
| 58 | Config config, String templateName, String keyName, Map<String, ? super String> out) { |
| Stefan Zager | d218743 | 2014-02-28 02:23:02 -0800 | [diff] [blame] | 59 | String variant = config.getString("template", null, templateName); |
| 60 | if (variant != null) { |
| 61 | out.put(keyName, variant); |
| 62 | } |
| 63 | } |
| 64 | |
| Dave Borowitz | 5d49ff2 | 2012-12-17 14:07:57 -0800 | [diff] [blame] | 65 | private GitilesConfig() {} |
| 66 | } |