blob: 8765d8d52c46dd9bfe30b9de33d3d24886002cf9 [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 {
Dave Borowitz8650d072018-06-13 14:58:57 -040063 if (config.getString("cache", name, "concurrencyLevel") != null) {
64 b.concurrencyLevel(config.getInt("cache", name, "concurrencyLevel", 4));
65 }
Dave Borowitz9de65952012-08-13 16:09:45 -070066 if (config.getString("cache", name, "maximumWeight") != null) {
67 b.maximumWeight(config.getLong("cache", name, "maximumWeight", 20 << 20));
68 }
69 if (config.getString("cache", name, "maximumSize") != null) {
70 b.maximumSize(config.getLong("cache", name, "maximumSize", 16384));
71 }
72 Duration expireAfterWrite = getDuration(config, "cache", name, "expireAfterWrite", null);
73 if (expireAfterWrite != null) {
David Pursehousec0037b82018-03-16 13:56:20 +090074 b.expireAfterWrite(expireAfterWrite.toMillis(), TimeUnit.MILLISECONDS);
Dave Borowitz9de65952012-08-13 16:09:45 -070075 }
76 Duration expireAfterAccess = getDuration(config, "cache", name, "expireAfterAccess", null);
77 if (expireAfterAccess != null) {
David Pursehousec0037b82018-03-16 13:56:20 +090078 b.expireAfterAccess(expireAfterAccess.toMillis(), TimeUnit.MILLISECONDS);
Dave Borowitz9de65952012-08-13 16:09:45 -070079 }
80 // Add other methods as needed.
81 } catch (IllegalArgumentException e) {
82 throw new IllegalArgumentException("Error getting CacheBuilder for " + name, e);
83 } catch (IllegalStateException e) {
84 throw new IllegalStateException("Error getting CacheBuilder for " + name, e);
85 }
86 return b;
87 }
88
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070089 /**
90 * Get a {@link TimeZone} from a config.
91 *
92 * @param config JGit config object.
93 * @param section section to read, e.g. "gitiles".
94 * @param subsection subsection to read, e.g. "subsection".
95 * @param name variable to read, e.g. "fixedTimeZone".
Dave Borowitz40255d52016-08-19 16:16:22 -040096 * @return a time zone read from parsing the specified config string value, or {@link
David Pursehouse7a7f5472016-10-14 09:59:20 +090097 * Optional#empty()} if not present. As in the behavior of {@link
Dave Borowitz40255d52016-08-19 16:16:22 -040098 * TimeZone#getTimeZone(String)}, unknown time zones are treated as GMT.
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070099 */
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200100 public static Optional<TimeZone> getTimeZone(
101 Config config, String section, String subsection, String name) {
David Pursehousec9621972016-10-17 14:56:57 +0900102 return Optional.ofNullable(config.getString(section, subsection, name))
103 .map(TimeZone::getTimeZone);
Dave Borowitz2b2f34b2014-04-29 16:47:20 -0700104 }
105
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200106 private ConfigUtil() {}
Dave Borowitz9de65952012-08-13 16:09:45 -0700107}