| 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 | |
| 17 | import org.eclipse.jgit.errors.ConfigInvalidException; |
| 18 | import org.eclipse.jgit.lib.Config; |
| 19 | import org.eclipse.jgit.storage.file.FileBasedConfig; |
| 20 | import org.eclipse.jgit.util.FS; |
| 21 | |
| 22 | import java.io.File; |
| 23 | import java.io.IOException; |
| 24 | |
| 25 | import javax.servlet.FilterConfig; |
| 26 | |
| 27 | public class GitilesConfig { |
| 28 | private static final String FILTER_CONFIG_PARAM = "configPath"; |
| 29 | private static final String PROPERTY_NAME = "com.google.gitiles.configPath"; |
| 30 | private static final String DEFAULT_PATH = "gitiles.config"; |
| 31 | |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 32 | public static File defaultFile() { |
| 33 | return defaultFile(null); |
| Dave Borowitz | 5d49ff2 | 2012-12-17 14:07:57 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 36 | public static File defaultFile(FilterConfig filterConfig) { |
| Dave Borowitz | 5d49ff2 | 2012-12-17 14:07:57 -0800 | [diff] [blame] | 37 | String configPath = null; |
| 38 | if (filterConfig != null) { |
| 39 | configPath = filterConfig.getInitParameter(FILTER_CONFIG_PARAM); |
| 40 | } |
| 41 | if (configPath == null) { |
| 42 | configPath = System.getProperty(PROPERTY_NAME, DEFAULT_PATH); |
| 43 | } |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 44 | return new File(configPath); |
| 45 | } |
| 46 | |
| 47 | public static Config loadDefault() throws IOException, ConfigInvalidException { |
| 48 | return loadDefault(null); |
| 49 | } |
| 50 | |
| 51 | public static Config loadDefault(FilterConfig filterConfig) |
| 52 | throws IOException, ConfigInvalidException { |
| Dave Borowitz | c096032 | 2013-03-26 23:53:27 -0400 | [diff] [blame] | 53 | FileBasedConfig config = new FileBasedConfig(defaultFile(filterConfig), FS.DETECTED); |
| Dave Borowitz | 5d49ff2 | 2012-12-17 14:07:57 -0800 | [diff] [blame] | 54 | config.load(); |
| 55 | return config; |
| 56 | } |
| 57 | |
| 58 | private GitilesConfig() {} |
| 59 | } |