blob: 131a62c97556c18ac6779b590c7e461c2bfd3f46 [file] [log] [blame]
Dave Borowitz9de65952012-08-13 16:09:45 -07001// 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
Jonathan Niederc49e92e2016-06-30 16:00:50 -070017import static java.util.concurrent.TimeUnit.MILLISECONDS;
18
Dave Borowitz9de65952012-08-13 16:09:45 -070019import com.google.common.cache.CacheBuilder;
David Pursehousec0037b82018-03-16 13:56:20 +090020import java.time.Duration;
David Pursehouse7a7f5472016-10-14 09:59:20 +090021import java.util.Optional;
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070022import java.util.TimeZone;
Dave Borowitz9de65952012-08-13 16:09:45 -070023import java.util.concurrent.TimeUnit;
Jonathan Niederc49e92e2016-06-30 16:00:50 -070024import javax.annotation.Nullable;
Dave Borowitz3b744b12016-08-19 16:11:10 -040025import org.eclipse.jgit.lib.Config;
Jonathan Niederc49e92e2016-06-30 16:00:50 -070026
Dave Borowitz9de65952012-08-13 16:09:45 -070027/** Utilities for working with {@link Config} objects. */
28public class ConfigUtil {
29 /**
30 * Read a duration value from the configuration.
Dave Borowitz40255d52016-08-19 16:16:22 -040031 *
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 Borowitz9de65952012-08-13 16:09:45 -070034 *
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 Pursehouse18e70c52018-03-17 09:39:42 +090042 @Nullable
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020043 public static Duration getDuration(
Dave Borowitz40255d52016-08-19 16:16:22 -040044 Config config,
45 String section,
46 String subsection,
47 String name,
Jonathan Niederc49e92e2016-06-30 16:00:50 -070048 @Nullable Duration defaultValue) {
49 long m = config.getTimeUnit(section, subsection, name, -1, MILLISECONDS);
David Pursehousec0037b82018-03-16 13:56:20 +090050 return m == -1 ? defaultValue : Duration.ofMillis(m);
Dave Borowitz9de65952012-08-13 16:09:45 -070051 }
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 Borowitzddd96b82014-04-18 10:57:18 -070061 CacheBuilder<Object, Object> b = CacheBuilder.newBuilder();
Dave Borowitz9de65952012-08-13 16:09:45 -070062 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 Pursehousec0037b82018-03-16 13:56:20 +090071 b.expireAfterWrite(expireAfterWrite.toMillis(), TimeUnit.MILLISECONDS);
Dave Borowitz9de65952012-08-13 16:09:45 -070072 }
73 Duration expireAfterAccess = getDuration(config, "cache", name, "expireAfterAccess", null);
74 if (expireAfterAccess != null) {
David Pursehousec0037b82018-03-16 13:56:20 +090075 b.expireAfterAccess(expireAfterAccess.toMillis(), TimeUnit.MILLISECONDS);
Dave Borowitz9de65952012-08-13 16:09:45 -070076 }
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 Borowitz2b2f34b2014-04-29 16:47:20 -070086 /**
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 Borowitz40255d52016-08-19 16:16:22 -040093 * @return a time zone read from parsing the specified config string value, or {@link
David Pursehouse7a7f5472016-10-14 09:59:20 +090094 * Optional#empty()} if not present. As in the behavior of {@link
Dave Borowitz40255d52016-08-19 16:16:22 -040095 * TimeZone#getTimeZone(String)}, unknown time zones are treated as GMT.
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070096 */
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020097 public static Optional<TimeZone> getTimeZone(
98 Config config, String section, String subsection, String name) {
David Pursehousec9621972016-10-17 14:56:57 +090099 return Optional.ofNullable(config.getString(section, subsection, name))
100 .map(TimeZone::getTimeZone);
Dave Borowitz2b2f34b2014-04-29 16:47:20 -0700101 }
102
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200103 private ConfigUtil() {}
Dave Borowitz9de65952012-08-13 16:09:45 -0700104}