blob: 2fbe102f839d8e1f7ac7d6124f07744799de67d1 [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 Pursehouse7a7f5472016-10-14 09:59:20 +090020import java.util.Optional;
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070021import java.util.TimeZone;
Dave Borowitz9de65952012-08-13 16:09:45 -070022import java.util.concurrent.TimeUnit;
Jonathan Niederc49e92e2016-06-30 16:00:50 -070023import javax.annotation.Nullable;
Dave Borowitz3b744b12016-08-19 16:11:10 -040024import org.eclipse.jgit.lib.Config;
25import org.joda.time.Duration;
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 */
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020042 public static Duration getDuration(
Dave Borowitz40255d52016-08-19 16:16:22 -040043 Config config,
44 String section,
45 String subsection,
46 String name,
Jonathan Niederc49e92e2016-06-30 16:00:50 -070047 @Nullable Duration defaultValue) {
48 long m = config.getTimeUnit(section, subsection, name, -1, MILLISECONDS);
49 return m == -1 ? defaultValue : Duration.millis(m);
Dave Borowitz9de65952012-08-13 16:09:45 -070050 }
51
52 /**
53 * Get a {@link CacheBuilder} from a config.
54 *
55 * @param config JGit config object.
56 * @param name name of the cache subsection under the "cache" section.
57 * @return a new cache builder.
58 */
59 public static CacheBuilder<Object, Object> getCacheBuilder(Config config, String name) {
Dave Borowitzddd96b82014-04-18 10:57:18 -070060 CacheBuilder<Object, Object> b = CacheBuilder.newBuilder();
Dave Borowitz9de65952012-08-13 16:09:45 -070061 try {
62 if (config.getString("cache", name, "maximumWeight") != null) {
63 b.maximumWeight(config.getLong("cache", name, "maximumWeight", 20 << 20));
64 }
65 if (config.getString("cache", name, "maximumSize") != null) {
66 b.maximumSize(config.getLong("cache", name, "maximumSize", 16384));
67 }
68 Duration expireAfterWrite = getDuration(config, "cache", name, "expireAfterWrite", null);
69 if (expireAfterWrite != null) {
70 b.expireAfterWrite(expireAfterWrite.getMillis(), TimeUnit.MILLISECONDS);
71 }
72 Duration expireAfterAccess = getDuration(config, "cache", name, "expireAfterAccess", null);
73 if (expireAfterAccess != null) {
74 b.expireAfterAccess(expireAfterAccess.getMillis(), TimeUnit.MILLISECONDS);
75 }
76 // Add other methods as needed.
77 } catch (IllegalArgumentException e) {
78 throw new IllegalArgumentException("Error getting CacheBuilder for " + name, e);
79 } catch (IllegalStateException e) {
80 throw new IllegalStateException("Error getting CacheBuilder for " + name, e);
81 }
82 return b;
83 }
84
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070085 /**
86 * Get a {@link TimeZone} from a config.
87 *
88 * @param config JGit config object.
89 * @param section section to read, e.g. "gitiles".
90 * @param subsection subsection to read, e.g. "subsection".
91 * @param name variable to read, e.g. "fixedTimeZone".
Dave Borowitz40255d52016-08-19 16:16:22 -040092 * @return a time zone read from parsing the specified config string value, or {@link
David Pursehouse7a7f5472016-10-14 09:59:20 +090093 * Optional#empty()} if not present. As in the behavior of {@link
Dave Borowitz40255d52016-08-19 16:16:22 -040094 * TimeZone#getTimeZone(String)}, unknown time zones are treated as GMT.
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070095 */
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020096 public static Optional<TimeZone> getTimeZone(
97 Config config, String section, String subsection, String name) {
David Pursehousec9621972016-10-17 14:56:57 +090098 return Optional.ofNullable(config.getString(section, subsection, name))
99 .map(TimeZone::getTimeZone);
Dave Borowitz2b2f34b2014-04-29 16:47:20 -0700100 }
101
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200102 private ConfigUtil() {}
Dave Borowitz9de65952012-08-13 16:09:45 -0700103}