| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [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 | |
| Jonathan Nieder | c49e92e | 2016-06-30 16:00:50 -0700 | [diff] [blame] | 17 | import static java.util.concurrent.TimeUnit.MILLISECONDS; |
| 18 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 19 | import com.google.common.cache.CacheBuilder; |
| David Pursehouse | c0037b8 | 2018-03-16 13:56:20 +0900 | [diff] [blame] | 20 | import java.time.Duration; |
| David Pursehouse | 7a7f547 | 2016-10-14 09:59:20 +0900 | [diff] [blame] | 21 | import java.util.Optional; |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 22 | import java.util.TimeZone; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 23 | import java.util.concurrent.TimeUnit; |
| Jonathan Nieder | c49e92e | 2016-06-30 16:00:50 -0700 | [diff] [blame] | 24 | import javax.annotation.Nullable; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 25 | import org.eclipse.jgit.lib.Config; |
| Jonathan Nieder | c49e92e | 2016-06-30 16:00:50 -0700 | [diff] [blame] | 26 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 27 | /** Utilities for working with {@link Config} objects. */ |
| 28 | public class ConfigUtil { |
| 29 | /** |
| 30 | * Read a duration value from the configuration. |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 31 | * |
| 32 | * <p>Durations can be written with unit suffixes, for example {@code "1 s"} or {@code "5 days"}. |
| 33 | * If units are not specified, milliseconds are assumed. |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 34 | * |
| 35 | * @param config JGit config object. |
| 36 | * @param section section to read, e.g. "google" |
| 37 | * @param subsection subsection to read, e.g. "bigtable" |
| 38 | * @param name variable to read, e.g. "deadline". |
| 39 | * @param defaultValue value to use when the value is not assigned. |
| 40 | * @return a standard duration representing the time read, or defaultValue. |
| 41 | */ |
| David Pursehouse | 18e70c5 | 2018-03-17 09:39:42 +0900 | [diff] [blame] | 42 | @Nullable |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 43 | public static Duration getDuration( |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 44 | Config config, |
| 45 | String section, |
| 46 | String subsection, |
| 47 | String name, |
| Jonathan Nieder | c49e92e | 2016-06-30 16:00:50 -0700 | [diff] [blame] | 48 | @Nullable Duration defaultValue) { |
| 49 | long m = config.getTimeUnit(section, subsection, name, -1, MILLISECONDS); |
| David Pursehouse | c0037b8 | 2018-03-16 13:56:20 +0900 | [diff] [blame] | 50 | return m == -1 ? defaultValue : Duration.ofMillis(m); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Get a {@link CacheBuilder} from a config. |
| 55 | * |
| 56 | * @param config JGit config object. |
| 57 | * @param name name of the cache subsection under the "cache" section. |
| 58 | * @return a new cache builder. |
| 59 | */ |
| 60 | public static CacheBuilder<Object, Object> getCacheBuilder(Config config, String name) { |
| Dave Borowitz | ddd96b8 | 2014-04-18 10:57:18 -0700 | [diff] [blame] | 61 | CacheBuilder<Object, Object> b = CacheBuilder.newBuilder(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 62 | try { |
| 63 | if (config.getString("cache", name, "maximumWeight") != null) { |
| 64 | b.maximumWeight(config.getLong("cache", name, "maximumWeight", 20 << 20)); |
| 65 | } |
| 66 | if (config.getString("cache", name, "maximumSize") != null) { |
| 67 | b.maximumSize(config.getLong("cache", name, "maximumSize", 16384)); |
| 68 | } |
| 69 | Duration expireAfterWrite = getDuration(config, "cache", name, "expireAfterWrite", null); |
| 70 | if (expireAfterWrite != null) { |
| David Pursehouse | c0037b8 | 2018-03-16 13:56:20 +0900 | [diff] [blame] | 71 | b.expireAfterWrite(expireAfterWrite.toMillis(), TimeUnit.MILLISECONDS); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 72 | } |
| 73 | Duration expireAfterAccess = getDuration(config, "cache", name, "expireAfterAccess", null); |
| 74 | if (expireAfterAccess != null) { |
| David Pursehouse | c0037b8 | 2018-03-16 13:56:20 +0900 | [diff] [blame] | 75 | b.expireAfterAccess(expireAfterAccess.toMillis(), TimeUnit.MILLISECONDS); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 76 | } |
| 77 | // Add other methods as needed. |
| 78 | } catch (IllegalArgumentException e) { |
| 79 | throw new IllegalArgumentException("Error getting CacheBuilder for " + name, e); |
| 80 | } catch (IllegalStateException e) { |
| 81 | throw new IllegalStateException("Error getting CacheBuilder for " + name, e); |
| 82 | } |
| 83 | return b; |
| 84 | } |
| 85 | |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 86 | /** |
| 87 | * Get a {@link TimeZone} from a config. |
| 88 | * |
| 89 | * @param config JGit config object. |
| 90 | * @param section section to read, e.g. "gitiles". |
| 91 | * @param subsection subsection to read, e.g. "subsection". |
| 92 | * @param name variable to read, e.g. "fixedTimeZone". |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 93 | * @return a time zone read from parsing the specified config string value, or {@link |
| David Pursehouse | 7a7f547 | 2016-10-14 09:59:20 +0900 | [diff] [blame] | 94 | * Optional#empty()} if not present. As in the behavior of {@link |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 95 | * TimeZone#getTimeZone(String)}, unknown time zones are treated as GMT. |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 96 | */ |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 97 | public static Optional<TimeZone> getTimeZone( |
| 98 | Config config, String section, String subsection, String name) { |
| David Pursehouse | c962197 | 2016-10-17 14:56:57 +0900 | [diff] [blame] | 99 | return Optional.ofNullable(config.getString(section, subsection, name)) |
| 100 | .map(TimeZone::getTimeZone); |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 103 | private ConfigUtil() {} |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 104 | } |