• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.caliper.config;
18 
19 import com.google.caliper.options.CaliperOptions;
20 import com.google.caliper.util.Util;
21 import com.google.common.base.Predicates;
22 import com.google.common.collect.ImmutableMap;
23 import com.google.common.collect.Iterables;
24 import com.google.common.collect.Maps;
25 import com.google.common.io.ByteSource;
26 import com.google.common.io.Files;
27 
28 import java.io.File;
29 import java.io.IOException;
30 import java.util.Map;
31 
32 import javax.inject.Inject;
33 import javax.inject.Singleton;
34 
35 /**
36  * Loads caliper configuration files and, if necessary, creates new versions from the defaults.
37  */
38 @Singleton
39 public final class CaliperConfigLoader {
40   private final CaliperOptions options;
41 
CaliperConfigLoader(CaliperOptions options)42   @Inject CaliperConfigLoader(CaliperOptions options) {
43     this.options = options;
44   }
45 
loadOrCreate()46   public CaliperConfig loadOrCreate() throws InvalidConfigurationException {
47     File configFile = options.caliperConfigFile();
48     ImmutableMap<String, String> defaults;
49     try {
50       defaults = Util.loadProperties(
51           Util.resourceSupplier(CaliperConfig.class, "global-config.properties"));
52     } catch (IOException impossible) {
53       throw new AssertionError(impossible);
54     }
55 
56     // TODO(kevinb): deal with migration issue from old-style .caliperrc
57 
58     if (configFile.exists()) {
59       try {
60         ImmutableMap<String, String> user =
61             Util.loadProperties(Files.asByteSource(configFile));
62         return new CaliperConfig(mergeProperties(options.configProperties(), user, defaults));
63       } catch (IOException keepGoing) {
64       }
65     }
66 
67     ByteSource supplier = Util.resourceSupplier(CaliperConfig.class, "default-config.properties");
68     tryCopyIfNeeded(supplier, configFile);
69 
70     ImmutableMap<String, String> user;
71     try {
72       user = Util.loadProperties(supplier);
73     } catch (IOException e) {
74       throw new AssertionError(e); // class path must be messed up
75     }
76     return new CaliperConfig(mergeProperties(options.configProperties(), user, defaults));
77   }
78 
mergeProperties(Map<String, String> commandLine, Map<String, String> user, Map<String, String> defaults)79   private static ImmutableMap<String, String> mergeProperties(Map<String, String> commandLine,
80       Map<String, String> user,
81       Map<String, String> defaults) {
82     Map<String, String> map = Maps.newHashMap(defaults);
83     map.putAll(user); // overwrite and augment
84     map.putAll(commandLine); // overwrite and augment
85     Iterables.removeIf(map.values(), Predicates.equalTo(""));
86     return ImmutableMap.copyOf(map);
87   }
88 
tryCopyIfNeeded(ByteSource supplier, File rcFile)89   private static void tryCopyIfNeeded(ByteSource supplier, File rcFile) {
90     if (!rcFile.exists()) {
91       try {
92         supplier.copyTo(Files.asByteSink(rcFile));
93       } catch (IOException e) {
94         rcFile.delete();
95       }
96     }
97   }
98 }
99