Java tutorial
/* * Copyright 2013 Marcelo Morales me@marcelomorales.name * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package name.marcelomorales.siqisiqi.configuration; import com.google.common.base.Function; import com.google.common.base.Predicate; import com.google.common.base.Strings; import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.Iterables; import com.google.common.collect.Multimap; import com.google.common.collect.Multimaps; import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.annotation.Nullable; import javax.inject.Inject; import javax.inject.Provider; import java.io.File; import java.text.MessageFormat; import java.util.Map; /** * @author Marcelo Morales * Since: 8/23/13 */ public class ConfigProvider implements Provider<Config> { private static final Logger LOGGER = LoggerFactory.getLogger(ConfigProvider.class); private static final Multimap<String, String> TEMPLATES = ImmutableMultimap.<String, String>builder() .put("APPDATA", "{0}/{1}/{2}"). // Windows personal ej \Documents And Settings\TheProject\example.json} put("HOME", "{0}/.{1}/{2}"). // Linux personal ej /home/marcelo/.theproject/example.json put("HOME", "{0}/.{2}"). // Linux personal ej /home/marcelo/.example.json put("PROGRAMDATA", "{0}/{1}/{2}"). // Windows global put("", "/etc/{1}/{2}"). // Linux global put("", "/etc/{2}").build(); // Linux global (2) private final String applicationName; private final String vendorName; @Inject public ConfigProvider(String vendorName, String applicationName) { this.vendorName = vendorName; this.applicationName = applicationName; } @Override public Config get() { Config config = ConfigFactory.systemProperties(); for (String fileTemplate : possibleConfigFilePositions()) { config = config.withFallback(ConfigFactory.parseFileAnySyntax(new File(fileTemplate))); } config = config.withFallback(ConfigFactory.parseResourcesAnySyntax(applicationName)); config = config.withFallback(ConfigFactory.defaultReference()); return config; } public Iterable<String> possibleConfigFilePositions() { // Filter out when APPDATA or HOME or anything else is missing final Multimap<String, String> applicableTemplates = Multimaps.filterKeys(TEMPLATES, new Predicate<String>() { @Override public boolean apply(@Nullable String input) { return Strings.isNullOrEmpty(input) || !Strings.isNullOrEmpty(System.getenv(input)); } }); final Iterable<String> transform = Iterables.transform(applicableTemplates.entries(), new Function<Map.Entry<String, String>, String>() { @Override public String apply(Map.Entry<String, String> input) { return MessageFormat.format(input.getValue(), System.getenv(input.getKey()), vendorName, applicationName); } }); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Will check all these paths in order {}", Iterables.toString(transform)); } return transform; } public ConfigProvider lifted(final String subconfigKey) { if (Strings.isNullOrEmpty(subconfigKey)) { return this; } return new ConfigProvider(vendorName, applicationName) { @Override public Config get() { return lift(super.get(), subconfigKey); } }; } public static Config lift(Config original, String subconfigKey) { return original.getConfig(subconfigKey).withFallback(original); } }